一、整体思路
显示识别二维码得到位置之后,提取颜色即可。
二、实现
1.识别二维码
import sensor, image
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # can be QVGA on M7...
sensor.skip_frames(30)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
while(True):
img = sensor.snapshot()
img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
for code in img.find_qrcodes():
print(code)
2.提取颜色
import sensor, image, time
sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 格式为 RGB565.
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(10) # 跳过10帧,使新设置生效
sensor.set_auto_whitebal(False) # Create a clock object to track the FPS.
ROI=(80,30,15,15)
while(True):
img = sensor.snapshot() # Take a picture and return the image.
statistics=img.get_statistics(roi=ROI)
color_l=statistics.l_mode()
color_a=statistics.a_mode()
color_b=statistics.b_mode()
print(color_l,color_a,color_b)
img.draw_rectangle(ROI)
3.综合
import sensor, image
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # can be QVGA on M7...
sensor.skip_frames(30)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
while(True):
img = sensor.snapshot()
img.lens_corr(1.6) # strength of 1.8 is good for the 2.8mm lens.
for code in img.find_qrcodes():
statistics=img.get_statistics(roi=code.rect())
img.draw_rectangle(code.rect())
color_l=statistics.l_mean()
color_a=statistics.a_mean()
color_b=statistics.b_mean()
#print(color_l,color_a,color_b)
if color_a<0:
print("绿色码")
if color_a>15:
print("红色码")
if 5<color_a<15:
print("黄色码")
#print(color_l)
三、效果