logo

识别色卡颜色并找到相对应颜色的气球扎破,我写的这个代码可以实现吗?

2021-09-15 23:28
6
iear
import sensor, image, time

sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 格式为 RGB565.
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(10) # 跳过10帧,使新设置生效
sensor.set_auto_whitebal(False)               
sensor.set_auto_gain(False)
clock=time.clock()
ROI=(0,0,300,300)

while(True):
    img = sensor.snapshot()         
    statistics=img.get_statistics(roi=ROI)
    color_l=statistics.l_mode()
    color_a=statistics.a_mode()
    color_b=statistics.b_mode()
    print(color_l,color_a,color_b)
    img.draw_rectangle(ROI)
    anycolor_threshold=(color_l,color_a,color_b)
    blobs =img.find_blobs([anycolor_threshold])
    if blobs:
    #如果找到了目标颜色
        for b in blobs:
             img.draw_rectangle(b[0:4]) 
             img.draw_cross(b[5], b[6]) 
            

        print(clock.fps())
2021-09-16 20:57
I
网红小智智 星瞳科技

需要你自己测试。如果出现了问题可以问具体问题。

2021-09-18 12:16
6
iear
ea5f489f-11bc-4d64-ad68-4c315fe4ffdb.png
2021-09-22 12:26
I
网红小智智 星瞳科技

最后结果不对?

我看你的代码是ROI=(0,0,300,300),可是你的画面是QVGA(320*240)

I
网红小智智 星瞳科技

你先测试代码:https://book.openmv.cc/example/10-Color-Tracking/automatic-rgb565-color-tracking.html

2021-09-22 16:59
6
iear

@kidswong999 这个代码测出来的画面有跟踪颜色的,是可以的,是我这个的代码不对吗,用统计信息得到的LAB颜色值是不等同于颜色阈值吗,我把roi调成(0,0,320,240)还是这样 黑色也出现好多+

f79121d6-d929-44f5-963b-89feba333626.png
2021-09-23 19:01
I
网红小智智 星瞳科技

LAB是有上下范围的,你把例子里的阈值打印出来,再把你的代码里的阈值打印出来看看就知道了。

2021-09-23 23:09
6
iear
import sensor, image, time
red_threshold=(0, 100, 7, 127, -128, 127)
green_threshold=(0, 100, -128, -28, -128, 127)
blue_threshold=(14, 87, -83, 127, -113, -14)
red_color_code = 1
blue_color_code = 4
green_color_code = 8

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(10) # 跳过10帧,使新设置生效
sensor.set_auto_whitebal(False)
sensor.set_auto_gain(False)
clock=time.clock()
ROI=(0,0,80,80)

while(True):
    img = sensor.snapshot()
    statistics=img.get_statistics(roi=ROI)
    color_l_min=statistics.l_min()
    color_a_min=statistics.a_min()
    color_b_min=statistics.b_min()
    color_l_max=statistics.l_max()
    color_a_max=statistics.a_max()
    color_b_max=statistics.b_max()
    anycolor_threshold=(color_l_min,color_l_max,color_a_min,color_a_max,color_b_min,color_b_max)
    blobs=img.find_blobs([anycolor_threshold],area_threshold=100)
    if anycolor_threshold==red_threshold:
        blobs =img.find_blobs([red_threshold],area_threshold=100)
    elif anycolor_threshold==green_threshold:
        blobs =img.find_blobs([green_threshold],area_threshold=100)
    elif anycolor_threshold==blue_threshold:
        blobs =img.find_blobs([blue_threshold],area_threshold=100)
    if blobs:
        for blob in blobs:
        #迭代找到的目标颜色区域
            x = blob[0]
            y = blob[1]
            width = blob[2]
            height = blob[3]
            center_x = blob[5]
            center_y = blob[6]
            color_code = blob[8]
            if color_code == red_color_code:
                img.draw_string(x, y - 10, "red", color = (0xFF, 0x00, 0x00))
            elif color_code == blue_color_code:
                img.draw_string(x, y - 10, "blue", color = (0xFF, 0x00, 0x00))
            elif color_code == green_color_code:
                img.draw_string(x, y - 10, "green", color = (0xFF, 0x00, 0x00))

            #用矩形标记出目标颜色区域
            img.draw_rectangle(blob[0],blob[1],blob[2],blob[3])
            #在目标颜色区域的中心画十字形标记
            img.draw_cross(blob[5], blob[6])



    print(clock.fps())
6
iear
1d3e2390-c604-41d2-8d5b-6a011cb3f03c.png

代码改了一点 结果是这样的