C语言&嵌入式@IT·互联网DNK210使用指南

《DNK210使用指南 -CanMV版 V1.0》第三十五章 i

2024-11-04  本文已影响0人  正点原子

第三十五章 image图像特征检测实验

在上一章节中,介绍了image模块中图像滤波方法给的使用,本章将继续介绍image模块中图像特征检测方法的使用。通过本章的学习,读者将学习到image模块中图像特征检测的使用。

本章分为如下几个小节:

35.1 image模块图像特征检测方法介绍

35.2 硬件设计

35.3 程序设计

35.4 运行验证

35.1 image模块图像特征检测方法介绍

image模块为Image对象提供了find_edges()方法,用于检测图像中的边缘特征,find_edges()方法如下所示:

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()方法的使用示例如下所示:

import image

img = image.Image(size=(320, 240))

img.to_grayscale()

img.find_edges(image.EDGE_SIMPLE, threshold=(100, 255))

image模块为Image对象提供了find_circles()方法,用于检测图像中的圆形特征,find_circles()方法如下所示:

image.find_circles(roi, x_stride=2, y_stride=1, threshold=2000, x_margin=10, y_margin=10, 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()方法的使用示例如下所示:

import image

img = image.Image(size=(320, 240))

circles = img.find_circles((0, 0, img.width(), img.height()), x_stride=2, y_stride=2, threshold=3800, x_margin=50, y_margin=50, r_margin=50, r_min=60, r_max=80, r_step=5)

for c in circles:

    img.draw_circle(c.x(), c.y(), c.r(), color=(255, 0, 0), thickness=2)

image模块为Image对象提供了find_lines()方法,用于检测图像中的直线特征,find_lines()方法如下所示:

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()方法的使用示例如下所示:

import image

img = image.Image(size=(320, 240))

lines = img.find_lines((0, 0, img.width(), img.height()), x_stride=2, y_stride=1, threshold=1000, theta_margin=25, rho_margin=25)

for l in lines:

    img.draw_line(l.line(), color=(255, 0, 0), thickness=2)

image模块为Image对象提供了find_hog()方法,用于检测图像中的定向梯度特征,find_hog()方法如下所示:

image.find_hog(roi, size=8)

find_hog()方法用于用HoG(定向梯度直方图)线替换图像中的像素,需要注意的是该方法仅支持灰度图像。

roi指的是对Image对象感兴趣的区域,若未指定,即为图像矩形。

size指的是HoG的尺寸。

find_hog()方法会返回经过处理的Image对象。

find_hog()方法的使用示例如下所示:

import image

img = image.Image(size=(320, 240))

img.find_hog((0, 0, img.width(), img.height()), size=8)

35.2 硬件设计

35.2.1 例程功能

1. 获取摄像头输出的图像,并使用image模块对图像进行一些特征检测后,将图像显示在LCD上。

2. 当KEY0按键被按下后,切换image模块对图像的特征检测方式。

35.2.2 硬件资源

本章实验内容,主要讲解image模块的使用,无需关注硬件资源。

35.2.3 原理图

本章实验内容,主要讲解image模块的使用,无需关注原理图。

35.3 程序设计

35.3.1 image模块图像特征检测方法介绍

有关image模块图像特征检测方法的介绍,请见第35.1小节《image模块图像特征检测方法介绍》。

35.3.2 程序流程图

图35.3.2.1image图像特征检测实验流程图

35.3.3 main.py代码

main.py中的脚本代码如下所示:

from board import board_info

from fpioa_manager import fm

from maix import GPIO

import time

import lcd

import sensor

import image

import gc

lcd.init()

sensor.reset()

sensor.set_framesize(sensor.QVGA)

sensor.set_pixformat(sensor.RGB565)

sensor.set_hmirror(False)

type = 0

type_dict = {

    0: "Normal",

    1: "Edge",

    2: "Circle",

    3: "Line",

    4: "HoG"

}

fm.register(board_info.KEY0, fm.fpioa.GPIOHS0)

key0 = GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_UP)

def key_irq_handler(key):

    global key0

    global type

   time.sleep_ms(20)

    if key is key0 and key.value() == 0:

       type = type + 1

       if type == len(type_dict):

           type = 0

key0.irq(key_irq_handler, GPIO.IRQ_FALLING, GPIO.WAKEUP_NOT_SUPPORT, 7)

while True:

    img= sensor.snapshot()

    if type == 0:

       # 原图

       pass

    elif type == 1:

       # 边缘检测

       gray = img.to_grayscale(copy=True)

       gray.find_edges(image.EDGE_SIMPLE, threshold=(100, 255))

       img.draw_image(gray, 0, 0, mask=gray)

       del gray

    elif type == 2:

       # 圆形检测

       circles = img.find_circles((0, 0, img.width(), img.height()), x_stride=2, y_stride=2, threshold=3800, x_margin=50, y_margin=50, r_margin=50, r_min=60, r_max=80, r_step=5)

       for c in circles:

           img.draw_circle(c.x(), c.y(), c.r(), color=(255, 0, 0), thickness=2)

    elif type == 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)

       for l in lines:

           img.draw_line(l.line(), color=(255, 0, 0), thickness=2)

    elif type == 4:

       img.to_grayscale()

       # HoG检测

       img.find_hog((0, 0, img.width(), img.height()), size=8)

    else:

       type = 0

    img.draw_string(10, 10, type_dict[type], color=(255, 0, 0), scale=1.6)

    lcd.display(img)

    gc.collect()

可以看到一开始是先初始化了LCD、摄像头和中断按键,并且按下中断按键可以切换图像特征检测的方式。

接着在一个循环中不断地获取摄像头输出的图像,因为获取到的图像就是Image对象,因此可以直接调用image模块为Image对象提供的各种方法,然后就是对图像进行特征检测,最后在LCD显示图像以及检测到的特征。

35.4 运行验证

将DNK210开发板连接CanMV IDE,点击CanMV IDE上的“开始(运行脚本)”按钮后,便能看到LCD上显示了处理后的摄像头图像,按下KEY0按键还能够切换特征检测方式,如下图所示:

图35.4.1 摄像头原图图像 图35.4.2 边缘检测 图35.4.3 圆形检测 图35.4.4 直线检测 图35.4.5 HoG检测
上一篇 下一篇

猜你喜欢

热点阅读