OPENMV的形状和颜色组合识别

 使用openmv,通过阈值颜色和形状来去真假宝藏。调试过程发现颜色的阈值比较重要,因为不准的话,它会把一些颜色相近的物体也识别了。识别的精度有待提高,可以使用YOLOV5来精确识别,奈何本人没精力来弄这个。

打开机器视觉的阈值调节器 

使用方法: 阈值调节主要是把你想要的颜色调为白色,其它颜色为黑色。将调节好的六个参数复制至代码那里。

 共六个参数,从上到下按顺序,L、A、B的最小值不超过其最大值,但也看情况。

 阈值没调好的效果。

(openmv拍宿舍,是谁放假了还在学校参加比赛 ^@^ ,大冤种就是我)

 

 附一段杂的代码,仅供参考:

import sensor, image, time

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
sensor.set_auto_gain(False)  # 关闭自动增益
sensor.set_auto_whitebal(False)  # 关闭自动白平衡
clock = time.clock()

fs_cnt = 0

x_list = []
y_list = []
len_list = []
area_list = []
color_list = []

x_location = 0
y_location = 0

shape = 0     # 0没检测到1圆形2长方形3三角形
color = 0     # 0没检测到1圆形2长方形3三角形

sidelen = 0   # 单位cm
area = 0

xt = 0

red_threshold = [(27,54,-35,59,3,68)]      #红色阈值
blue_threshold = [(37, 74, -80, 6, -100, -5)]   #浅蓝色阈值
yellow_threshold = [(38, 86, -40, 83, 27, 118)] #黄色阈值
green_threshold  = [(15, 52, -88, -12, -122, 44)]  #绿色阈值


while True:
    clock.tick()
    fs_cnt = fs_cnt + 1
    img = sensor.snapshot().lens_corr(1.8)
    #img.bilateral(1)

    #矩形识别
    for d in img.find_rects(threshold=10000):
        area = (d[0], d[1], d[2], d[3])
        statistics1 = img.get_statistics(roi=area)  # 像素颜色统计
        xq = 0
        xq = d[2] / d[3]
        coners = d.corners()
        sb = ((abs(coners[0][0] - coners[1][0]))**2 + (abs(coners[0][1]-coners[1][1])**2))**0.5
        #识别红色矩形(3, 59, -82, 108, -122, 44)
        if 3<statistics1.l_mode()<59 and -82<statistics1.a_mode()<108 and -122<statistics1.b_mode()<44 and 0.8<xq<1.2:
            #color = 1
            x_location = int(d[0] + d[2] / 2)
            y_location = int(d[1] + d[3] / 2)

            img.draw_rectangle(d.rect(), color=(0, 255, 0))
            img.draw_cross(x_location, y_location, color=(0,255,0)) #绿色标识
            print("识别红色矩形")

        #识别三角形
        for g in img.find_blobs(green_threshold):
            Solidty = g.solidity()
            Roundness = g.roundness()
            pixels = g.pixels()
           # abs(width - height) <= 10:
            xt = g.w()/g.h()
            if  0.1<= Solidty <=0.815 and 0.9< xt <1.1 and Roundness < 0.9405 and 250 < pixels < 3000:
                x_location = int(g.x() + 0.5 * g.w())
                y_location = int(g.y() + 0.5 * g.h())
                img.draw_rectangle(g.x(), g.y(), g.w(), g.h(), color=(255, 0, 0))
                img.draw_string(g.x() + 2, g.y() + 2, "green_rantangle")
                #img.draw_cross(x_location, y_location, color=(0,255,0))
                print("识别绿色三角形")







     #识别蓝色矩形(35, 73, -49, -9, -34, 62)
     #if 35<statistics1.l_mode()<73 and -49<statistics1.a_mode()<-9 and -34<statistics1.b_mode()<62 and 0.8<xq<1.2:
     #       x_location = int(d[0] + d[2] / 2)
      #      y_location = int(d[1] + d[3] / 2)
      #      img.draw_rectangle(d.rect(), color=(255, 255, 0))
      #      img.draw_cross(x_location, y_location, color=(255,255,0))  #黄色标识
      #      print("识别蓝色矩形")

    #识别圆形
    for c in img.find_circles(threshold=2000, x_margin=100, y_margin=10, r_margin=10,
                          r_min=10, r_max=200, r_step=2):
        area = (c.x() - c.r(), c.y() - c.r(), 2 * c.r(), 2 * c.r())
        statistics = img.get_statistics(roi=area)
        #识别黄色圆
        if 38<statistics.l_mode()<86 and -40<statistics.a_mode()<83 and 27<statistics.b_mode()<118:
            x_location = c.x()
            y_location = c.y()
            img.draw_cross(c.x(), c.y(), size=5, color=(255,0,255))
            img.draw_circle(c.x(), c.y(), c.r(), color=(255, 0, 255))
            print("识别黄色圆")

  • 3
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于OpenMV来说,形状颜色识别是一项常见的任务。OpenMV是一个基于Python的嵌入式计算机视觉平台,它可以通过连接摄像头模块进行图像处理和分析。 要实现形状颜色识别,您可以按照以下步骤进行操作: 1. 初始化摄像头模块:在OpenMV上运行代码之前,您需要初始化摄像头模块,并设置适当的分辨率和图像处理参数。 2. 捕捉图像:使用摄像头模块捕捉实时图像。您可以使用`sensor.snapshot()`方法来获取当前帧的图像。 3. 颜色识别:通过在图像中使用颜色过滤器来识别特定颜色的对象。您可以使用`img.find_blobs()`函数来检测图像中的颜色区域,并获取其位置和大小信息。 4. 形状识别:一旦您识别出特定颜色的对象,您可以使用形状检测算法来辨别它们的形状OpenMV提供了`img.find_circles()`和`img.find_rects()`等函数来检测圆形和矩形对象。 5. 输出结果:根据您的需求,您可以在图像上绘制边界框或其他标记来显示识别结果。您还可以将结果通过串口、无线通信或其他方式发送给外部设备。 需要注意的是,颜色形状识别的准确性取决于摄像头模块的分辨率、光照条件和对象的特征等因素。您可能需要根据实际情况进行参数调整和算法优化。 以上是基本的步骤,您可以根据具体需求和OpenMV的API文档进一步深入研究和实践。希望对您有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值