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())

你可能感兴趣的:(python,图像处理)