open mv单颜色识别例程

# 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

# 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)                 #设置为彩色
sensor.set_framesize(sensor.QVGA)					#设置图像的大小为:320x240
sensor.skip_frames(time = 2000)						#跳过100张照片,在更改设置后,跳过一些帧,等待感光元件变稳定。
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):                                                 #while循环
    clock.tick()                                             #开始追踪运行时间。
    img = sensor.snapshot()									 #拍摄一张照片,img为一个image对象
    for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):                             #for循环,在find.blogs中寻找色块面积大于200,像素大于200,并且合并blog。
        # These values depend on the blob not being circular - otherwise they will be shaky.
        if blob.elongation() > 0.5:                     #返回一个介于0和1之间的值,该值表示对象的长度(不是圆形)。一条线将是1。
            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())                              #在图像中画一个矩形框,blob.rect() 返回这个色块的外框——矩形元组(x, y, w, h),可以直接在image.draw_rectangle中使用。
        img.draw_cross(blob.cx(), blob.cy())                     #image.draw_cross(x, y, size=5, color=White) 在图像中画一个十字,blob.cx() 返回色块的外框的中心x坐标(int),也可以通过blob[5]来获取,blob.cy() 返回色块的外框的中心y坐标(int),也可以通过blob[6]来获取。
        # Note - the blob rotation is unique to 0-180 only.
        img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)                       #在图像上绘制关键点对象的关键点。您还可以传递包含(x, y, rotation_angle_in_degrees)三个值元组的列表, 以重新使用此方法来绘制关键点字形,这些关键字形是具有指向特定方向的线的圆。

 #color 是用于灰度或RGB565图像的RGB888元组。默认为白色。但是,您也可以传递灰度图像的基础像素值(0-255)或RGB565图像的字节反转RGB565值。

 #size 控制特征点的大小。

 #thickness 控制线的粗细像素。

 #将 fill 设置为True以填充特征点。

 #返回图像对象,以便您可以使用 . 表示法调用另一个方法。          
    print(clock.fps())

merge 合并,如果设置为True,那么合并所有重叠的blob为一个。若是多颜色识别,有两个框将分别框红色和绿色,merge为True则将这两个框合并为一个框。

open mv识别红颜色圆形例程:

# 圆形检测例程
#
# 这个例子展示了如何用Hough变换在图像中找到圆。
# https://en.wikipedia.org/wiki/Circle_Hough_Transform
#
# 请注意,find_circles()方法将只能找到完全在图像内部的圆。圈子之外的
# 图像/ roi被忽略...

import sensor, image, time,math
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

sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 灰度更快
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()

while(True):
    clock.tick()

    #lens_corr(1.8)畸变矫正
    img = sensor.snapshot().lens_corr(1.8)

    # Circle对象有四个值: x, y, r (半径), 和 magnitude。
    # magnitude是检测圆的强度。越高越好

    # roi 是一个用以复制的矩形的感兴趣区域(x, y, w, h)。如果未指定,
    # ROI 即图像矩形。操作范围仅限于roi区域内的像素。

    # x_stride 是霍夫变换时需要跳过的x像素的数量。若已知圆较大,可增加
    # x_stride 。

    # y_stride 是霍夫变换时需要跳过的y像素的数量。若已知直线较大,可增加
    # y_stride 。

    # threshold 控制从霍夫变换中监测到的圆。只返回大于或等于阈值的圆。
    # 应用程序的阈值正确值取决于图像。注意:一条圆的大小是组成圆所有
    # 索贝尔滤波像素大小的总和。

    # x_margin 控制所检测的圆的合并。 圆像素为 x_margin 、 y_margin 和
    # r_margin的部分合并。

    # y_margin 控制所检测的圆的合并。 圆像素为 x_margin 、 y_margin 和
    # r_margin 的部分合并。

    # r_margin 控制所检测的圆的合并。 圆像素为 x_margin 、 y_margin 和
    # r_margin 的部分合并。

    # r_min,r_max和r_step控制测试圆的半径。
    # 缩小测试圆半径的数量可以大大提升性能。
    # threshold = 3500比较合适。如果视野中检测到的圆过多,请增大阈值;
    # 相反,如果视野中检测到的圆过少,请减少阈值。
    for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,r_min = 2, r_max = 100, r_step = 2):
        for blob in img.find_blobs([thresholds[0]], pixels_threshold=200, area_threshold=200):
           img.draw_rectangle(blob.rect())
           img.draw_cross(blob.cx(), blob.cy())
           img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
           print(c)
           print(blob)

    print("FPS %f" % clock.fps())

open mv颜色和形状同时识别:

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
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()

while(True):
    clock.tick()
    img = sensor.snapshot().lens_corr(1.8)
    for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
            r_min = 2, r_max = 100, r_step = 2):
        area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())#将圆形的切矩形坐标放到area中
        #area为识别到的圆的区域,即圆的外接矩形框
        statistics = img.get_statistics(roi=area)#像素颜色统计      roi感兴趣区域为圆形的外切矩形
        print(statistics)
        #(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
        #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
        if 0<statistics.l_mode()<100 and 0<statistics.a_mode()<127 and 0<statistics.b_mode()<127:#if the circle is red
            img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来
        else:
            img.draw_rectangle(area, color = (255, 255, 255))
            #将非红色的圆用白色的矩形框出来
    print("FPS %f" % clock.fps())

statistics.l_mode():返回RGB5656 LAB 中L的众值(0-255) (int)。
statistics.a_mode():返回RGB5656 LAB 中A的众值(0-255) (int)。
statistics.b_mode():返回RGB5656 LAB 中B的众值(0-255) (int)。
LAB三个通道是什么意思?

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值