logo

怎么实现将一张图片上的色块信息(三种颜色,每种颜色若干),按照由上到下的顺序分别输出

2022-04-26 21:42
O
uwcd
f804bdad-b6f1-43db-b623-8060d8dbbea2.png

【这是识别对象】

我现在的代码如下(不能一起且按顺序输出):import sensor, image, time

from pyb import UART

import json #载入库

thresholds = [(65, 70, -59, -47, 11, 35),

(27,33,33,44,-75,-64),

(34,41,49,60,1,22)] #1*绿色 2*蓝色 3*红色

sensor.reset()

sensor.set_pixformat(sensor.RGB565) #设置图像格式为 LAB 三通道图像

sensor.set_framesize(sensor.QVGA) #设置图像分辨率为 320*240

sensor.skip_frames(time = 5000) #等待 2s 感光元件稳定后执行后续任务

sensor.set_auto_gain(False) #关闭自动增益

sensor.set_auto_whitebal(False) #关闭白平衡

clock = time.clock() #初始化时钟

uart = UART(3, 9600) #初始化串口 3,波特率为 9600

#******************寻找最大的目标像素块函数*******************#

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

#******************寻找目标颜色块程序*******************#

while(True):

img = sensor.snapshot() #获取图像存至 img 变量

#sensor.set_vflip(True) #开启镜头反向

GREEN_blobs = img.find_blobs([thresholds[0]], pixels_threshold=15, area_threshold=15, merge=False)

#识别LVSEI

BLUE_blobs = img.find_blobs([thresholds[1]], pixels_threshold=15, area_threshold=15, merge=False)

#识别LANSEI

RED_blobs = img.find_blobs([thresholds[2]], pixels_threshold=15, area_threshold=15, merge=False)

#识别HONGSEI

print("干旱信息如下")

if GREEN_blobs:

max_blob=find_max(GREEN_blobs) #查找色块中最大的一个

img.draw_rectangle(max_blob.rect()) #用矩形框框出最大的色块

img.draw_cross(max_blob.cx(), max_blob.cy()) #十字标记最大色块中心

print("轻微干旱")

output_target="1"

print( output_target)

uart.write(output_target) #通过串口发送数据

elif BLUE_blobs:

max_blob=find_max(BLUE_blobs)

img.draw_rectangle(max_blob.rect())

img.draw_cross(max_blob.cx(), max_blob.cy())

print("一般干旱")

output_target="2"

print( output_target)

uart.write(output_target)

elif RED_blobs:

max_blob=find_max(RED_blobs)

img.draw_rectangle(max_blob.rect())

img.draw_cross(max_blob.cx(), max_blob.cy())

print("严重干旱")

output_target="3"

print( output_target)

uart.write(output_target)

else:

print('not found!')

output_target="0"

print( output_target)

uart.write(output_target)

2022-04-26 22:29
I
网红小智智 星瞳科技

把所有的色块放到一起,然后通过y坐标排序就行了。