OpenMV多颜色识别详解

需提前观看:OpenMV单颜色识别详解

多颜色识别和单颜色识别差不多,只需要改动[thresholds[threshold_index]]这个地方

#更改之前 
for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
#更改之后
for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200, merge=True):

首先我查看OpenMV的官方函数介绍可知,thresholds 是一个元组列表 。[]表示是一个列表,()表示是一个元组,如果是[(),(),()]则表示这是一个元组列表。单颜色识别的时候,我们这个列表里面只有一个元组,而多颜色识别的时候,我们这个列表里面将会存放多个元组

因为我们是彩色识别,选择的RGB565图像,所以我们的元组需要包含6个值,分别是

 (L Min, L Max, A Min, A Max, B Min, B Max)。

总结,我们只需要thresholds 这一个元组列表从一个元组{()},变成多个元组{(),(),()},就可以实现从单颜色识别变成多颜色识别了。把代码如下。注意:注释掉的部分可以删除

# Single Color RGB565 Blob Tracking Example
#
# 这个例子展示了使用OpenMV Cam的单色RGB565跟踪。
import sensor, image, time, math

#threshold_index = 0 # 0 for red, 1 for green, 2 for blue

# 颜色跟踪阈值 (L Min, L Max, A Min, A Max, B Min, B Max)
# 下面的阈值通常跟踪红色/绿色/蓝色的东西。您可能希望调整它们……
thresholds = [(45, 60, 1, 18, 39, 55), # generic_red_thresholds
              (30, 48, -21, 0, -47, -9), # generic_green_thresholds
              ]     # generic_blue_thresholds

sensor.reset()#重置感光元件,重置摄像机
sensor.set_pixformat(sensor.RGB565) #设置颜色格式为RGB565,彩色,每个像素16bit。
sensor.set_framesize(sensor.QVGA)   #图像大小为QVGA
sensor.skip_frames(time = 2000)     #跳过n张照片,在更改设置后,跳过一些帧,等待感光元件变稳定。
sensor.set_auto_gain(False)         #颜色识别必须关闭自动增益,会影响颜色识别效果
sensor.set_auto_whitebal(False)     #颜色识别必须关闭白平衡,会影响颜色识别效果,导致颜色的阈值发生改变
clock = time.clock()

# 只有像素大于“pixels_threshold”和面积大于“area_threshold”的区域才是
# 由下面的"find_blobs"返回。更改“pixels_threshold”和“area_threshold”
# 相机的分辨率。"merge=True"合并图像中所有重叠的斑点。

while(True):
    clock.tick()# 追踪两个snapshots()之间经过的毫秒数.
    img = sensor.snapshot()#截取感光元件中的一张图片
    #在img.find_blobs这个函数中,我们进行颜色识别
    #roi是“感兴趣区”,是在画面的中央还是右上方或者哪里进行颜色识别。此处我们没有进行配置,默认整个图像进行识别
    for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200, merge=True):
        # 这些值依赖于blob不是循环的-否则它们将不稳定。
        #if blob.elongation() > 0.5:
            #img.draw_edges(blob.min_corners(), color=(255,0,0))    #利用一个红色的方框,绘制出Blob的最小边界
            #img.draw_line(blob.major_axis_line(), color=(0,255,0)) #利用一个绿色的线,绘制穿过最小面积矩形的最长边
            #img.draw_line(blob.minor_axis_line(), color=(0,0,255)) #利用一个蓝色的线,绘制穿过最小面积矩形的最短边
        # 这些值始终是稳定的。
        img.draw_rectangle(blob.rect())      #用矩形标记出目标颜色区域
        img.draw_cross(blob.cx(), blob.cy()) #在目标颜色区域的中心画十字形标记
        # 注意- blob的旋转是唯一的0-180。
        #img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
    #print(clock.fps()) #打印帧率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风正豪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值