一、模板匹配概念
二、单模板匹配
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv; void main()
{
Mat temp=imread("E://mu.jpg");
Mat src=imread("E://lena.jpg");
Mat dst=src.clone();
imshow("temp",temp); int width=src.cols-temp.cols+;//result宽度
int height=src.rows-temp.rows+;//result高度 Mat result(height,width,CV_32FC1);//创建结果映射图像
//matchTemplate(srcImg, templateImg, resultImg, CV_TM_SQDIFF); //平方差匹配法(最好匹配0)
//matchTemplate(srcImg, templateImg, resultImg, CV_TM_SQDIFF_NORMED); //归一化平方差匹配法(最好匹配0)
//matchTemplate(srcImg, templateImg, resultImg, CV_TM_CCORR); //相关匹配法(最坏匹配0)
//matchTemplate(srcImg, templateImg, resultImg, CV_TM_CCORR_NORMED); //归一化相关匹配法(最坏匹配0)
//matchTemplate(srcImg, templateImg, resultImg, CV_TM_CCOEFF); //系数匹配法(最好匹配1)
matchTemplate(src,temp,result,CV_TM_CCOEFF_NORMED);//化相关系数匹配,最佳值1
imshow("result",result);
normalize(result,result,,,NORM_MINMAX,-);//归一化到0-1范围 double minValue,maxValue;
Point minLoc,maxLoc;
minMaxLoc(result,&minValue,&maxValue,&minLoc,&maxLoc);
cout<<"minValue="<<minValue<<endl;
cout<<"maxValue="<<maxValue<<endl; rectangle(dst,maxLoc,Point(maxLoc.x+temp.cols,maxLoc.y+temp.rows),Scalar(,,),,);
imshow("dst",dst); waitKey();
}
注意:result的长宽正好是原图-模板图的长宽,result图中白亮程度表示匹配程度
三、视频模板匹配
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv; void main()
{
Mat frame,resultImg;
Mat templateImg = imread("E://green.jpg");
VideoCapture cap("E://1.mp4");
if(!cap.isOpened())
return;
int resultImg_cols,resultImg_rows; while()
{
cap>>frame;
if(frame.empty()) break;
Mat showImg = frame.clone();
resultImg_cols = frame.cols - templateImg.cols + ;
resultImg_rows = frame.rows - templateImg.rows + ;
resultImg.create(resultImg_cols, resultImg_rows, CV_32FC1);
matchTemplate(frame, templateImg, resultImg, CV_TM_CCOEFF_NORMED); //化相关系数匹配法(最好匹配1)
normalize(resultImg, resultImg, , , NORM_MINMAX); double minValue, maxValue;
Point minLoc, maxLoc;
Point matchLoc; minMaxLoc(resultImg, &minValue, &maxValue, &minLoc, &maxLoc);
cout<<"max_value= "<<maxValue<<endl;
//cout<<"min_value= "<<minValue<<endl;
if(maxValue>=0.7)
rectangle(showImg, maxLoc, Point(maxLoc.x + templateImg.cols, maxLoc.y + templateImg.rows), Scalar(, , ), );
imshow("frame", frame);
imshow("result", showImg);
if( == waitKey())
break;
}
destroyAllWindows(); waitKey();
}
四、多模板匹配(没懂/(ㄒoㄒ)/~~)
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv; void main()
{
Mat srcImg = imread("E://src.png");
Mat templateImg = imread("E://temp.png");
Mat resultImg;
Mat showImg = srcImg.clone(); int resultImg_cols = srcImg.cols - templateImg.cols + ;
int resultImg_rows = srcImg.rows - templateImg.rows + ; resultImg.create(resultImg_cols, resultImg_rows, CV_32FC1);
matchTemplate(srcImg, templateImg, resultImg, CV_TM_CCOEFF_NORMED); //化相关系数匹配法(最好匹配1)
normalize(resultImg, resultImg, , , NORM_MINMAX);
Mat midImg = resultImg.clone(); //多目标模板匹配---方法一
/*double matchValue;
int count0=0;
int tempW=0, tempH=0;
char matchRate[10]; for(int i=0; i<resultImg_rows; i++)
{
for(int j=0; j<resultImg_cols; j++)
{
matchValue = resultImg.at<float>(i, j);
sprintf(matchRate, "%0.2f", matchValue);
if(matchValue>=0.85 && (abs(j - tempW)>5) && (abs(i - tempH)>5) )
{
count0++;
putText(showImg, matchRate, Point(j-5, i-5), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 1);
rectangle(showImg, Point(j, i), Point(j + templateImg.cols, i + templateImg.rows), Scalar(0, 255, 0), 2);
tempW = j;
tempH = i;
}
}
}
cout<<"count="<<count0<<endl;
imshow("resultImg", resultImg);
imshow("dst", showImg);*/ //多目标模板匹配---方法二
double minValue, maxValue;
Point minLoc, maxLoc;
Point matchLoc;
char matchRate[]; for(int i=; i<; i++)
{
int startX = maxLoc.x - ;
int startY = maxLoc.y - ;
int endX = maxLoc.x + ;
int endY = maxLoc.y + ;
if(startX< || startY<)
{
startX = ;
startY = ;
}
if(endX > resultImg.cols - || endY > resultImg.rows - )
{
endX = resultImg.cols - ;
endY = resultImg.rows- ;
}
Mat temp = Mat::zeros(endX - startX, endY - startY, CV_32FC1);
//Mat ROI = resultImg(Rect(Point(startX, startY), temp.cols, temp.rows));
temp.copyTo(resultImg(Rect(startX, startY, temp.cols, temp.rows)));
minMaxLoc(resultImg, &minValue, &maxValue, &minLoc, &maxLoc);
if(maxValue<0.8) break; cout<<"max_value= "<<maxValue<<endl;
sprintf(matchRate, "%0.2f", maxValue);
putText(showImg, matchRate, Point(maxLoc.x - , maxLoc.y - ), CV_FONT_HERSHEY_COMPLEX, , Scalar(, , ), );
rectangle(showImg, maxLoc, Point(maxLoc.x + templateImg.cols, maxLoc.y + templateImg.rows), Scalar(, , ), ); }
imshow("midImg", midImg);
imshow("resultImg", resultImg);
imshow("dst", showImg); waitKey();
}