import sensor, image, time , math , pyb
from pyb import UART
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
a1=0
uart = UART(3, 115200)
uart.init(115200, bits=8, parity=None, stop=1)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob = blob
max_size = blob[2]*blob[3]
return max_blob
red_threshold_01 = ((82, 36, 63, 23, 63, 28));
white_threshold_01 = ((75, 100, -18, 2, -4, 23));
pink_threshold_01 = ((48, 100, 5, 127, -25, 12));
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1.8)
blobs = img.find_blobs([red_threshold_01], pixels_threshold=150, area_threshold=500, merge=True, margin=10);
if blobs:
#如果找到了目标颜色
print("ored")
max_blob_0 = find_max(blobs)
for b in blobs:
x = b[0]
y = b[1]
width = b[2]
height = b[3]
img.draw_rectangle(max_blob_0.rect())
#用矩形标记出目标颜色区域
img.draw_cross(max_blob_0.cx(), max_blob_0.cy())
#在目标颜色区域的中心画十字形标记
print(b[5],b[6],b[7])
X =int(max_blob_0.cx()-img.width()/2)
Y =int(max_blob_0.cy()-img.height()/2)
blobs = img.find_blobs([white_threshold_01], pixels_threshold=100, area_threshold=200, merge=True, margin=10);
if blobs:
print("white")
max_blob_1 = find_max(blobs)
img.draw_rectangle(max_blob_1.rect())
#用矩形标记出目标颜色区域
img.draw_cross(max_blob_1.cx(), max_blob_1.cy())
print("cx: ", max_blob_1.cx()) #打印X的的位置
print("cy: ", max_blob_1.cy())
blobs = img.find_blobs([pink_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10);
if blobs:
max_blob_2 = find_max(blobs)
img.draw_rectangle(max_blob_2.rect(),color = (255, 0, 0))
#用矩形标记出目标颜色区域
img.draw_cross(max_blob_2.cx(), max_blob_2.cy())
print("pink cx: cy: ", max_blob_2.cx(),max_blob_2.cy()) #打印X的的位置
那说明白色阈值没调好。