logo

为什么识别蓝色返回的blob.code()为4?为什么不为3?

2022-09-08 10:09
4
_2iay
# Untitled - By: ASUS - 周二 9月 6 2022

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

#*********************************************************************
def find_max(blobs):
    max_size=0
    global max_blob

    for blob in blobs:
        if blob[2]*blob[3] > max_size and 50<blob.cy()<150:
            max_blob=blob
            max_size = blob[2]*blob[3]
    return max_blob
#*********************************
def detect(max_blob):
    global mark
    #judge=0
    #nothing=0
    row_data=[-1,-1]
    print(max_blob.density())
    #mark=([-1,-1],[-1,-1]) #1 yellow 2 green 3 blue 4 red ;1 rect 2 circle 3 rt
    #shape=0 #1是圆 2是矩形 圆形和矩形直接用函数识别,剩下的判断是不是三角形 3是三角形
    #colour=0

    if  max_blob.density()>0.82:
        row_data[0]=max_blob.code()
        img.draw_rectangle(max_blob.rect())
        row_data[1]=1

    elif max_blob.density()>0.6:
        img.draw_circle((max_blob.cx(), max_blob.cy(),int((max_blob.w()+max_blob.h())/4)))
        row_data[0]=max_blob.code()
        row_data[1]=2
    elif max_blob.density()>0.4:
            img.draw_cross(max_blob.cx(), max_blob.cy())
            row_data[0]=max_blob.code()
            row_data[1]=3
    return row_data
#*********************************
def sending_data(a,b):
    global uart;
    #frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
    #data = bytearray(frame)
    data_end = ustruct.pack("<bbhhb",      #格式为俩个字符俩个短整型(2字节)
                   0x2C,                      #帧头1
                   0x12,                      #帧头2
                   int(a), # up sample by 4   #数据1
                   int(b), # up sample by 4    #数据2
                   0x5B)
    uart.write(data_end);   #必须要传入一个字节数组
#********************************************************************
red=(8, 55, 73, 10, -71, 57)
green=(63, 22, -83, -4, 50, -23)
blue=(52, 7, 40, -73, -15, -117)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

uart = UART(3, 115200)
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters

clock = time.clock()
#********************************************************************
while(True):
    clock.tick()
    img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0)#畸变矫正
    blobs = img.find_blobs([red,green,blue],area_threshold=100
    )
    data=detect(find_max(blobs))
    print(data)
    sending_data(data[0],data[1])
    #uart.write(jsonstr)
    #break
    #print(clock.fps())
2022-09-09 17:44
I
网红小智智 星瞳科技

你的阈值是红绿蓝,(0, 0, 1),code的二进制就是100(高位在前),十进制就是4。这么设计是为了识别颜色组合。