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

2024-03-09 22:10

本文主要是介绍OpenMV4 基于色块识别的图形+颜色+坐标识别代码(micropython),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

# Untitled - By: zzy - 周五 11月 25 2022import sensor, image, time
from pyb import UART
import jsonoutput_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=0for blob in blobs:if blob.pixels() > max_size:max_blob=blobmax_size = blob.pixels()return max_blobdef detect(max_blob):#输入的是寻找到色块中的最大色块#print(max_blob.solidity())shape=0if max_blob.solidity()>0.90 or max_blob.density()>0.84:img.draw_rectangle(max_blob.rect(),color=(255,255,255))shape=1elif max_blob.density()>0.6:img.draw_circle((max_blob.cx(), max_blob.cy(),int((max_blob.w()+max_blob.h())/4)))shape=2elif max_blob.density()>0.4:img.draw_rectangle(max_blob.rect(),color=(0,0,0))shape=3return shapewhile(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) #方式1print('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) #方式1print('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) #方式1print('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) #方式1print('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) #方式1print('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())

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

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

这篇关于OpenMV4 基于色块识别的图形+颜色+坐标识别代码(micropython)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/792095

相关文章

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时