OpenMV多色块识别

OpenMV多色块识别

  • 实现效果
  • 具体代码

实现效果

OpenMV多色块识别_第1张图片

具体代码

在屏幕上放的测试图片,实物可能需要更改一下阈值
import sensor, image, time ,pyb

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(100)
#初始化指示灯
led = pyb.LED(2)
led.on()
time.sleep(150)
led.off()

Red_threshold = (23, 77, 18, 101, -16, 96)
Green_threshold = (9, 87, -67, -11, -2, 66)
Blue_threshold = (12, 69, 2, 86, -128, -23)

Red_Blobs = None    #本帧捕捉到的色块列表
Green_Blobs = None
Blue_Blobs = None
Target_threshold = [Red_threshold,Green_threshold,Blue_threshold]   #目标色块阈值列表
Target_Blobs = [Red_Blobs,Green_Blobs,Blue_Blobs]   #注意!: 这是嵌套列表 , 成员为色块列表
Name_List = ['Red','Green','Blue']
Target_proportion = (1.05,2)      #色块长宽比例系数  W/H

clock = time.clock()

def TargetBlobs_Find():
    temp = 0
    while temp < 3:
        Target_Blobs[temp] = img.find_blobs([Target_threshold[temp]],merge = True,area_threshold = 600)
        if len(Target_Blobs[temp]) > 0:
            for Blob in Target_Blobs[temp]:
                proportion = Blob.h()/Blob.w()
                if proportion > Target_proportion[0] and proportion < Target_proportion[1]:
                    img.draw_rectangle(Blob.rect())
                    img.draw_string(Blob.x(),Blob.y(),Name_List[temp])
        temp += 1

while(True):
    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(strength=1.6)    #矫正画面
    TargetBlobs_Find()
    print(clock.fps())

你可能感兴趣的:(图像识别)