1657浏览
查看: 1657|回复: 1

[官方教程] 【新手基础教程】 传统算法之图像处理

[复制链接]
本帖最后由 猪百岁 于 2021-3-27 17:13 编辑

传统算法之图像处理

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)

empty  版主

发表于 2021-3-28 19:43:18

有东西的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail