实现效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210210115232389.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1JhbXBhZ2VQQlo=,size_16,color_FFFFFF,t_70#pic_center)
具体代码
在屏幕上放的测试图片,实物可能需要更改一下阈值
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())