找回密码
 立即注册
首页 业界区 安全 《DNK210使用指南 -CanMV版 V1.0》第三十五章 image图像 ...

《DNK210使用指南 -CanMV版 V1.0》第三十五章 image图像特征检测实验

郗燕岚 4 小时前
第三十五章 image图像特征检测实验
1)实验平台:正点原子DNK210开发板
2)章节摘自【正点原子】DNK210使用指南 - CanMV版 V1.0
3)购买链接:https://detail.tmall.com/item.htm?&id=782801398750
4)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/k210/ATK-DNK210.html
5)正点原子官方B站:https://space.bilibili.com/394620890
6)正点原子K210技术交流企鹅群:605557868
1.jpeg

2.png

在上一章节中,介绍了image模块中图像滤波方法给的使用,本章将继续介绍image模块中图像特征检测方法的使用。通过本章的学习,读者将学习到image模块中图像特征检测的使用。
本章分为如下几个小节:
35.1 image模块图像特征检测方法介绍
35.2 硬件设计
35.3 程序设计
35.4 运行验证
35.1 image模块图像特征检测方法介绍
image模块为Image对象提供了find_edges()方法,用于检测图像中的边缘特征,find_edges()方法如下所示:
  1. image.find_edges(edge_type, threshold=(100, 200))
复制代码
find_edges()方法用于检测图像中的边缘特征,该方法会将图像变为黑白,仅将边缘像素保留为白色,需要注意的是该方法仅支持灰度图像。
edge_type指的是边缘检测算法的选择,可以是image.EDGE_SIMPLE(简单的阈值高通滤波算法)或image.EDGE_CANNY(Canny边缘检测算法)。
threshold指的是一个包含一个低阈值和一个高阈值的二值元组,可以通过调整阈值来控制边缘质量,默认为(100, 200)。
find_edges()方法会返回经过处理的Image对象。
find_edges()方法的使用示例如下所示:
  1. import image
  2. img = image.Image(size=(320, 240))
  3. img.to_grayscale()
  4. img.find_edges(image.EDGE_SIMPLE, threshold=(100, 255))
复制代码
image模块为Image对象提供了find_circles()方法,用于检测图像中的圆形特征,find_circles()方法如下所示:
  1. image.find_circles(roi, x_stride=2, y_stride=1, threshold=2000, x_margin=10, y_margin=10,
  2. r_margin=10, r_min=2, r_max, r_step=2)
复制代码
find_circles()方法用于检测图像中的圆形特征,该方法使用霍夫变换在图像中查找圆。
roi指的是对Image对象感兴趣的区域,若未指定,即为图像矩形。
x_stride和y_stride指的是霍夫变换时需要跳过的X和Y像素的数量,若已知被检测圆的半径较大,可以增加该参数。
threshold指的是霍夫变换阈值,只返回大于或等于阈值的圆。
x_margin、y_margin和r_margin指的是控制所检测的圆的合并,圆像素为x_margin、y_margin和r_margin的部分合并。
r_min和r_max指的是圆半径的阈值,只返回半径在阈值间的圆。
r_step指的是检测时的半径步进。
find_cricles()方法会返回一个image.circle对象列表
find_circles()方法的使用示例如下所示:
  1. import image
  2. img = image.Image(size=(320, 240))
  3. circles = img.find_circles((0, 0, img.width(), img.height()), x_stride=2, y_stride=2, threshold=3800,
  4. x_margin=50, y_margin=50, r_margin=50, r_min=60, r_max=80, r_step=5)
  5. for c in circles:
  6.     img.draw_circle(c.x(), c.y(), c.r(), color=(255, 0, 0), thickness=2)
复制代码
image模块为Image对象提供了find_lines()方法,用于检测图像中的直线特征,find_lines()方法如下所示:
  1. image.find_lines(roi, x_stride=2, y_stride=1, threshold=1000, theta_margin=25, rho_margin=25)
复制代码
find_lines()方法用于检测图像中的直线特征,该方法使用霍夫变换在图像中查找直线。
roi指的是对Image对象感兴趣的区域,若未指定,即为图像矩形。
x_stride和y_stride指的是霍夫变换时需要跳过的X和Y像素的数量,若已知被检测直线较大,可以增加该参数。
threshold指的是霍夫变换阈值,只返回大于或等于阈值的直线。
theta_margin和rho_margin指的是所检测直线的合并,直线角度为theta_margin的部分和直线p值为rho_margin的部分合并。
find_lines()方法会返回image.line对象列表。
find_lines()方法的使用示例如下所示:
  1. import image
  2. img = image.Image(size=(320, 240))
  3. lines = img.find_lines((0, 0, img.width(), img.height()), x_stride=2, y_stride=1, threshold=1000, theta_margin=25, rho_margin=25)
  4. for l in lines:
  5.     img.draw_line(l.line(), color=(255, 0, 0), thickness=2)
复制代码
image模块为Image对象提供了find_hog()方法,用于检测图像中的定向梯度特征,find_hog()方法如下所示:
  1. image.find_hog(roi, size=8)
复制代码
find_hog()方法用于用HoG(定向梯度直方图)线替换图像中的像素,需要注意的是该方法仅支持灰度图像。
roi指的是对Image对象感兴趣的区域,若未指定,即为图像矩形。
size指的是HoG的尺寸。
find_hog()方法会返回经过处理的Image对象。
find_hog()方法的使用示例如下所示:
  1. import image
  2. img = image.Image(size=(320, 240))
  3. img.find_hog((0, 0, img.width(), img.height()), size=8)
复制代码
35.2 硬件设计
35.2.1 例程功能

  • 获取摄像头输出的图像,并使用image模块对图像进行一些特征检测后,将图像显示在LCD上。
  • 当KEY0按键被按下后,切换image模块对图像的特征检测方式。
    35.2.2 硬件资源
    本章实验内容,主要讲解image模块的使用,无需关注硬件资源。
    35.2.3 原理图
    本章实验内容,主要讲解image模块的使用,无需关注原理图。
    35.3 程序设计
    35.3.1 image模块图像特征检测方法介绍
    有关image模块图像特征检测方法的介绍,请见第35.1小节《image模块图像特征检测方法介绍》。
    35.3.2 程序流程图
    3.png

    图35.3.2.1image图像特征检测实验流程图
35.3.3 main.py代码
main.py中的脚本代码如下所示:
  1. from board import board_info
  2. from fpioa_manager import fm
  3. from maix import GPIO
  4. import time
  5. import lcd
  6. import sensor
  7. import image
  8. import gc
  9. lcd.init()
  10. sensor.reset()
  11. sensor.set_framesize(sensor.QVGA)
  12. sensor.set_pixformat(sensor.RGB565)
  13. sensor.set_hmirror(False)
  14. type = 0
  15. type_dict = {
  16.     0: "Normal",
  17.     1: "Edge",
  18.     2: "Circle",
  19.     3: "Line",
  20.     4: "HoG"
  21. }
  22. fm.register(board_info.KEY0, fm.fpioa.GPIOHS0)
  23. key0 = GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_UP)
  24. def key_irq_handler(key):
  25.     global key0
  26.     global type
  27.    time.sleep_ms(20)
  28.     if key is key0 and key.value() == 0:
  29.        type = type + 1
  30.        if type == len(type_dict):
  31.            type = 0
  32. key0.irq(key_irq_handler, GPIO.IRQ_FALLING, GPIO.WAKEUP_NOT_SUPPORT, 7)
  33. while True:
  34.     img= sensor.snapshot()
  35.     if type == 0:
  36.        # 原图
  37.        pass
  38.     elif type == 1:
  39.        # 边缘检测
  40.        gray = img.to_grayscale(copy=True)
  41.        gray.find_edges(image.EDGE_SIMPLE, threshold=(100, 255))
  42.        img.draw_image(gray, 0, 0, mask=gray)
  43.        del gray
  44.     elif type == 2:
  45.        # 圆形检测
  46.        circles = img.find_circles((0, 0, img.width(), img.height()), x_stride=2, y_stride=2, threshold=3800,
  47. x_margin=50, y_margin=50, r_margin=50, r_min=60, r_max=80, r_step=5)
  48.        for c in circles:
  49.            img.draw_circle(c.x(), c.y(), c.r(), color=(255, 0, 0), thickness=2)
  50.     elif type == 3:
  51.        # 直线检测
  52.        lines = img.find_lines((0, 0, img.width(), img.height()), x_stride=2, y_stride=1, threshold=1000,
  53. theta_margin=25, rho_margin=25)
  54.        for l in lines:
  55.            img.draw_line(l.line(), color=(255, 0, 0), thickness=2)
  56.     elif type == 4:
  57.        img.to_grayscale()
  58.        # HoG检测
  59.        img.find_hog((0, 0, img.width(), img.height()), size=8)
  60.     else:
  61.        type = 0
  62.     img.draw_string(10, 10, type_dict[type], color=(255, 0, 0), scale=1.6)
  63.     lcd.display(img)
  64.     gc.collect()
复制代码
可以看到一开始是先初始化了LCD、摄像头和中断按键,并且按下中断按键可以切换图像特征检测的方式。
接着在一个循环中不断地获取摄像头输出的图像,因为获取到的图像就是Image对象,因此可以直接调用image模块为Image对象提供的各种方法,然后就是对图像进行特征检测,最后在LCD显示图像以及检测到的特征。
35.4 运行验证
将DNK210开发板连接CanMV IDE,点击CanMV IDE上的“开始(运行脚本)”按钮后,便能看到LCD上显示了处理后的摄像头图像,按下KEY0按键还能够切换特征检测方式,如下图所示:
4.png

图35.4.1 摄像头原图图像
5.png

图35.4.2 边缘检测
6.png

图35.4.3 圆形检测
7.png

图35.4.4 直线检测
8.png

图35.4.5 HoG检测

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册