Openmv实现图像中最大色块查找

目录

前言

一、前期准备

二、使用步骤

1.引入库

2.初始化

 3.元件初始化

4.色块分类


前言

本例程实现对图像中红 黄 绿三种不同颜色色块的分类


一、前期准备

openmv IDE安装及熟悉,详细可以参考

https://book.openmv.cc/

二、使用步骤

1.引入库

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

2.初始化

red = LED(1) # 红led
green=LED(2)

uart = UART(3, 115200)

uart.init(115200, bits=8, parity=None, stop=1)  #8位数据位,无校验位,1位停止位、

#红色阈值
pink_threshold =(44, 75, 8, 77, -44, 21)
# 黄色阈值
yellow_threshold = (36, 75, -20, 11, 23, 48)
# 蓝色阈值
blue_threshold = (61, 95, -23, -10, -30, -10)
# 绿色阈值
green_threshold =(50, 60, -48, -30, 15, 38)
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green things. You may wish to tune them...
thresholds = [pink_threshold, # generic_red_thresholds -> index is 0 so code == (1 << 0)
              yellow_threshold, # generic_green_thresholds -> index is 1 so code == (1 << 1)
              green_threshold] # generic_blue_thresholds -> index is 2 so code == (1 << 2)
# Codes are or'ed together when "merge=True" for "find_blobs".

设置颜色分类阈值,初始化openmv的串口以及三色的led灯光,识别到上述三种颜色则亮对应的  

 3.元件初始化

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
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()

4.色块分类

 

while(True):
    #clock.tick()
    time.sleep_ms(10)
    img = sensor.snapshot()
    for blob in img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100, merge=True):
        s1=0
        if(blob.area()>s1):
            s1=blob.area()
            aimblob=blob
    if aimblob.code() == 1:
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        img.draw_string(blob.x() + 2, blob.y() + 2, "r/g")
        red.on()
        uart.write("red")
        time.sleep_ms(300)
        print("识别到红色!")
        red.off()
    if aimblob.code() == 2:
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        img.draw_string(blob.x() + 2, blob.y() + 2, "r/b")
        red.on()
        green.on()
        uart.write("yellow")
        time.sleep_ms(300)
        print("识别到黄色!")
        red.off()
        green.off()
    if aimblob.code() == 4: # g/b code
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        img.draw_string(blob.x() + 2, blob.y() + 2, "g/b")
        green.on()
        uart.write("green")
        time.sleep_ms(300)
        print("识别到绿色!")
        green.off()
       #print(clock.fps())

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿古的童谣

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

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

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

打赏作者

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

抵扣说明:

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

余额充值