OpenMV4 基于色块识别的图形+颜色+坐标识别代码(micropython)

Hello大家好,最近竞赛需要开始研究OpenMV4,今天和大家分享一段基于色块识别的图形+颜色+坐标识别代码,实测准确率高于90%哦,当然,需要在光线和距离都合适的情况下使用(假如你的识别结果不尽如人意,可以自行调节颜色阈值和目标与摄像头的距离),下面,话不多说,上代码!(需要搭配OpenMV IDE使用)

# Untitled - By: zzy - 周五 11月 25 2022

import sensor, image, time
from pyb import UART
import json

output_str_green="[0,0]"
output_str_red="[0,0]"
output_str_blue="[0,0]"
output_str_brown="[0,0]"
output_str_yellow="[0,0]"


#green_threshold  = (   0,   80,  -70,   -10,   -0,   30)
green_threshold  = (   3,   39,  -29,   2,   1,   25)
red_threshold    = (   28,   40,  51,   65,   22,   50)
orange_threshold = (   23,   39,  19,   42,   13,   31)
blue_threshold  = (   50,   56,  -14,   1,   -31,   -13)
brown_threshold  = (   22,   30,  1,   17,   8,   25)
yellow_threshold  = (   53,   58,  -7,   3,   58,   63)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((0,20,320,200))#QVGA find Region Of Interest
#sensor.set_windowing((5,10,160,95))#QQVGA find Region Of Interest
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()

uart = UART(3, 115200)
def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob.pixels() > max_size:
            max_blob=blob
            max_size = blob.pixels()
    return max_blob

def detect(max_blob):#输入的是寻找到色块中的最大色块
    #print(max_blob.solidity())
    shape=0
    if max_blob.solidity()>0.90 or max_blob.density()>0.84:
        img.draw_rectangle(max_blob.rect(),color=(255,255,255))
        shape=1

    elif max_blob.density()>0.6:
        img.draw_circle((max_blob.cx(), max_blob.cy(),int((max_blob.w()+max_blob.h())/4)))
        shape=2

    elif max_blob.density()>0.4:
        img.draw_rectangle(max_blob.rect(),color=(0,0,0))
        shape=3

    return shape

while(True):
    #clock.tick()
    img = sensor.snapshot() # Take a picture and return the image.
    blobs_green = img.find_blobs([green_threshold])
    blobs_red = img.find_blobs([red_threshold])
    #blobs_orange = img.find_blobs([orange_threshold])
    blobs_blue = img.find_blobs([blue_threshold])
    blobs_brown = img.find_blobs([brown_threshold])
    blobs_yellow = img.find_blobs([yellow_threshold])

    if blobs_green:
        max_blob_green=find_max(blobs_green)
        shape_green=detect(max_blob_green)
        #img.draw_rectangle(max_blob_green.rect(),color=(0,255,0))#画框
        img.draw_cross(max_blob_green.cx(), max_blob_green.cy(),color=(0,255,0))#画十字准星
        output_str_green="[%d,%d,%d]" % (max_blob_green.cx(),max_blob_green.cy(),shape_green) #方式1
        print('green:',output_str_green)

    else:
        print('not found green!')


    if blobs_red:
        max_blob_red=find_max(blobs_red)
        shape_red=detect(max_blob_red)
        #img.draw_rectangle(max_blob_red.rect(),color=(255,0,0))
        img.draw_cross(max_blob_red.cx(), max_blob_red.cy(),color=(255,0,0))
        output_str_red="[%d,%d,%d]" % (max_blob_red.cx(),max_blob_red.cy(),shape_red) #方式1
        print('red:',output_str_red)

    else:
        print('not found red !')


    #if blobs_orange:
        #max_blob_orange=find_max(blobs_orange)
        #detect(max_blob_orange)
        ##img.draw_rectangle(max_blob_orange.rect(),color=(255,128,0))
        #img.draw_cross(max_blob_orange.cx(), max_blob_orange.cy(),color=(255,128,0))
        #output_str_orange="[%d,%d]" % (max_blob_orange.cx(),max_blob_orange.cy()) #方式1
        #print('orange:',output_str_orange)
        #uart.write(output_str_orange+'\r\n')
    #else:
        #print('not found orange !')


    if blobs_blue:
        max_blob_blue=find_max(blobs_blue)
        shape_blue=detect(max_blob_blue)
        #img.draw_rectangle(max_blob_blue.rect(),color=(0,0,255))
        img.draw_cross(max_blob_blue.cx(), max_blob_blue.cy(),color=(0,0,255))
        output_str_blue="[%d,%d,%d]" % (max_blob_blue.cx(),max_blob_blue.cy(),shape_blue) #方式1
        print('blue:',output_str_blue)
    else:
        print('not found blue !')


    if blobs_brown:
        max_blob_brown=find_max(blobs_brown)
        shape_brown=detect(max_blob_brown)
        #img.draw_rectangle(max_blob_brown.rect(),color=(205,133,63))
        img.draw_cross(max_blob_brown.cx(), max_blob_brown.cy(),color=(205,133,63))
        output_str_brown="[%d,%d,%d]" % (max_blob_brown.cx(),max_blob_brown.cy(),shape_brown) #方式1
        print('brown:',output_str_brown)
    else:
        print('not found brown !')

    if blobs_yellow:
        max_blob_yellow=find_max(blobs_yellow)
        shape_yellow=detect(max_blob_yellow)
        #img.draw_rectangle(max_blob_yellow.rect(),color=(255,255,0))
        img.draw_cross(max_blob_yellow.cx(), max_blob_yellow.cy(),color=(255,255,0))
        output_str_yellow="[%d,%d,%d]" % (max_blob_yellow.cx(),max_blob_yellow.cy(),shape_yellow) #方式1
        print('yellow:',output_str_yellow)
    else:
        print('not found yellow !')


    uart.write(output_str_green + output_str_red + output_str_blue + output_str_brown + output_str_yellow + '\r\n')

    #print(clock.fps())

再来看看程序的运行结果吧 ,在识别出多个图形,颜色及坐标之后,性能仍然不赖

实测运行结果,在颜色阈值选择正确情况下识别率还是比较高的哦(三角形识别出之后画黑色矩形框,不是没识别出来哦)
解除部分注释之后可以查看帧数以调整性能,开发者还可以根据自己的需求增加被检测颜色与图形,再将其通过串口发送到目标单片机上哦,假如之后有时间,我再出一份解释代码含义的文章,嘻嘻,就看大家的需求和小编的时间啦。

  • 27
    点赞
  • 234
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
### 回答1: OpenMV是一个基于MicroPython的开源微控制器平台,能够实现机器视觉和机器学习的应用。在OpenMV中,红蓝色块识别坐标获取功能是非常常见的一个功能。 首先,要实现红蓝色块识别坐标获取,我们需要使用OpenMV的图像传感器来采集场景中的图像,并通过OpenMV的图像处理功能对图像进行处理,提取场景中的红蓝色块的信息。OpenMV的图像处理功能可以使用Python语言写出相关脚本来实现,开发者可以根据自己的需要进行调整和修改。 其次,我们需要确定红蓝色块颜色范围。在OpenMV中,采用HSV颜色模型,来确定颜色范围。然后在程序中,我们可以设置红蓝色块颜色范围,然后使用OpenMV的相关函数进行图像处理和识别。 最后,我们可以通过OpenMV坐标获取函数获取到红蓝色块坐标信息,进而进行下一步的操作,例如,通过串口通信把坐标数据发送到其他设备上进行处理,或者在机器人中添加相应的控制算法,来实现机器人对于红蓝色块的跟踪等功能。 ### 回答2: OpenMV是一款小巧而强大的嵌入式硬件设备,可用于计算机视觉领域的各种应用,如红蓝色块识别坐标OpenMV可以使用机器学习算法识别不同颜色的物体,因此可以用来探索各种计算机视觉应用,例如机器人导航、智能安保系统等。 当OpenMV扫描一个场景中的物体时,它能够检测到其中的红蓝色块,并精确测量出它们的位置,大小和形状。在检测过程中,使用的是一种称为颜色追踪的算法。该算法将物体的颜色与设置颜色阈值的限制相比较,如果颜色匹配,则标记该块并提供其坐标。 要开发一个红蓝色块检测程序,首先需要在OpenMV软件中设置颜色阈值。默认情况下,OpenMV将红色和蓝色视为需要监测的颜色。如果想要增加或删除其他颜色,则可以通过在固件中进行设置来完成。 一旦确定了需要追踪的颜色OpenMV就会开始扫描场景并检测对象。在检测到物体后,OpenMV会计算出物体的位置,并将其坐标提供给用户。这些坐标通常来自场景中物体的中心,因此可以用来确定物体的位置和朝向。 总之,OpenMV可以轻松地识别和测量红蓝色块,并提供它们的坐标。这种能力使OpenMV成为许多计算机视觉应用程序的理想选择,例如机器人控制、自动识别等等。 ### 回答3: OpenMV是一款基于Python的低成本、低功耗的智能摄像头,可以实现多种图像识别和计算机视觉应用。其中,OpenMV可以识别简单的红蓝色块,给出坐标。 对于红蓝色块识别OpenMV可以通过颜色阈值确定图像上红色和蓝色的像素范围,并通过像素面积进行筛选。在输入图像中找到符合条件的最大像素区域,并计算出中心坐标,然后输出该坐标作为识别结果。 在OpenMV的应用中,坐标的输出类似于图形界面上的鼠标光标,以像素值为单位,可以直观地获取当前颜色块在图像中的位置信息。同时,在OpenMV的图像处理库中也提供了一些坐标变换和图像缩放等函数,可以方便快捷地处理不同图像大小和坐标系下的数据。 总而言之,OpenMV通过颜色阈值、像素筛选和坐标输出的方法,实现了对红蓝色块的简单识别和位置获取,提高了人工智能应用在物联网和嵌入式系统上的普及性和可行性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值