本章内容:
1. 模板匹配算法
输出结果:
代码
#include <ostream>
#include <opencv.hpp>
#include <math.h>int main(int argc, char *argv[])
{
/*
本章内容:
1. 模板匹配算法
*/
cv::String fileName = "/home/wang/dev/Image/heihei.jpeg";
cv::String fileName1 = "/home/wang/dev/Image/hei.png";
cv::Mat src = cv::imread(fileName);
cv::Mat src1 = cv::imread(fileName1);
if(src.data == NULL){
printf("图像读入失败\n");
return -1;
}
cv::imshow("src1",src1);
/* 1. 模板匹配
api接口:CV_EXPORTS_W void matchTemplate( InputArray image, InputArray templ,
OutputArray result, int method, InputArray mask = noArray() );@param image 原图像
@param templ 搜索模板
@param result 返回结果, single-channel 32-bit floating-point
@param method Parameter specifying the comparison method, see #TemplateMatchModes
enum TemplateMatchModes {
TM_SQDIFF = 0,
TM_SQDIFF_NORMED = 1,
TM_CCORR = 2,
TM_CCORR_NORMED = 3,
TM_CCOEFF = 4,
TM_CCOEFF_NORMED = 5
*/
cv::Mat dst;
cv::matchTemplate(src,src1,dst,cv::TM_CCOEFF_NORMED);
cv::imshow("dst", dst);
cv::Point minIdx;
cv::Point maxIdx;
cv::minMaxLoc(dst,0,0,&minIdx,&maxIdx);
cv::rectangle(src,maxIdx,cv::Point(maxIdx.x + src1.rows,maxIdx.y + src1.cols),cv::Scalar(0,0,255),4);
cv::imshow("src",src);cv::waitKey(0);
return 1;
}