logo

我想要按顺序识别色块,但为什么老说没定义center_x1呢,我前面有定义呀

2019-02-24 17:04
Z
_5i1g
while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # 拍照,返回图像
    # 在图像中寻找满足颜色阈值约束(color_threshold, 数组格式), 像素阈值pixel_threshold, 色块面积大小阈值(area_threshold)的色块
    #blobs = img.find_blobs([red_threshold],[green_threshold],[blue_threshold])
    #blobs = img.find_blobs([red_threshold, green_threshold, blue_threshold],area_threshold=200,) #merge=True)
    red_blobs = img.find_blobs([red_threshold],area_threshold=200)# merge=False)
    green_blobs = img.find_blobs([green_threshold],area_threshold=200)# merge=False)
    blue_blobs=img.find_blobs([blue_threshold],area_threshold=200)#, merge=False)

    #if blobs:
#如果找到了目标颜色
        #for blob in blobs:
#迭代找到的目标颜色区域
            #x = blob[0] #x坐标
             #y= blob[1] #y坐标
            #width = blob[2] # 色块矩形的宽度
            #height = blob[3] # 色块矩形的高度

            #center_x = blob[5] # 色块中心点x值
            #center_y = blob[6] # 色块中心点y值

            #color_code = blob[8] # 颜色代码
    



    if red_blobs:
#如果找到了目标颜色
        for rblob in red_blobs:
#迭代找到的目标颜色区域
            x1 = rblob[0] #x坐标
            y1 = rblob[1] #y坐标
            width1 = rblob[2] # 色块矩形的宽度
            height1 = rblob[3] # 色块矩形的高度

            center_x1 = rblob[5] # 色块中心点x值
            center_y1 = rblob[6] # 色块中心点y值

            color_code1 = rblob[8] # 颜色代码
    
    if green_blobs:
#如果找到了目标颜色
        for gblob in green_blobs:
#迭代找到的目标颜色区域
            x2 = gblob[0] #x坐标
            y2 = gblob[1] #y坐标
            width2 = gblob[2] # 色块矩形的宽度
            height2 = gblob[3] # 色块矩形的高度

            center_x2 = gblob[5] # 色块中心点x值
            center_y2 = gblob[6] # 色块中心点y值

            color_code2 = gblob[8] # 颜色代码
    
    if blue_blobs:
#如果找到了目标颜色
        for bblob in blue_blobs:
#迭代找到的目标颜色区域
            x3 = bblob[0] #x坐标
            y3 = bblob[1] #y坐标
            width3 = bblob[2] # 色块矩形的宽度
            height3 = bblob[3] # 色块矩形的高度

            center_x3 = bblob[5] # 色块中心点x值
            center_y3 = bblob[6] # 色块中心点y值

            color_code3 = bblob[8] # 颜色代码
    



        # 添加颜色说明
        #if color_code == red_color_code:
         #   img.draw_string(x, y - 10, "red", color = (0xFF, 0x00, 0x00))
        #elif color_code == green_color_code:
         #   img.draw_string(x, y - 10, "green", color = (0x00, 0xFF, 0x00))
        #elif color_code == blue_color_code:
         #   img.draw_string(x, y - 10, "blue", color = (0x00, 0x00, 0xFF))

        #用矩形标记出目标颜色区域
        #img.draw_rectangle([x, y, width, height])
        #在目标颜色区域的中心画十字形标记
        #img.draw_cross(center_x, center_y)

    if center_x1>center_x2 and center_x2>center_x3:
		print(color_code1,color_code2,color_code3)
    if center_x1>center_x3 and center_x3>center_x2:
		print(color_code1,color_code3,color_code2)
    if center_x2>center_x3 and center_x3>center_x1:
		print(color_code2,color_code3,color_code1)
    if center_x2>center_x1 and center_x1>center_x3:
		print(color_code2,color_code1,color_code3)
    if center_x3>center_x2 and center_x2>center_x1:
		print(color_code3,color_code2,color_code1)
    if center_x3>center_x1 and center_x1>center_x2:
		print(color_code3,color_code1,color_code2)
2019-02-24 19:08
I
网红小智智 星瞳科技

你的代码是,只有找到了red_blobs,才会定义center_x1。

也就是说,没找到色块,就会出现未定义的错误。

2019-02-26 19:29
Z
_5i1g

那这程序要怎么改呢

2019-02-27 12:31
I
网红小智智 星瞳科技

你想一下,如果没有找到红色的色块你要做什么。

或者是三个颜色里,如果没找全怎么做。