识别颜色和框选目标不准确
-
import sensor, image, time from pyb import UART import ustruct #sensor.set_auto_exposure(False) state="now" last_state="once" flag=0 code=0 green_threshold = (30, 71, -42, 5, -17, 28) #绿色阈值 yellow_threshold = (66, 100, -30, 19, -6, 50) #黄色阈值 thresholds = [yellow_threshold, green_threshold] 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() uart = UART(3,9600) #定义串口3变量 uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters def find_max(blobs, min_size=0): if not blobs: # 处理空列表情况 return None max_blob = None max_size = 10000# 初始化为最小像素数量 for blob in blobs: size = blob.pixels() # 获取像素数量 if size > max_size: # 增加对最小像素数量的判断 max_blob = blob max_size = size return max_blob def send_data_packet(code): print(f"已发送类型{code}") uart.write(str(code))#串口发送 while(True): img = sensor.snapshot() blobs = img.find_blobs(thresholds); cx=0;cy=0; if blobs: max_b = find_max(blobs) if max_b: img.draw_rectangle(max_b[0:4]) #用矩形标记出目标颜色区域 img.draw_cross(max_b[5], max_b[6]) #在目标颜色区域的中心画十字形标记 cx=max_b[5]; cy=max_b[6]; cw=max_b[2]; ch=max_b[3]; code=max_b[8]; state=str(code) if last_state!=state: send_data_packet(code) last_state=state else: istate="now" last_state="once"
-
应该是颜色阈值没选好。