模板匹配及應用
1.模版匹配——matchTemplate()
2.實現了幾個小應用:圖像單目標模板匹配、視頻單目標模板匹配、多目標模板匹配
先上ppt:
代碼:
1.圖像單目標模板匹配
///圖像單目標模板匹配
#include "opencv2/opencv.hpp"
using namespace cv;
#include <iostream>
using namespace std;
int main()
{
Mat srcImg = imread("j.jpg",CV_LOAD_IMAGE_COLOR);
Mat tempImg = imread("template.jpg",CV_LOAD_IMAGE_COLOR);
cout << "Size of template: "<<tempImg.size() << endl;
//1.構建結果圖像resultImg(注意大小和類型)
//如果原圖(待搜索圖像)尺寸爲W x H, 而模版尺寸爲 w x h, 則結果圖像尺寸一定是(W-w+1)x(H-h+1)
//結果圖像必須爲單通道32位浮點型圖像
int width = srcImg.cols - tempImg.cols + 1;
int height = srcImg.rows - tempImg.rows + 1;
Mat resultImg(Size(width,height),CV_32FC1);
//2.模版匹配
matchTemplate(srcImg, tempImg, resultImg, CV_TM_CCOEFF_NORMED);
imshow("result",resultImg);
//3.正則化(歸一化到0-1)
normalize(resultImg,resultImg,0,1,NORM_MINMAX,-1);
//4.找出resultImg中的最大值及其位置
double minValue = 0;
double maxValue = 0;
Point minPosition;
Point maxPosition;
minMaxLoc(resultImg,&minValue,&maxValue,&minPosition,&maxPosition);
cout << "minValue: " << minValue << endl;
cout << "maxValue: " << maxValue << endl;
cout << "minPosition: " << minPosition << endl;
cout << "maxPosition: " << maxPosition << endl;
//5.根據resultImg中的最大值位置在源圖上畫出矩形
rectangle(srcImg,maxPosition,Point(maxPosition.x+tempImg.cols,maxPosition.y+tempImg.rows),Scalar(0,255,0),1,8);
imshow("srcImg", srcImg);
imshow("template", tempImg);
waitKey(0);
return 0;
}
運行結果:
2.視頻單目標模板匹配
///視頻單目標模板匹配
#include "opencv2/opencv.hpp"
using namespace cv;
#include <iostream>
using namespace std;
int main()
{
//1.定義VideoCapture類對象video,讀取視頻
VideoCapture video("1.mp4");
//1.1.判斷視頻是否打開
if (!video.isOpened())
{
cout << "video open error!" << endl;
return 0;
}
//2.循環讀取視頻的每一幀,對每一幀進行模版匹配
while (1)
{
//2.1.讀取幀
Mat frame;
video >> frame;
//2.2.對幀進行異常檢測
if (frame.empty())
{
cout << "frame empty" << endl;
break;
}
//2.3.對幀進行模版匹配
Mat tempImg = imread("green.JPG", CV_LOAD_IMAGE_COLOR);
cout << "Size of template: " << tempImg.size() << endl;
//2.3.1.構建結果圖像resultImg(注意大小和類型)
//如果原圖(待搜索圖像)尺寸爲W x H, 而模版尺寸爲 w x h, 則結果圖像尺寸一定是(W-w+1)x(H-h+1)
//結果圖像必須爲單通道32位浮點型圖像
int width = frame.cols - tempImg.cols + 1;
int height = frame.rows - tempImg.rows + 1;
Mat resultImg(Size(width, height), CV_32FC1);
//2.3.2.模版匹配
matchTemplate(frame, tempImg, resultImg, CV_TM_CCOEFF_NORMED);
imshow("result", resultImg);
//2.3.3.正則化(歸一化到0-1)
normalize(resultImg, resultImg, 0, 1, NORM_MINMAX, -1);
//2.3.4.找出resultImg中的最大值及其位置
double minValue = 0;
double maxValue = 0;
Point minPosition;
Point maxPosition;
minMaxLoc(resultImg, &minValue, &maxValue, &minPosition, &maxPosition);
cout << "minValue: " << minValue << endl;
cout << "maxValue: " << maxValue << endl;
cout << "minPosition: " << minPosition << endl;
cout << "maxPosition: " << maxPosition << endl;
//2.3.5.根據resultImg中的最大值位置在源圖上畫出矩形
rectangle(frame, maxPosition, Point(maxPosition.x + tempImg.cols, maxPosition.y + tempImg.rows), Scalar(0, 255, 0), 1, 8);
imshow("srcImg", frame);
imshow("template", tempImg);
if (waitKey(10) == 27)
{
cout << "ESC退出" << endl;
break;
};
}
return 0;
}
運行結果:
3.多目標模板匹配
///多目標模板匹配
#include "opencv2/opencv.hpp"
using namespace cv;
#include <iostream>
using namespace std;
int main()
{
Mat srcImg = imread("k.jpg", CV_LOAD_IMAGE_COLOR);
Mat tempImg = imread("template.jpg", CV_LOAD_IMAGE_COLOR);
//1.構建結果圖像resultImg(注意大小和類型)
//如果原圖(待搜索圖像)尺寸爲W x H, 而模版尺寸爲 w x h, 則結果圖像尺寸一定是(W-w+1)x(H-h+1)
//結果圖像必須爲單通道32位浮點型圖像
int width = srcImg.cols - tempImg.cols + 1;
int height = srcImg.rows - tempImg.rows + 1;
Mat resultImg(Size(width, height), CV_32FC1);
//2.模版匹配
matchTemplate(srcImg, tempImg, resultImg, CV_TM_CCOEFF_NORMED);
imshow("result", resultImg);
//3.正則化(歸一化到0-1)
normalize(resultImg, resultImg, 0, 1, NORM_MINMAX, -1);
//4.遍歷resultImg,給定篩選條件,篩選出前幾個匹配位置
int tempX = 0;
int tempY = 0;
char prob[10] = { 0 };
//4.1遍歷resultImg
for (int i = 0 ; i<resultImg.rows;i++)
{
for (int j = 0; j<resultImg.cols; j++)
{
//4.2獲得resultImg中(j,x)位置的匹配值matchValue
double matchValue = resultImg.at<float>(i, j);
sprintf(prob, "%.2f", matchValue);
//4.3給定篩選條件
//條件1:概率值大於0.9
//條件2:任何選中的點在x方向和y方向上都要比上一個點大5(避免畫邊框重影的情況)
if (matchValue > 0.9&& abs(i-tempY)>5&&abs(j-tempX)>5)
{
//5.給篩選出的點畫出邊框和文字
rectangle(srcImg, Point(j,i), Point(j + tempImg.cols, i + tempImg.rows), Scalar(0, 255, 0), 1, 8);
putText(srcImg, prob, Point(j, i+100),CV_FONT_BLACK,1,Scalar(0,0,255),1);
tempX = j;
tempY = i;
}
}
}
imshow("srcImg", srcImg);
imshow("template", tempImg);
waitKey(0);
return 0;
}
運行結果: