
import sensor, image, time
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()
def find_max_c(areas):
max_size = 0
for area in areas:
if area.r() > max_size:
max_area = area
max_size = area.r()
return max_area
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1.8)
for c in img.find_circles(threshold = 2000, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2):
areas = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
#area为识别到的圆的区域,即圆的外接矩形框
if areas:
max_area = find_max_c(areas)
statistics = img.get_statistics(roi=max_area)#像素颜色统计roi=areas
#print(statistics)
#(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
if 0<statistics.l_mode()<35 and -20<statistics.a_mode()<20 and -20<statistics.b_mode()<20:#if the circle is red
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))#识别到的黑色圆形用红色的圆框出来
print("[%d,%d]" % (c.x(),c.y())) #方式1输出圆心坐标你自己写的代码看不懂吗?
areas应该是一个列表,里面应该有很多的area,可以你24行为什么直接赋值?
大佬能给出参考代码吗?
不能,我不知道你要做什么。比如你24行为什么直接赋值?