一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 578万

OpenCV中支持的匹配算法

  1. 平方差匹配 method=CV_TM_SQDIFF

这类方法利用平方差来进行匹配,最好匹配为0.匹配越差,匹配值越大.

R(x,y)= \sum _{x',y'} (T(x',y')-I(x+x',y+y'))^2

  1. 标准平方差匹配 method=CV_TM_SQDIFF_NORMED

    R(x,y)= \frac{\sum_{x',y'} (T(x',y')-I(x+x',y+y'))^2}{\sqrt{\sum_{x',y'}T(x',y')^2 \cdot \sum_{x',y'} I(x+x',y+y')^2}}

  2. 相关匹配 method=CV_TM_CCORR

这类方法采用模板和图像间的乘法操作,所以较大的数表示匹配程度较高,0标识最坏的匹配效果.

R(x,y)= \sum _{x',y'} (T(x',y')  \cdot I(x+x',y+y'))

  1. 标准相关匹配 method=CV_TM_CCORR_NORMED

    R(x,y)= \frac{\sum_{x',y'} (T(x',y') \cdot I'(x+x',y+y'))}{\sqrt{\sum_{x',y'}T(x',y')^2 \cdot \sum_{x',y'} I(x+x',y+y')^2}}

  2. 相关匹配 method=CV_TM_CCOEFF

这类方法将模版对其均值的相对值与图像对其均值的相关值进行匹配,1表示完美匹配,-1表示糟糕的匹配,0表示没有任何相关性(随机序列).

R(x,y)= \sum _{x',y'} (T'(x',y')  \cdot I(x+x',y+y'))

在这里

\begin{array}{l} T'(x',y')=T(x',y') - 1/(w  \cdot h)  \cdot \sum _{x'',y''} T(x'',y'') \\ I'(x+x',y+y')=I(x+x',y+y') - 1/(w  \cdot h)  \cdot \sum _{x'',y''} I(x+x'',y+y'') \end{array}

  1. 标准相关匹配 method=CV_TM_CCOEFF_NORMED

    R(x,y)= \frac{ \sum_{x',y'} (T'(x',y') \cdot I'(x+x',y+y')) }{ \sqrt{\sum_{x',y'}T'(x',y')^2 \cdot \sum_{x',y'} I'(x+x',y+y')^2} }

通常,随着从简单的测量(平方差)到更复杂的测量(相关系数),我们可获得越来越准确的匹配(同时也意味着越来越大的计算代价). 最好的办法是对所有这些设置多做一些测试实验,以便为自己的应用选择同时兼顾速度和精度的最佳方案.

 

复制代码
 1 #include "opencv2/highgui/highgui.hpp"
 2 #include "opencv2/imgproc/imgproc.hpp"
 3 #include <iostream>
 4 #include <stdio.h>
 5 
 6 using namespace std;
 7 using namespace cv;
 8 
 9 /// 全局变量
10 Mat img; Mat templ; Mat result;
11 char* image_window = "Source Image";
12 char* result_window = "Result window";
13 
14 int match_method;
15 int max_Trackbar = 5;
16 
17 /// 函数声明
18 void MatchingMethod( int, void* );
19 
20 /** @主函数 */
21 int main( int argc, char** argv )
22 {
23   /// 载入原图像和模板块
24   img = imread( argv[1], 1 );
25   templ = imread( argv[2], 1 );
26 
27   /// 创建窗口
28   namedWindow( image_window, CV_WINDOW_AUTOSIZE );
29   namedWindow( result_window, CV_WINDOW_AUTOSIZE );
30 
31   /// 创建滑动条
32   char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
33   createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );
34 
35   MatchingMethod( 0, 0 );
36 
37   waitKey(0);
38   return 0;
39 }
40 
41 /**
42  * @函数 MatchingMethod
43  * @简单的滑动条回调函数
44  */
45 void MatchingMethod( int, void* )
46 {
47   /// 将被显示的原图像
48   Mat img_display;
49   img.copyTo( img_display );
50 
51   /// 创建输出结果的矩阵
52   int result_cols =  img.cols - templ.cols + 1;
53   int result_rows = img.rows - templ.rows + 1;
54 
55   result.create( result_cols, result_rows, CV_32FC1 );
56 
57   /// 进行匹配和标准化
58   matchTemplate( img, templ, result, match_method );
59   normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
60 
61   /// 通过函数 minMaxLoc 定位最匹配的位置
62   double minVal; double maxVal; Point minLoc; Point maxLoc;
63   Point matchLoc;
64 
65   minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
66 
67   /// 对于方法 SQDIFF 和 SQDIFF_NORMED, 越小的数值代表更高的匹配结果. 而对于其他方法, 数值越大匹配越好
68   if( match_method  == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED )
69     { matchLoc = minLoc; }
70   else
71     { matchLoc = maxLoc; }
72 
73   /// 让我看看您的最终结果
74   rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
75   rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
76 
77   imshow( image_window, img_display );
78   imshow( result_window, result );
79 
80   return;
81 }
复制代码
posted on   一杯清酒邀明月  阅读(690)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示