记录一次openMv色块查找

该文章描述了一位参赛者在使用openMV进行颜色目标识别时遇到的问题,主要涉及识别红色矩形和排除蓝色矩形的过程。作者已经成功识别出圆形,但在识别红色矩形时,蓝色矩形也会被误识别。代码示例展示了如何查找红色和蓝色矩形以及内部的黄色圆形,同时存在对特定颜色识别的调整和优化需求。
摘要由CSDN通过智能技术生成

参加了一个比赛,最近在弄openMv识别目标色块部分,主要特定颜色的矩形,而矩形里有一个特定颜色的圆或者三角形。经过调试,圆是最好识别出来的,但问题出在矩形,我想查找红色的矩形,但识别蓝色的矩形它也同样进入程序进行识别。

import sensor
import image
import time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_auto_gain(False) # 关闭自动自动增益。默认开启的。
sensor.set_auto_whitebal(False) #关闭白平衡。在颜色识别中,一定要关闭白平衡
clock = time.clock()

blue_threshold = [(0, 100, -128, -20, 0, 50)]
yellow_threshold = [(70, 90, -10, 127, 0, 50)]
min_rect_width = 5  # 矩形最小宽度
min_rect_height = 5  # 矩形最小高度


def find_red_rectangles(img):
    for blob in img.find_blobs([(100, 0, 0, 50, 0, 0)], pixels_threshold=200, area_threshold=1000,
                               merge=True):  # 检测红色颜色的斑块
        if blob.is_rectangle():  # 检测斑块是否为矩形
            img.draw_rectangle(blob.rect(), color=(255, 0, 0), thickness=2)  # 绘制检测到的矩形
            print("检测到红色矩形")



while True:
    clock.tick()
    img = sensor.snapshot()
    min_rect_area = 1000  # 设置最小矩形面积
    find_red_rectangles(img)

    # 查找蓝色矩形
    blue_rects = img.find_rects(thresholds=blue_threshold, min_width=min_rect_width,min_height=min_rect_height)


    if blue_rects:
        #print("bbbbbbbbb")

        for rect in blue_rects:
            x, y, w, h = rect.rect()

            # 在矩形区域内查找黄色圆形
            roi = img.crop((x, y, w, h))
            img.draw_rectangle(rect.rect(), color=(255, 0, 0), thickness=2)  # 绘制检测到的矩形
            yellow_circles = roi.find_circles(
                thresholds=yellow_threshold, x_margin=10, y_margin=10, r_margin=10,
                r_min=10, r_max=30, robust=True)

            if not yellow_circles:
                 print("没有识别到黄色圆形")
                 continue

            else:# 绘制识别结果
                print("yyyyyy")
                for c in yellow_circles:
                    roi.draw_circle(c.x(), c.y(), c.r(), color=(255, 255, 0))  # 绘制黄色圆形






    # 输出图像
    #img.draw_string(0, 0, "Blue Rect and Yellow Circle", color=(0, 255, 255), scale=2)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值