#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
using namespace cv;
using namespace std;
int main()
{
Mat g_findImage = imread("利利.jpg");
Mat modeImage = imread("利利頭像.jpg");
imshow("【被查找的圖像】", g_findImage);
imshow("【模版圖像】", modeImage);
Mat findImage;
g_findImage.copyTo(findImage);
//建立輸出圖像,輸出圖像的寬度 = 被查找到額圖像的寬度 - 模版圖像的寬度 + 1, 高度同樣符合
Mat dstImage;
dstImage.create(findImage.rows - modeImage.rows + 1, findImage.cols - modeImage.cols + 1, CV_32FC1);
//進行模版比對,首先是方式0(平方差比對法)
matchTemplate(findImage, modeImage, dstImage, 0);
normalize(dstImage, dstImage, 0, 1, 32);
//繪制矩形友善顯示
//首先是從得到的 輸出矩陣中得到 最大或最小值(平方差比對方式是越小越好,是以在這種方式下,找到最小位置)
//找矩陣的最小位置的函數是 minMaxLoc函數
Point minPoint;
minMaxLoc(dstImage, 0, 0, &minPoint, 0);
//開始正式繪制
rectangle(findImage, minPoint, Point(minPoint.x + modeImage.cols, minPoint.y + modeImage.rows)
, Scalar(theRNG().uniform(0, 255), theRNG().uniform(0, 255), theRNG().uniform(0, 255)), 3, 8);
imshow("【比對後的圖像】", findImage);
rectangle(dstImage, minPoint, Point(minPoint.x + modeImage.cols, minPoint.y + modeImage.rows)
, Scalar(theRNG().uniform(0, 255), theRNG().uniform(0, 255), theRNG().uniform(0, 255)), 3, 8);
imshow("【比對後的計算過程圖像】", dstImage);
waitKey(0);
return 0;
}
opencv3 實作模版比對-matchTemplate函數