标签:eth normal 全局变量 参考 创建 oid win using double
演示用局部图片匹配整张图片
VS2017 + OpenCV4.0.1编译
1 #include <iostream> 2 #include <opencv2/opencv.hpp> 3 #include <opencv2/core.hpp> 4 #include <opencv2/highgui.hpp> 5 #include <opencv2/imgproc.hpp> 6 using namespace std; 7 using namespace cv; 8 9 //定义全局变量 10 Mat src, temp, res; 11 12 //显示窗口名 13 string img_window = "source image"; 14 string res_window = "result image"; 15 16 int match_method; 17 int max_tracker = 5; 18 19 //滑动条回调函数 20 void match(int pos, void*) 21 { 22 Mat subImg; 23 src.copyTo(subImg);//拷贝副本 24 int res_row = src.rows - temp.rows + 1; 25 int res_col = src.cols - temp.cols + 1; 26 res.create(res_row, res_col, CV_32FC1);//创建结果矩阵 27 28 //模板匹配 29 matchTemplate(subImg, temp, res, match_method); 30 normalize(res, res, 0, 1, NORM_MINMAX, -1, Mat()); 31 32 //定位 33 double minVal, maxVal; 34 Point minLoc, maxLoc, matchLoc; 35 minMaxLoc(res, &minVal, &maxVal, &minLoc, &maxLoc, Mat()); 36 37 //分类讨论 38 if (match_method == TM_SQDIFF || match_method == TM_SQDIFF_NORMED) 39 matchLoc = minLoc; 40 else 41 matchLoc = maxLoc; 42 43 //矩形框显示匹配结果 44 rectangle(subImg, matchLoc, Point(matchLoc.x + temp.cols, matchLoc.y + temp.rows), Scalar(0, 255, 0), 2); 45 rectangle(res, matchLoc, Point(matchLoc.x + temp.cols, matchLoc.y + temp.rows), Scalar(0, 255, 0), 2); 46 47 imshow(img_window, subImg); 48 imshow(res_window, res); 49 } 50 51 int main() 52 { 53 //读入原图和模板图 54 src = imread("D:\\trashBox\\testIMG\\memory.jpg"); 55 temp = imread("D:\\trashBox\\testIMG\\temp.png"); 56 imshow("模板", temp); 57 58 //创建显示窗口 59 namedWindow(img_window); 60 namedWindow(res_window); 61 62 //创建滑动条 63 match(2, 0); 64 createTrackbar("匹配方法", img_window, &match_method, max_tracker, match); 65 66 waitKey(0); 67 return 0; 68 }
参考资料
标签:eth normal 全局变量 参考 创建 oid win using double
原文地址:https://www.cnblogs.com/yocichen/p/10960967.html