根据颜色形状同时识别的代码,改为颜色加矩形,但是就是识别不出来红色的矩形,请各位帮忙看看。
-
import sensor, image, time sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) 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() while(True): clock.tick() img = sensor.snapshot().lens_corr(1.8) for c in img.find_rects(threshold = 10000): area = c.rect() #area为识别到的圆的区域,即圆的外接矩形框 statistics = img.get_statistics(roi=area)#像素颜色统计 print(statistics) #(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。 #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:#if the circle is red img.draw_rectangle(area,color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来 #elif 30<statistics.l_mode()<100 and -64<statistics.a_mode()<8 and -32<statistics.b_mode()<32:#if the circle is red # img.draw_circle(c.x(), c.y(), c.r(), color = (0, 255, 0))#识别到的红色圆形用红色的圆框出来 else: img.draw_rectangle(area, color = (255, 255, 255)) #将非红色的圆用白色的矩形框出来 print("FPS %f" % clock.fps())
-
可能是你的阈值不对。
print(statistics) 你可以直接看到你说的红色的矩形的数据。发出来看一下。
-
{"l_mean":51, "l_median":54, "l_mode":65, "l_stdev":13, "l_min":9, "l_max":67, "l_lq":42, "l_uq":65, "a_mean":2, "a_median":1, "a_mode":0, "a_stdev":5, "a_min":-8, "a_max":27, "a_lq":0, "a_uq":5, "b_mean":15, "b_median":21, "b_mode":22, "b_stdev":14, "b_min":-33, "b_max":35, "b_lq":15, "b_uq":23} FPS 30.412698 {"l_mean":53, "l_median":59, "l_mode":65, "l_stdev":14, "l_min":9, "l_max":68, "l_lq":43, "l_uq":65, "a_mean":2, "a_median":1, "a_mode":0, "a_stdev":5, "a_min":-7, "a_max":27, "a_lq":0, "a_uq":4, "b_mean":17, "b_median":22, "b_mode":22, "b_stdev":11, "b_min":-32, "b_max":34, "b_lq":17, "b_uq":23} FPS 30.413361 FPS 30.413797 FPS 30.414231 {"l_mean":52, "l_median":51, "l_mode":65, "l_stdev":12, "l_min":9, "l_max":86, "l_lq":43, "l_uq":65, "a_mean":2, "a_median":1, "a_mode":0, "a_stdev":4, "a_min":-8, "a_max":25, "a_lq":0, "a_uq":4, "b_mean":14, "b_median":21, "b_mode":22, "b_stdev":15, "b_min":-35, "b_max":34, "b_lq":15, "b_uq":24} FPS 30.413971 {"l_mean":53, "l_median":55, "l_mode":65, "l_stdev":12, "l_min":22, "l_max":68, "l_lq":44, "l_uq":65, "a_mean":3, "a_median":1, "a_mode":0, "a_stdev":5, "a_min":-8, "a_max":27, "a_lq":0, "a_uq":5, "b_mean":14, "b_median":21, "b_mode":22, "b_stdev":15, "b_min":-35, "b_max":35, "b_lq":15, "b_uq":23}
-
@kidswong999 我解决了,就是阈值不对和threshold的值调太大了,谢谢了