实现效果

        识别出三种颜色,并输出坐标信息。

find_blobs函数

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为颜色的阈值,通过调用列表中的颜色阈值,来判断是什么颜色的色块。

·roi

        roi为“感兴趣区”,即在图像的那个方块进行识别。不设置则在整个图像中识别。

         例:left_roi = [0,0,160,240]#在原点坐标(0,0),长为160,宽为240的的长方形中寻找
                blobs = img.find_blobs([red],roi=left_roi)

·x_stride

        x_stride为查找的色块的x方向上最小宽度的像素,默认为2。

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

·y_stride

        y_stride为查找的色块的x方向上最小宽度的像素,默认为2。

       例:blobs = img.find_blobs([red],y_stride=10)

·invert

        invert为反转阈值,把阈值以外的颜色作为阈值进行查找

·area_threshold      

        area_threshold为方框面积阈值,如果色块面积阈值小于这个值,会被过滤掉

        例:area_threshold=50

·pixels_threshold        

        pixels_threshold 为像素个数阈值,如果色块像素个数小于这个值,会被过滤掉

         例:pixels_threshold=50

·merge

        merge 为合并,如果设置为True,多颜色识别时所有被识别到的颜色被框在一个大框内。

          例: merge=True

阈值编辑器

        首先运行hello world.py,显示目标图案。

        工具——机器视觉——阈值编辑器

        点击帧缓冲区

        滑动六个滑块,可以实时的看到阈值的结果,目标颜色为白色,其他颜色全为黑色

 实现代码

/ *
  * 文件名:multi_color_blob_tracking_1.py
  * 作者:阿豪不掉发
  * 描述:识别出三种颜色,并输出坐标信息
    */
import sensor, image, time, math

#颜色阈值可以通过(工具——机器视觉——阈值编辑器)调整
thresholds = [(35, 67, 26, 62, -86, 103), # 红色阈值
              (58, 100, -43, 127, 22, 127), #黄色阈值
              (72, 46, -83, -43, 39, 127)] # 绿色阈值

sensor.reset()#重置摄像机
sensor.set_pixformat(sensor.RGB565)#颜色格式为rgb565
sensor.set_framesize(sensor.QVGA)#图像大小QAGA
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) #关闭自动增益
sensor.set_auto_whitebal(False) #关闭白平衡
clock = time.clock()



while(True):
    clock.tick()
    img = sensor.snapshot()#截取一张图像
    for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):       
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        print(blob)#输出坐标信息
    print(clock.fps())

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐