跳转到内容

模板匹配

本页使用了标题或全文手工转换
维基百科,自由的百科全书

模板匹配(英語:Template matching[1]數位影像處理中用于将模板图像匹配目标图像其中一小部分的技术。在常被用于制作业中的质量控制[2]机器人导航英语robotic navigation[3]或图像边缘检测[4]

模板匹配的最大挑战是遮挡检测,在这种情况下,所需的对象部分隐藏在图像中;此外,当物体扭曲或从不同角度成像时,检测非刚性变换亦为挑战之一。其它挑战因素包括图像对照明和背景变化的敏感性、图像背景杂乱,以及图像尺度变化[5]

传统的模板匹配方法包括交叉相关(Cross Correlation)和绝对差和(Sum of Absolute Differences, SAD)法。随着机器学习领域的进步,使用深度神经网络(如 卷积神经网络)进行特征提取已被证明极其有效,并已成为最先进的模板匹配算法的标准[6]。这种基于特征的方法通常比传统的模板匹配方法更具有鲁棒性,并且可以匹配具有非刚性和平面外变换以及高背景杂波和光照变化的模板[7][8][9]

实现

在这个简单的实现中,模板匹配方法应用于灰度图像,通过绝对差和法,最终将给出模板图像与搜索图像最匹配的左上角位置。

minSAD = VALUE_MAX;

// 在匹配的图像中进行循环
for ( size_t x = 0; x <= S_cols - T_cols; x++ ) {
    for ( size_t y = 0; y <= S_rows - T_rows; y++ ) {
        SAD = 0.0;

        // 在模板的图像中进行循环
        for ( size_t j = 0; j < T_cols; j++ )
            for ( size_t i = 0; i < T_rows; i++ ) {

                pixel p_SearchIMG = S[y+i][x+j];
                pixel p_TemplateIMG = T[i][j];
		
                SAD += abs( p_SearchIMG.Grey - p_TemplateIMG.Grey );
            }

        // 保存寻找到的最佳位置
        if ( minSAD > SAD ) { 
            minSAD = SAD;
            position.bestRow = y;
            position.bestCol = x;
            position.bestSAD = SAD;
        }
    }
    
}

对彩色图像执行模板匹配的一种方法是将像素分解为其颜色分量,并使用分别为每种颜色计算的SAD之和来测量颜色模板和搜索图像之间的匹配质量。

参见

参考来源

  1. ^ R. Brunelli, Template Matching Techniques in Computer Vision: Theory and Practice, Wiley, ISBN 978-0-470-51706-2, 2009 ([1] TM book)
  2. ^ Aksoy, M. S.; Torkul, O.; Cedimoglu, I. H. An industrial visual inspection system that uses inductive learning. Journal of Intelligent Manufacturing. 2004, 15 (4): 569–574. S2CID 35493679. doi:10.1023/B:JIMS.0000034120.86709.8c. 
  3. ^ Kyriacou, Theocharis, Guido Bugmann, and Stanislao Lauria. "Vision-based urban navigation procedures for verbally instructed robots." Robotics and Autonomous Systems 51.1 (April 30, 2005): 69-80. Expanded Academic ASAP. Thomson Gale.
  4. ^ WANG, CHING YANG, Ph.D. "EDGE DETECTION USING TEMPLATE MATCHING (IMAGE PROCESSING, THRESHOLD LOGIC, ANALYSIS, FILTERS)". Duke University, 1985, 288 pages; AAT 8523046
  5. ^ Talmi, Itamar; Mechrez, Roey; Zelnik-Manor, Lihi. Template Matching with Deformable Diversity Similarity. 2016-12-07. arXiv:1612.02190可免费查阅 [cs.CV]. 
  6. ^ Zhang, Richard; Isola, Phillip; Efros, Alexei A.; Shechtman, Eli; Wang, Oliver. The Unreasonable Effectiveness of Deep Features as a Perceptual Metric. 2018-01-11. arXiv:1801.03924可免费查阅 [cs.CV]. 
  7. ^ Talmi, Mechrez, Zelnik-Manor. Template Matching with Deformable Diversity Similarity. 2016. arXiv:1612.02190可免费查阅 [cs.CV]. 
  8. ^ Li, Yuhai, L. Jian, T. Jinwen, X. Honbo. “A fast rotated template matching based on point feature.” Proceedings of the SPIE 6043 (2005): 453-459. MIPPR 2005: SAR and Multispectral Image Processing.
  9. ^ B. Sirmacek, C. Unsalan. “Urban Area and Building Detection Using SIFT Keypoints and Graph Theory”, IEEE Transactions on Geoscience and Remote Sensing, Vol.47 (4), pp. 1156-1167, April 2009.

外部链接