opencv 模板匹配matchTemplate研究之一
阿新 • • 發佈:2019-02-04
#include"opencv2\core\core.hpp"
#include"opencv2\highgui\highgui.hpp"
#include"opencv2\imgproc\imgproc.hpp"
#include<iostream>
using namespace std;
using namespace cv;
Mat src,tmpl,result;
int matchMethod = 0;
Mat src_gray,tmpl_gray;
void on_match(int,void*);
int main()
{
src = imread("D:/Develop/opencv/opencvtest/Debug/2.jpg");
tmpl = imread("D:/Develop/opencv/opencvtest/Debug/t3.jpg");
imshow("tmpl",tmpl);
if (!src.data)
return-1;
cvtColor(src,src_gray,CV_RGB2GRAY);
if (!tmpl.data)
return-1;
cvtColor(tmpl,tmpl_gray,CV_RGB2GRAY);
namedWindow("MatchImage");
namedWindow("Result");
createTrackbar("匹配演算法","MatchImage",&matchMethod,5,on_match);
on_match(matchMethod,0);
waitKey();
return 0;
}
void on_match(int,void*)
{
Mat img = src_gray.clone();
matchTemplate(src_gray,tmpl_gray,result,matchMethod);
///定位
double minValue,maxValue;
Point minLoc,maxLoc;
Point matchLoc;
minMaxLoc(result,&minValue,&maxValue,&minLoc,&maxLoc,Mat());
if( matchMethod == CV_TM_SQDIFF || matchMethod == CV_TM_SQDIFF_NORMED )
{ matchLoc = minLoc; }
else
{ matchLoc = maxLoc; }
rectangle(img,matchLoc,Point(matchLoc.x+tmpl.cols,matchLoc.y+tmpl.rows),Scalar::all(0),2,8,0);
rectangle( result, matchLoc, Point( matchLoc.x + tmpl.cols , matchLoc.y + tmpl.rows ), Scalar::all(0), 2, 8, 0 );
resize(img,img,Size(800,600),0,0,CV_INTER_LINEAR); //增加後對大圖也能顯示
resize(result,result,Size(800,600),0,0,CV_INTER_LINEAR);
namedWindow("MatchImage",CV_WND_PROP_FULLSCREEN);
namedWindow("Result",CV_WND_PROP_FULLSCREEN);
imshow("MatchImage",img);
imshow("Result",result);
}
#include"opencv2\highgui\highgui.hpp"
#include"opencv2\imgproc\imgproc.hpp"
#include<iostream>
using namespace std;
using namespace cv;
Mat src,tmpl,result;
int matchMethod = 0;
Mat src_gray,tmpl_gray;
void on_match(int,void*);
int main()
{
src = imread("D:/Develop/opencv/opencvtest/Debug/2.jpg");
tmpl = imread("D:/Develop/opencv/opencvtest/Debug/t3.jpg");
imshow("tmpl",tmpl);
if (!src.data)
return-1;
cvtColor(src,src_gray,CV_RGB2GRAY);
if (!tmpl.data)
return-1;
cvtColor(tmpl,tmpl_gray,CV_RGB2GRAY);
namedWindow("MatchImage");
namedWindow("Result");
createTrackbar("匹配演算法","MatchImage",&matchMethod,5,on_match);
on_match(matchMethod,0);
waitKey();
return 0;
}
void on_match(int,void*)
{
Mat img = src_gray.clone();
matchTemplate(src_gray,tmpl_gray,result,matchMethod);
///定位
double minValue,maxValue;
Point minLoc,maxLoc;
Point matchLoc;
minMaxLoc(result,&minValue,&maxValue,&minLoc,&maxLoc,Mat());
if( matchMethod == CV_TM_SQDIFF || matchMethod == CV_TM_SQDIFF_NORMED )
{ matchLoc = minLoc; }
else
{ matchLoc = maxLoc; }
rectangle(img,matchLoc,Point(matchLoc.x+tmpl.cols,matchLoc.y+tmpl.rows),Scalar::all(0),2,8,0);
rectangle( result, matchLoc, Point( matchLoc.x + tmpl.cols , matchLoc.y + tmpl.rows ), Scalar::all(0), 2, 8, 0 );
resize(img,img,Size(800,600),0,0,CV_INTER_LINEAR); //增加後對大圖也能顯示
resize(result,result,Size(800,600),0,0,CV_INTER_LINEAR);
namedWindow("MatchImage",CV_WND_PROP_FULLSCREEN);
namedWindow("Result",CV_WND_PROP_FULLSCREEN);
imshow("MatchImage",img);
imshow("Result",result);
}