代码如下:
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()
min_degree = 45
max_degree = 135
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1.8)
for l in img.find_lines(threshold = 1000, theta_margin = 50, rho_margin = 50):
if (min_degree <= l.theta()) and (l.theta() <= max_degree):
# print(l)
statistics = img.get_statistics()#像素颜色统计
#(26, 54, -51, 50, -90, -43)是蓝色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
#l_mean(),a_mean(),b_mean()是L通道,A通道,B通道的中位数。
if 26<statistics.l_mode()<54 and -51<statistics.a_mode()<50 and -90<statistics.b_mode()<-43:#if the circle is red
img.draw_line(l.line(), color = (255, 255, 255))#识别到蓝色直线用红色的直线表示出来
print(l.y1())
print("YES")
else:
img.draw_line(l.line(), color = (255, 0, 0))
#将非蓝色的线用白色的直线表示出来
print("NO")
有几个疑问:
1.目前不能做到同时识别颜色和直线,阈值是没问题的,是代码出现什么问题了吗?
2.识别直线的时候,图像里的直线变化很快是为什么呢?

图像里出现的直线都能识别,但是就是不能把目标直线单独识别出来
你的统计信息,判断的是整个图片的,不是直线的颜色,也不是目标的。
@kidswong999 那直线的应该用哪个函数统计鸭?我是单纯的把“识别直线”和“颜色形状同时识别”的代码稍加修改结合起来的
我不知道你具体要做什么?
@kidswong999 我想要识别蓝色的那根杆
@r52i 并且输出蓝色杆的y轴坐标
那就直接颜色找到蓝色色块就行吧
@kidswong999 懂啦,谢谢~