Openmv第四天之模板匹配

前几天学的内容是只是关于颜色和Apriltag的识别出来,但是如果颜色一样呢?那东西识别出来不就一个玩意儿了,这样是不好的,所以应该就衍生出了模板匹配。你在openmv内存里面给定一个模板图片,也就是你的目标的样子,你就可以根据样子去找一定是这个样子的目标,一定是什么意思呢,一定的意思我理解为是大小形状都要基本上一样,小了不行,大了也不行,形状也要大致相等。采用的是ncc算法,只能匹配与模板图片大小和角度基本一致的图案。局限性相对来说比较大,视野中的目标图案稍微比模板图片大一些或者小一些就可能匹配不成功。

那么用处在哪里呢?

  • 模板匹配适应于摄像头与目标物体之间距离确定,不需要动态移动的情况。比如适应于流水线上特定物体的检测,而不适应于小车追踪一个运动的排球(因为运动的排球与摄像头的距离是动态的,摄像头看到的排球大小会变化,不会与模板图片完全一样)。

下面是官方代码

# 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 enviorments
# 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 reamin to show
# that the functionality exists, but, in its current state is inadequate.

import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
#从imgae模块引入SEARCH_EX和SEARCH_DS。使用from import仅仅引入SEARCH_EX, 
#SEARCH_DS两个需要的部分,而不把image模块全部引入。

# 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("/template.pgm")
#加载模板图片

clock = time.clock()

# 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))
    #find_template(template, threshold, [roi, step, search]),threshold中
    #的0.7是相似度阈值,roi是进行匹配的区域(左上顶点为(10,0),长80宽60的矩形),
    #注意roi的大小要比模板图片大,比frambuffer小。
    #把匹配到的图像标记出来
    if r:
        img.draw_rectangle(r)

    print(clock.fps())

这里有个一眼就觉得很重要的函数

那就是image.find_template(templatethreshold[, roi[, step=2[, search=image.SEARCH_EX]]])

这个函数就是返回你所想找的模板的roi,这样你就可以框.框起来了?QQVGA也要,因为为了提高速度!

里面传入的参数呢有哪些呢?

1:你想找的模板的图片

2:阈值,看起来莫名其妙,感觉就是你给大了准确但慢,给小了就是快但不准确的东西,小调试有用。

3:也是一个莫名其妙的东西,就是要忽略像素的个数在你找模板的时候,也是为了算法速度的。

4:这个就是寻找方式了,内部的东东,大多数用这个Ex的这个东西,对了3必须要在EX下有用赋值。

他返回回来的就是一个roi 没有的话就是none,所以很明显可以用if判断是否有找到,找到了的话,就用roi了,直接画个框框,框住他。

要注意的是 这里要引入寻找方法模块 还有要插一个sd卡来存你的模板图片,格式要改成pmg

而已你可以用buffer里面画一个roi来存图片也是允许的。

还有一个最重要的 这个模板识别是灰度图,也就是都是要么白要么黑图片,那些彩色的圈圈或者框框就不能用了,也不是不能用,用了等于没用。

多模板呢咋办呢,我想识别多个东西,或者一个东西的不同样子

第一改存的东西

templates = ["/0.pgm", "/1.pgm", "/2.pgm", "/6.pgm"] 

然后呢?这么多东西,他一次只能识别一个,当然用前面的for循环,一个个遍历就好了

 for t in templates:
        template = image.Image(t)
        #对每个模板遍历进行模板匹配
        r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))

 然后下面画框框就行了0.0

/./ 你如果不嫌麻烦 你可以截取一个物体的很多照片,然后他放大放小也能被识别出来了0..0

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年7月29日
下一篇 2023年7月29日

相关推荐