OpenMv单颜色彩色识别

这是OpenMv的自带例程,用于单颜色彩色识别
追踪小球是OpenMV用的最多的功能了

1 重点讲解

image.find_blobs(thresholds, roi=Auto, x_stride=2, y_stride=1, invert=False, area_threshold=10, pixels_threshold=10, merge=False, margin=0, threshold_cb=None, merge_cb=None)

thresholds

thresholds是颜色的阈值

注意:这个参数是一个列表,可以包含多个颜色。如果你只需要一个颜色,那么在这个列表中只需要有一个颜色值,如果你想要多个颜色阈值,那这个列表就需要多个颜色阈值。
注意:在返回的色块对象blob可以调用code方法,来判断是什么颜色的色块。

red = (xxx,xxx,xxx,xxx,xxx,xxx)
blue = (xxx,xxx,xxx,xxx,xxx,xxx)
yellow = (xxx,xxx,xxx,xxx,xxx,xxx)

img=sensor.snapshot()
red_blobs = img.find_blobs([red])

color_blobs = img.find_blobs([red,blue, yellow])

roi

**roi是“感兴趣区”。**就是你想要在画面的哪个区域进行颜色识别
eg:画面中间的矩形?整个画面?
roi的格式是(x, y, w, h)的tupple.
四个参数分别是左上角定点的(x,y)坐标,举行的长、宽
x:ROI区域中左上角的x坐标
y:ROI区域中左上角的y坐标
w:ROI的宽度
h:ROI的高度

如果不设置RIO的话就是对整个图像进行颜色识别

left_roi = [0,0,160,240]
blobs = img.find_blobs([red],roi=left_roi)

x_stride

x_stride 就是查找的色块的x方向上最小宽度的像素,默认为2,如果你只想查找宽度10个像素以上的色块,那么就设置这个参数为10:

blobs = img.find_blobs([red],x_stride=10)

y_stride

y_stride 就是查找的色块的y方向上最小宽度的像素,默认为1,如果你只想查找宽度5个像素以上的色块,那么就设置这个参数为5:

blobs = img.find_blobs([red],y_stride=5)

invert

invert 反转阈值,把阈值以外的颜色作为阈值进行查找
eg:例程中的阈值设置,就是查找红色

thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds

如果find_blobs中invert=true,则就是查找红色以外的颜色

area_threshold

area_threshold 面积阈值,如果色块被框起来的面积小于这个值,会被过滤掉
是框起来的整个面积(可能会包含其他的一些背景的像素数)

pixels_threshold

pixels_threshold 色块的像素个数(面积)阈值,如果色块像素数量小于这个值,会被过滤掉。
是指的框中的所有的色块的颜色,不包含其他(比如背景的颜色)

merge

merge 合并,如果设置为True,那么合并所有重叠的blob为一个。
注意:这会合并所有的blob,无论是什么颜色的。如果你想混淆多种颜色的blob,只需要分别调用不同颜色阈值的find_blobs。

blob

遍历一遍所有的色块blob


for blob in blobs:
    print(blob.cx())
    

blob有多个方法:

blob.rect() 返回这个色块的外框——矩形元组(x, y, w, h),可以直接在image.draw_rectangle中使用。

blob.x() 返回色块的外框的x坐标(int),也可以通过blob[0]来获取。

blob.y() 返回色块的外框的y坐标(int),也可以通过blob[1]来获取。

blob.w() 返回色块的外框的宽度w(int),也可以通过blob[2]来获取。

blob.h() 返回色块的外框的高度h(int),也可以通过blob[3]来获取。

blob.pixels() 返回色块的像素数量(int),也可以通过blob[4]来获取。

blob.cx() 返回色块的外框的中心x坐标(int),也可以通过blob[5]来获取。

blob.cy() 返回色块的外框的中心y坐标(int),也可以通过blob[6]来获取。

blob.rotation() 返回色块的旋转角度(单位为弧度)(float)。如果色块类似一个铅笔,那么这个值为0~180°。如果色块是一个圆,那么这个值是无用的。如果色块完全没有对称性,那么你会得到0~360°,也可以通过blob[7]来获取。

blob.code() 返回一个16bit数字,每一个bit会对应每一个阈值。举个例子:

blobs = img.find_blobs([red, blue, yellow], merge=True)

如果这个色块是红色,那么它的code就是0001,如果是蓝色,那么它的code就是0010。注意:一个blob可能是合并的,如果是红色和蓝色的blob,那么这个blob就是0011。这个功能可以用于查找颜色代码。也可以通过blob[8]来获取。

blob.count() 如果merge=True,那么就会有多个blob被合并到一个blob,这个函数返回的就是这个的数量。如果merge=False,那么返回值总是1。也可以通过blob[9]来获取。

blob.area() 返回色块的外框的面积。应该等于(w * h)

blob.density() 返回色块的密度。这等于色块的像素数除以外框的区域。如果密度较低,那么说明目标锁定的不是很好。
比如,识别一个红色的圆,返回的blob.pixels()是目标圆的像素点数,blob.area()是圆的外接正方形的面积。

2 源程序

为了避免注释过多,最主要的find_blobs上一部分

# Single Color RGB565 Blob Tracking Example
#
# This example shows off single color RGB565 tracking using the OpenMV Cam.

import sensor, image, time, math
#导入自带的模块

threshold_index = 0     # 0 for red, 1 for green, 2 for blue
#设置颜色的阈值,LAB的三个最大值,三个最小值
# 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
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds

sensor.reset()			#重置感光元件,重置摄像机
sensor.set_pixformat(sensor.RGB565)			#重置颜色格式为RGB56
sensor.set_framesize(sensor.QVGA)				#图像的大小是QVGA
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # 务必要关闭白平衡
sensor.set_auto_whitebal(False) # 务必要关闭自动增益
#因为打开会影响颜色识别的效果,会使颜色的阈值发生改变
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. "merge=True" merges all overlapping blobs in the image.

while(True):
    clock.tick()
    img = sensor.snapshot()			#先截取感光元件的一张图片
    #在img.find_blobs这个函数中,进行颜色识别
    for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
   #此处没有设置roi的值,便是对整个图像进行颜色识别
        # These values depend on the blob not being circular - otherwise they will be shaky.
        if blob.elongation() > 0.5:
            img.draw_edges(blob.min_corners(), color=(255,0,0))
            img.draw_line(blob.major_axis_line(), color=(0,255,0))
            img.draw_line(blob.minor_axis_line(), color=(0,0,255))
        # These values are stable all the time.
        img.draw_rectangle(blob.rect())
        #如果识别到了颜色,我们就在这个色块的周围画一个框把它圈出来
        img.draw_cross(blob.cx(), blob.cy())
        #中间画一个十字,(blob.cx(), blob.cy())是色块的中心坐标
        # Note - the blob rotation is unique to 0-180 only.
        img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
        #blob是find_blobs返回的参数
    print(clock.fps())
  • 13
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值