问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

OpenMV模版匹配及其应用

创作时间:
作者:
@小白创作中心

OpenMV模版匹配及其应用

引用
CSDN
1.
https://bbs.csdn.net/topics/619466039

OpenMV是由美国克里斯团队基于MicroPython发起的开源机器视觉项目,它搭载了MicroPython解释器,使开发者能够在嵌入式设备上进行Python开发。OpenMV基于32位ARM Cortex-M7内核,结合各种摄像头,可以实现多种机器视觉应用,如人脸检测和物体分类等。

一、OpenMV简单介绍

OpenMV是由美国克里斯团队基于MicroPython发起的开源机器视觉项目,OpenMV搭载了MicroPython解释器,使其可以在嵌入式端进行python开发,OpenMV基于32位,ARM Cortex-M7内核的OpenMV-H7, 并结合各种摄像头,可以进行多种机器视觉应用的实现,比如人脸检测,物体分类等。

然后这里介绍两款基础OpenMV的相关信息

二、OpenMV的应用

OpenMV可以与无人机相结合,也可以运用在电子设计大赛中。2022全国大学生电子设计大赛中的小车识别数字项目,以及2023全国大学生电子设计大赛中的云台追踪激光项目,都运用到了OpenMV。

三、模版匹配及代码

模板匹配就是在给定的图片中,查找和模板最相似的区域,算法的输入包括模板和图片,通过不断移动模板图片,计算其与图片对应区域匹配度,将匹配度最高区域选择为最终结果。

下面是使用OpenMV进行模板匹配的代码示例:

# Template Matching Example - Normalized Cross Correlation (NCC)
#
# This example shows off how to use the NCC feature of your OpenMV Cam to match
# image patches to parts of an image... expect for extremely controlled environments
# NCC is not all to useful.
#
# WARNING: NCC supports needs to be reworked! As of right now this feature needs
# a lot of work to be made into somethin useful. This script will remain to show
# that the functionality exists, but, in its current state is inadequate.
import time
import sensor
import image
import math
from image import SEARCH_EX
from pyb import UART
# from image import SEARCH_DS
# Reset sensor
sensor.reset()
# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# Max resolution for template matching with SEARCH_EX is QQVGA
sensor.set_framesize(sensor.QQVGA)
# You can set windowing to reduce the search image.
# sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)
# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
template = image.Image("5.pgm")
clock = time.clock()
uart = UART(3, 115200)
# Run template matching
while True:
    clock.tick()
    img = sensor.snapshot()
    # find_template(template, threshold, [roi, step, search])
    # ROI: The region of interest tuple (x, y, w, h).
    # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
    # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
    #
    # Note1: ROI has to be smaller than the image and bigger than the template.
    # Note2: In diamond search, step and ROI are both ignored.
    r = img.find_template(
        template, 0.70, step=4, search=SEARCH_EX
    )  # , roi=(10, 0, 60, 60))
    if r:
        img.draw_rectangle(r)
    print(clock.fps())

这里的 “emplate = image.Image("5.pgm")” 就是识别sd卡中5.pgm图片,识别到相同图片即可框出。

四、总结

OpenMV的应用非常广泛,尤其最近两年全国大学生电子设计大赛中加重了摄像头模块的使用,这也是现在智能化发展的趋势,本次只是举了一个简单的例子,接下来我们将讲解OpenMV与stm32之间的通信。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号