关于圆形色块识别通过串口发送不能输出的问题
-
事情是这样的,我想写用 圆形的小球(指的是颜色形状同时识别),结合串口发送与外部设备通信,写好程序,运行不报错,也能识别圆形色块或小球,如果能识别到小球色块他能输出坐标,偶尔输出not found!。。。但是如果什么都没有识别到的时候它却不输出 not found! ,我不明白,为什么她不能输出not found! 我的代码是根据如下两个视频修改的。
视频教程18 - 算法的组合使用:https://singtown.com/learn/50029/
视频教程27 - 串口通信发送数据:https://singtown.com/learn/50235/最后附上我写的代码,请求大佬指点指正,谢谢!
from pyb import UART import json # For color tracking to work really well you should ideally be in a very, very, # very, controlled enviroment where the lighting is constant... #yellow_threshold = (63, 79, 24, 66, 70, 32) # You may need to tweak the above settings for tracking green things... # Select an area in the Framebuffer to copy the color settings. sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # use RGB565. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed. sensor.skip_frames(time=2000) # Let new settings take affect. sensor.set_auto_whitebal(False) # turn this off. clock = time.clock() # Tracks FPS. uart = UART(3, 115200) while(True): img = sensor.snapshot().lens_corr(1.8)#畸变矫正 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):#find_circles用形状识别来识别圆形 area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r()) #area为识别到的圆的区域,即圆的外接矩形框 roi区域 #c.x():圆的中心x坐标,c.r():圆的半径,c.y():圆的中心y坐标,2*c.r():两倍圆的半径。做差:左上角(x,y) statistics = img.get_statistics(roi=area)#像素颜色统计。 #print(statistics) #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。 #如果在该范围内识别到了红色的圆,将这个圆用红色的圈画出来 if 30<statistics.l_mode()<100 and 15<statistics.a_mode()<127 and 15<statistics.b_mode()<127: img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来 output_str="[%d,%d]" % (c.x(),c.y()) print('you send:',output_str) uart.write("坐标是:"+output_str+'\r\n') #数据输出串口 time.sleep_ms(10) #如果在该范围内识别到了绿色的圆,将这个圆用绿色的圈画出来 if 30<statistics.l_mode()<100 and -64<statistics.a_mode()<-8 and -32<statistics.b_mode()<32: img.draw_circle(c.x(), c.y(), c.r(), color = (0, 255, 0))#识别到的绿色圆形用绿色的圆框出来 output_str="[%d,%d]" % (c.x(),c.y()) print('you send:',output_str) uart.write("坐标是:"+output_str+'\r\n') #数据输出串口 time.sleep_ms(10) #如果在该范围内识别到了蓝色的圆,将这个圆用蓝色的圈画出来 if 0<statistics.l_mode()<15 and 0<statistics.a_mode()<40 and -80<statistics.b_mode()<-20: img.draw_circle(c.x(), c.y(), c.r(), color = (0, 0, 255))#识别到的蓝色圆形用蓝色的圆框出来 output_str="[%d,%d]" % (c.x(),c.y()) print('you send:',output_str) uart.write("坐标是:"+output_str+'\r\n') #数据输出串口 time.sleep_ms(10) #如果在该范围内识别到了蓝色的圆,将这个圆用蓝色的圈画出来 if 57<statistics.l_mode()<77 and 20<statistics.a_mode()<64 and 26<statistics.b_mode()<71: img.draw_circle(c.x(), c.y(), c.r(), color = (0, 0, 255))#识别到的蓝色圆形用蓝色的圆框出来 output_str="[%d,%d]" % (c.x(),c.y()) print('you send:',output_str) uart.write("坐标是:"+output_str+'\r\n') #数据输出串口 time.sleep_ms(10) #将非以上色的圆用黑色的矩形框出来 else: img.draw_rectangle(area, color = (0, 0, 0)) print('not found!') uart.write('not found!'+'\n') #数据输出串口 time.sleep_ms(10)
-
最后的else应该减少一个缩进,来配合最上面的for。
你现在的代码,最后的else是配合上面的if的。
-
可以了,谢谢大神指点