栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【opencv】template matching 模板匹配 之 模板选择

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【opencv】template matching 模板匹配 之 模板选择

【opencv】template matching 模板匹配 之 模板选择
  • 一. opencv算法分析
    • 1. TM_CCORR
    • 2. TM_SQDIFF
    • 其他算法
    • 选用指引
  • 二. 代码示例
    • 图片
    • 代码
    • 结果
  • 参考链接

opencv提供了6种算法:

  1. cv.TM_CCOEFF
  2. cv.TM_CCOEFF_NORMED
  3. cv.TM_CCORR
  4. cv.TM_CCORR_NORMED
  5. cv.TM_SQDIFF
  6. cv.TM_SQDIFF_NORMED

以下介绍各种算法的特点

一. opencv算法分析 1. TM_CCORR

互相关算法(cross-correlation)。(拓展阅读见文末)

擅长区分出(有颜色差异的)不同区域。


2. TM_SQDIFF

误差平方和算法(Sum of Squared Differences,简称SSD算法),也叫差方和算法

优点:

  1. 思路简单,容易理解(子图与模板图对应位置上,灰度值之差的绝对值总和,再求平均)。
  2. 运算过程简单,匹配精度高。

缺点:

  1. 运算量偏大。
  2. 对噪声非常敏感。

其他算法

opencv中的算法:

  1. Sum of square differences (SSD): TM_SQDIFF
  2. Cross correlation (CC): TM_CCORR
  3. Mean shifted cross correlation (Pearson correlation coefficient): TM_CCOEFF
  4. Normalization: TM_SQDIFF_NORMED, TM_CCORR_NORMED, TM_CCOEFF_NORMED

图像匹配常用算法

  1. 平均绝对差算法(Mean Absolute Differences,简称MAD算法)
  2. 绝对误差和算法(Sum of Absolute Differences,简称SAD算法)
  3. 误差平方和算法(Sum of Squared Differences,简称SSD算法),也叫差方和算法
  4. 平均误差平方和算法(Mean Square Differences,简称MSD算法),也称均方差算法(MSD之于SSD,等同于MAD之于SAD)
  5. 归一化积相关算法(Normalized Cross Correlation,简称NCC算法),利用子图与模板图的灰度,通过归一化的相关性度量公式来计算二者之间的匹配程度。
  6. 序贯相似性检测算法(Sequential Similiarity Detection Algorithm,简称SSDA算法),是对传统模板匹配算法的改进,比MAD算法快几十到几百倍

选用指引

I think it really depends on your imagery and template. Generally I’d say: if you’re looking for exact or very close to exact matches, use SSD. It is fast, and it definitely maps to what you’re trying to minimize (the difference between the template and image patch). There’s no need to normalize in that case, it is just added overhead. If you have similar requirements but need multiple templates to be comparable, then normalize the SSD. If you’re looking for matches, but you’re working with real-world photographs that may have exposure or contrast differences, the mean shifting and variance equalization from ZNCC will likely be the best.

我认为这取决于你的图像和模板。通常情况是:如果想要精确或接近精确的匹配,请使SSD。它较快,而且肯定按照你所要求的那样,试图最小化模板图片和目标图像之间的差异。在这种情况下,是不需要归一化的,那只是徒增开销。另一种情况是,如果需要多个模板来进行比较,那么建议将SSD算法归一化。还有一种情况是,如果你使用的是,可能有曝光或对比度差异的真实照片,那么ZNCC所包含的均值漂移和方差均衡可能会带来最好的结果。

(source)


二. 代码示例

以下是官方提供的opencv模板匹配代码示例。

图片

原图

模板图片


代码
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('messi5.jpg',0)
img2 = img.copy()
template = cv.imread('template.jpg',0)
w, h = template.shape[::-1]
# All the 6 methods for comparison in a list
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
            'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']
for meth in methods:
    img = img2.copy()
    method = eval(meth)
    # Apply template Matching
    res = cv.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv.rectangle(img,top_left, bottom_right, 255, 2)
    plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img,cmap = 'gray')
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.suptitle(meth)
    plt.show()

结果

1. cv.TM_CCOEFF


2. cv.TM_CCOEFF_NORMED


3. cv.TM_CCORR


4.cv.TM_CCORR_NORMED


5.cv.TM_SQDIFF


6.cv.TM_SQDIFF_NORMED


可以看出, cv.TM_CCORR在这个场景中表现不太好


参考链接

OpenCV: Template Matching

Understanding and evaluating template matching methods - stackoverflow

Lecture 7: Correspondence Matching - 宾夕法尼亚州立大学

卷积(convolution)与互相关(cross-correlation)的一点探讨 - 知乎

【图像配准】基于灰度的模板匹配算法(一):MAD、SAD、SSD、MSD、NCC、SSDA、SATD算法 - CSDN

Pattern Matching & Image Registration - 比利时列日大学课件

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/529852.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号