平方差匹配 method=CV_TM_SQDIFF
这类方法利用平方差来进行匹配,最好匹配为0.匹配越差,匹配值越大.
R ( x , y ) = ∑ x ′ , y ′ ( T ( x ′ , y ′ ) − I ( x + x ′ , y + y ′ ) ) 2 R(x,y)= sum _{x',y'} (T(x',y')-I(x+x',y+y'))^2 R(x,y)=x′,y′∑(T(x′,y′)−I(x+x′,y+y′))2
标准平方差匹配 method=CV_TM_SQDIFF_NORMED
R ( x , y ) = ∑ x ′ , y ′ ( T ( x ′ , y ′ ) − I ( x + x ′ , y + y ′ ) ) 2 ∑ x ′ , y ′ T ( x ′ , y ′ ) 2 ⋅ ∑ x ′ , y ′ I ( x + x ′ , y + y ′ ) 2 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}} R(x,y)=∑x′,y′T(x′,y′)2⋅∑x′,y′I(x+x′,y+y′)2 ∑x′,y′(T(x′,y′)−I(x+x′,y+y′))2
相关匹配 method=CV_TM_CCORR
这类方法采用模板和图像间的乘法操作,所以较大的数表示匹配程度较高, 0标识最坏的匹配效果.
R ( x , y ) = ∑ x ′ , y ′ ( T ( x ′ , y ′ ) ⋅ I ( x + x ′ , y + y ′ ) ) R(x,y)= sum _{x',y'} (T(x',y') cdot I(x+x',y+y')) R(x,y)=x′,y′∑(T(x′,y′)⋅I(x+x′,y+y′))
标准相关匹配 method=CV_TM_CCORR_NORMED
R ( x , y ) = ∑ x ′ , y ′ ( T ( x ′ , y ′ ) ⋅ I ′ ( x + x ′ , y + y ′ ) ) ∑ x ′ , y ′ T ( x ′ , y ′ ) 2 ⋅ ∑ x ′ , y ′ I ( x + x ′ , y + y ′ ) 2 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}} R(x,y)=∑x′,y′T(x′,y′)2⋅∑x′,y′I(x+x′,y+y′)2 ∑x′,y′(T(x′,y′)⋅I′(x+x′,y+y′))
相关匹配 method=CV_TM_CCOEFF
这类方法将模版对其均值的相对值与图像对其均值的相关值进行匹配,1表示完美匹配,-1表示糟糕的匹配,0表示没有任何相关性(随机序列).
R ( x , y ) = ∑ x ′ , y ′ ( T ′ ( x ′ , y ′ ) ⋅ I ( x + x ′ , y + y ′ ) ) R(x,y)= sum _{x',y'} (T'(x',y') cdot I(x+x',y+y')) R(x,y)=x′,y′∑(T′(x′,y′)⋅I(x+x′,y+y′))
其中
T ′ ( x ′ , y ′ ) = T ( x ′ , y ′ ) − 1 / ( w ⋅ h ) ⋅ ∑ x ′ ′ , y ′ ′ T ( x ′ ′ , y ′ ′ ) I ′ ( x + x ′ , y + y ′ ) = I ( x + x ′ , y + y ′ ) − 1 / ( w ⋅ h ) ⋅ ∑ x ′ ′ , y ′ ′ 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} T′(x′,y′)=T(x′,y′)−1/(w⋅h)⋅∑x′′,y′′T(x′′,y′′)I′(x+x′,y+y′)=I(x+x′,y+y′)−1/(w⋅h)⋅∑x′′,y′′I(x+x′′,y+y′′)
标准相关匹配 method=CV_TM_CCOEFF_NORMED
R ( x , y ) = ∑ x ′ , y ′ ( T ′ ( x ′ , y ′ ) ⋅ I ′ ( x + x ′ , y + y ′ ) ) ∑ x ′ , y ′ T ′ ( x ′ , y ′ ) 2 ⋅ ∑ x ′ , y ′ I ′ ( x + x ′ , y + y ′ ) 2 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} } R(x,y)=∑x′,y′T′(x′,y′)2⋅∑x′,y′I′(x+x′,y+y′)2 ∑x′,y′(T′(x′,y′)⋅I′(x+x′,y+y′))
函数签名:
头文件 quick_opencv.h:声明类与公共函数
#pragma once
#include <opencv2opencv.hpp>
using namespace cv;class QuickDemo {
public:...void template_match_Demo(Mat& image1, Mat& image2);};
主函数调用该类的公共成员函数
#include <opencv2opencv.hpp>
#include <quick_opencv.h>
#include <iostream>
using namespace cv;int main(int argc, char** argv) {Mat src1 = imread("D:\Desktop\pandas_small22.png");Mat src2 = imread("D:\Desktop\pandas_small22_test1.png");if (pty()) {printf("Could not load n");return -1;}if (pty()) {printf("Could not load n");return -1;}QuickDemo plate_match_Demo(src1, src2);waitKey(0);destroyAllWindows();return 0;
}
源文件 quick_demo.cpp:实现类与公共函数
#include <quick_opencv.h>
#include <opencv2/dnn.hpp>
#include <iostream>using namespace cv;
using namespace std;class templ_match {
public:Mat image_src;Mat image_tem;const char* OUTPUT_t;const char* MATCH_t;
};static void on_match(int method_index, void* templ_match_) {Mat img_display;templ_match match_info = *((templ_match*)templ_match_);Mat src = match_info.image_src;Mat templ = match_info.image_pyTo(img_display);int result_rows = ws - ws + 1;int result_cols = ls - ls + 1;Mat result = Mat::ls, ws, CV_32FC1);matchTemplate(src, templ, result, method_index, Mat());normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());double minValue, maxValue;Point minLoc, maxLoc;Point matcLoc;minMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc, Mat());if ((method_index == TM_SQDIFF) || (method_index == TM_SQDIFF_NORMED)) {matcLoc = minLoc;}else{matcLoc = maxLoc;}rectangle(img_display, matcLoc, Point(matcLoc.x + ls, matcLoc.y + ws), Scalar::all(0), 2, LINE_AA);rectangle(result, matcLoc, Point(matcLoc.x + ls, matcLoc.y + ws), Scalar::all(0), 2, LINE_AA);imshow(match_info.OUTPUT_t, result);imshow(match_info.MATCH_t, img_display);
}void QuickDemo::template_match_Demo(Mat& image, Mat& test1) {const char* OUTPUT_WIN = "result image";const char* MATCH_WIN = "match demo";namedWindow(OUTPUT_WIN, WINDOW_AUTOSIZE);namedWindow(MATCH_WIN, WINDOW_AUTOSIZE);templ_match match_info;match_info.OUTPUT_t = OUTPUT_WIN;match_info.MATCH_t = MATCH_WIN;match_info.image_src = image;match_info.image_tem = test1;int current_Method = 0;createTrackbar("Algo_type", MATCH_WIN, ¤t_Method, 5, on_match, &match_info);on_match(current_Method, &match_info);
}
本文发布于:2024-02-02 14:13:25,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170685440444336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |