传统算法之图像处理
MaixPy 查找色块
找出图片中指定颜色所有色块
1. 使用方法
MaixPy 已经在 image 模块中实现有查找色块方法,需要使用非 minimum 固件版本。
import image, sensor
img=sensor.snapshot()
- 从图片中查找所有色块对象(image.blob)列表, 传入的颜色阈值参数按照 LAB 格式(l_lo,l_hi,a_lo,a_hi,b_lo,b_hi)
green_threshold   = (0,   80,  -70,   -10,   -0,   30)
blobs = img.find_blobs([green_threshold])
根据自己的需求操作色块对象, 例如将色块对象在图像中用矩形框标识出来
tmp=img.draw_rectangle(b[0:4])
详细 API 介绍请查看API-Image.
2. 例程
找绿色色块
import sensor
import image
import lcd
import time
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
green_threshold   = (0,   80,  -70,   -10,   -0,   30)
while True:
    img=sensor.snapshot()
    blobs = img.find_blobs([green_threshold])
    if blobs:
        for b in blobs:
            tmp=img.draw_rectangle(b[0:4])
            tmp=img.draw_cross(b[5], b[6])
            c=img.get_pixel(b[5], b[6])
    lcd.display(img)
MaixPy 查找二维码
从图片中识别二维码,常见的二维码为 QR Code,QR 全称 Quick Response,它比传统的条形码(Bar Code)能存更多的信息,也能表示更多的数据类型.
1. 使用方法
image 模块中已经实现有查找二维码方法,需要使用非 minimum 固件版本,需要准备一个二维码,可以用草料二维码生成你想要的内容.
import image, sensor
img=sensor.snapshot()
- 从图片中查找所有二维码对象(image.qrcode)列表
res = img.find_qrcodes()
例如打印信息
print(res[0].payload())
详细 API 介绍请查看API-Image.
2. 例程
识别二维码,如果识别不到二维码,请尝试更改  sensor.vflip()  函数参数。
import sensor
import image
import lcd
import time
clock = time.clock()
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_vflip(1)
sensor.run(1)
sensor.skip_frames(30)
while True:
    clock.tick()
    img = sensor.snapshot()
    res = img.find_qrcodes()
    fps =clock.fps()
    if len(res) > 0:
        img.draw_string(2, 2, res[0].payload(), color=(0,128,0), scale=2)
        print(res[0].payload())
    lcd.display(img)