【星曈科技】OpenMv笔记——单颜色彩色识别

颜色识别

单颜色彩色识别

# Single Color RGB565 Blob Tracking Example单色RGB565斑点跟踪示例
#
# This example shows off single color RGB565 tracking using the OpenMV Cam.这个例子展示了使用OpenMV Cam的单色RGB565跟踪。

import sensor, image, time#导入此代码所依赖的模块

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

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
#设置所要识别的颜色的预置
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (48, 83, -128, 23, 15, 59), # generic_green_thresholds
              (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds

sensor.reset()#重置感光元件、摄像机
sensor.set_pixformat(sensor.RGB565)#设置颜色格式
sensor.set_framesize(sensor.QVGA)#设置图像大小
sensor.skip_frames(time = 2000)#跳过前几帧,等待前两个设置生效
#图像识别时必须关闭白平衡和自动增益,因为他们会影响颜色阈值
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()#返回一个时钟对象。

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are只有像素大于“pixel_threshold”和面积大于“area_threshold”的blobs才是
# 由下面的“find_blobs”返回。修改“pixels_threshold”和“area_threshold”即可
# camera resolution. "merge=True" merges all overlapping blobs in the image.相机的分辨率。“merge=True”合并图像中所有重叠的斑点。

while(True):
    clock.tick()#开始追踪运行时间。
    img = sensor.snapshot()#先截感光元件的一张照片
    #for...in循环,依次把list或tuple中的每个元素迭代出来
    #用for循环,把所有符合条件的色块找一遍
    for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
        
          
        # These values are stable all the time.这些值一直是稳定的
        img.draw_rectangle(blob.rect())#给色块画一个外框
        img.draw_cross(blob.cx(), blob.cy())#在色块外框的中心画一个+
        print(blob.cx(), blob.cy())
    print(clock.fps())#打印帧率

        

相关知识点

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
find_blobs函数
在这里插入图片描述

多颜色彩色识别

# 多颜色斑点跟踪示例
#
# 这个例子展示了使用OpenMV摄像头的多色斑点跟踪。

import sensor, image, time

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green things. You may wish to tune them...
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 15, 0, 40, -80, -20)] # generic_blue_thresholds
# 您可以超过16个以上的阈值。然而,在颜色阈值开始严重重叠之前,用16个阈值分割任何场景是不可能的。

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. Don't set "merge=True" becuase that will merge blobs which we don't want here.
#不要设置"merge=True"因为那会合并我们不想要的blobs。

while(True):
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs(thresholds,merge=False, pixels_threshold=200, area_threshold=200):
       
        # These values are stable all the time.
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        print(blob.code())
    #print(clock.fps())

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ღ 金龍戲水 ღ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值