OPENMV之识别蓝色三角形圆形矩形

星瞳科技给的方法试过了,利用线的角度和来识别,但结果误识别很严重  于是想出了 利用三角形面积占比为外接矩形的一半 再根据与三角形在相机中的周长进一步筛选 应该还可以根据三角形的边长做文章(有待完善) 圆形和矩形都要思考下边长和面积 同理可证

思路简单

调整合适的阈值

thresholds = [(0, 25, 28, 49, 41, 90),
              (14, 87, -83, 127, -113, -14),
              (30, 44, -46, -9, 7, 44)] #三个阈值更准确些

合适的面积范围

        if blob.density()>0.80:
            A=...
        elif blob.density()>0.60:
            A= ...
        elif blob.density()>0.40:
            A= ...

合理的周长范围

        if blob.w()<78:
            A= 0
        if blob.w()>100:
                A= 0

上全部代码

import sensor, image, time, math
from pyb import UART
#thresholds是指颜色阈值,把你想要检测的颜色阈值写成一个元组放进这个list
thresholds = [(0, 25, 28, 49, 41, 90),
              (14, 87, -83, 127, -113, -14),
              (30, 44, -46, -9, 7, 44)] #三个阈值更准确些
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()
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
while(True):
    clock.tick()
    img = sensor.snapshot()
    uart = UART(3,115200)
    uart.init(115200,bits=8,parity=None,stop=1)
    deflection_math= 0
    A=deflection_math
    for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200):
        if blob.density()>0.80:
            A=0
        elif blob.density()>0.60:
            A= 0
        elif blob.density()>0.40:
            A= 3
            print("检测为三角型  ",end='')
            img.draw_cross(blob.cx(), blob.cy())
            print('三角型边长',blob.w())
        else:
            A= 0
            #print("no dectedtion")
        if blob.w()<78:
            A= 0
        if blob.w()>100:
                A= 0
    uart_buf = bytearray([int (A)])
    uart.write(uart_buf)
    time.sleep(0)#延时

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然可以帮您完善OpenMV代码来检测圆形三角形矩形。以下是一个简单示例代码,可以通过颜色识别和形状匹配来实现这个功能: ```python import sensor import image import time # 初始化摄像头 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time=2000) # 定义颜色阈值 red_threshold = (30, 100, 15, 127, 15, 127) green_threshold = (30, 100, -64, -8, -32, 32) blue_threshold = (0, 30, 0, 64, -128, 0) # 定义形状识别函数 def detect_shape(c): shape = "" # 计算轮廓的周长和面积 perimeter = c.perimeter() area = c.area() # 计算圆度 circularity = 4 * 3.1415926 * area / (perimeter * perimeter) # 判断形状 if circularity > 0.85: shape = "圆形" elif 2.5 < c.aspect_ratio() < 3.5: shape = "矩形" elif 0.5 < c.aspect_ratio() < 2.0: shape = "三角形" return shape # 主循环 while True: img = sensor.snapshot() # 检测红色物体 red_blobs = img.find_blobs([red_threshold], pixels_threshold=200, area_threshold=200) if red_blobs: for b in red_blobs: # 绘制外接矩形 img.draw_rectangle(b.rect(), color=(255, 0, 0)) # 绘制形状名称 img.draw_string(b.x(), b.y(), detect_shape(b), color=(255, 0, 0)) # 检测绿色物体 green_blobs = img.find_blobs([green_threshold], pixels_threshold=200, area_threshold=200) if green_blobs: for b in green_blobs: # 绘制外接矩形 img.draw_rectangle(b.rect(), color=(0, 255, 0)) # 绘制形状名称 img.draw_string(b.x(), b.y(), detect_shape(b), color=(0, 255, 0)) # 检测蓝色物体 blue_blobs = img.find_blobs([blue_threshold], pixels_threshold=200, area_threshold=200) if blue_blobs: for b in blue_blobs: # 绘制外接矩形 img.draw_rectangle(b.rect(), color=(0, 0, 255)) # 绘制形状名称 img.draw_string(b.x(), b.y(), detect_shape(b), color=(0, 0, 255)) # 显示图像 img.show() ``` 您可以根据需要调整颜色阈值和形状识别的参数来适应您的实际场景。请确保在使用之前已经安装了OpenMV的开发环境,并且连接了OpenMV摄像头。希望能对您有所帮助!如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

李雨婵

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

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

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

打赏作者

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

抵扣说明:

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

余额充值