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 = ((20, 58, 49, 127, -128, 127));
white_threshold_01 = ((75, 100, 96, -24, 31, -7));
pink_threshold_01 = ((30, 100, 5, 73, -42, 32));
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1.8)
blobs = img.find_blobs([red_threshold_01], roi=[0,0,200,200],pixels_threshold=150, area_threshold=500, merge=True, margin=10);
blobs1 = img.find_blobs([white_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10);
if blobs:
#如果找到了目标颜色
print("ored")
for b in blobs:
#迭代找到的目标颜色区
x = b[0]
y = b[1]
width = b[2]
height = b[3]
# Draw a rect around the blob.
img.draw_rectangle([x,y,width,height]) # rec
img.draw_cross(b[5], b[6]) # cx, cy
#在目标颜色区域的中心画十字形标记
print(b[5],b[6],b[7])
X =int(b[5]-img.width()/2)
Y =int(b[6]-img.height()/2)
if 9<X<100:
datax =("ao"+str(X))
print("X坐标: ",datax)
elif 0<X<10:
datax =("aoo"+str(X))
print("X坐标: ",datax)
elif X==0:
datax =("aooo")
print("X坐标: ",datax)
elif X>99:
datax =("a"+str(X))
print("X坐标: ",datax)
elif -100<X<-9:
datax =("b"+str(X))
print("X坐标: ",datax)
elif -10<X<0:
datax =("boo"+str(X))
print("X坐标: ",datax)
elif X<-99:
datax =("b"+str(X))
print("X坐标: ",datax)
if 9<Y<100:
datay =("ao"+str(Y))
print("Y坐标: ",datay)
elif 0<Y<10:
datay =("aoo"+str(Y))
print("Y坐标: ",datay)
elif Y==0:
datay =("aooo")
print("Y坐标: ",datay)
elif Y>99:
datay =("a"+str(Y))
print("Y坐标: ",datay)
elif -100<Y<-9:
datay =("b"+str(Y))
print("Y坐标: ",datay)
elif -10<Y<0:
datay =("boo"+str(Y))
print("Y坐标: ",datay)
elif Y<-99:
datay =("b"+str(Y))
print("Y坐标: ",datay)
if(a1==60):
a1=0
uart.write(datax+datay+"\r\n")
print("X Y 坐标 : ",datax,datax)
else:
a1=a1+1
for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2):
area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
statistics = img.get_statistics(roi=area)
#像素颜色统计
if 75<statistics.l_mode()<100 and -27<statistics.a_mode()<96 and -7<statistics.b_mode()<31:
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
#识别到的白色圆形用红色的圆框出来
print("白",c.x(),c.y())
for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2):
area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
statistics = img.get_statistics(roi=area)
#像素颜色统计
print(statistics)
if 30<statistics.l_mode()<100 and 5<statistics.a_mode()<73 and -42<statistics.b_mode()<32:
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
#识别到的粉色圆形用红色的圆框出来
print("粉",c.x(),c.y())find_circles函数比较慢,你应该把103行,和115行,合并在一起。
for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2):
area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
statistics = img.get_statistics(roi=area)
#像素颜色统计
if 75<statistics.l_mode()<100 and -27<statistics.a_mode()<96 and -7<statistics.b_mode()<31:
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
#识别到的白色圆形用红色的圆框出来
print("白",c.x(),c.y())
if 30<statistics.l_mode()<100 and 5<statistics.a_mode()<73 and -42<statistics.b_mode()<32:
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
#识别到的粉色圆形用红色的圆框出来
print("粉",c.x(),c.y())