#include <quick_opencv.h>
#include <opencv2/dnn.hpp>
#include <iostream>
using namespace cv;
using namespace std;
class templ_match {
public:
Mat image_src;
Mat image_tem;
const char* OUTPUT_t;
const char* MATCH_t;
};
static void on_match(int method_index, void* templ_match_) {
Mat img_display;
templ_match match_info = *((templ_match*)templ_match_);
Mat src = match_info.image_src;
Mat templ = match_info.image_tem;
src.copyTo(img_display);
int result_rows = src.rows - templ.rows + 1;
int result_cols = src.cols - templ.cols + 1;
Mat result = Mat::zeros(src.cols, src.rows, CV_32FC1);
matchTemplate(src, templ, result, method_index, Mat());
normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
double minValue, maxValue;
Point minLoc, maxLoc;
Point matcLoc;
minMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc, Mat());
if ((method_index == TM_SQDIFF) || (method_index == TM_SQDIFF_NORMED)) {
matcLoc = minLoc;
}else{
matcLoc = maxLoc;
}
rectangle(img_display, matcLoc, Point(matcLoc.x + templ.cols, matcLoc.y + templ.rows), Scalar::all(0), 2, LINE_AA);
rectangle(result, matcLoc, Point(matcLoc.x + templ.cols, matcLoc.y + templ.rows), Scalar::all(0), 2, LINE_AA);
imshow(match_info.OUTPUT_t, result);
imshow(match_info.MATCH_t, img_display);
}
void QuickDemo::template_match_Demo(Mat& image, Mat& test1) {
const char* OUTPUT_WIN = "result image";
const char* MATCH_WIN = "match demo";
namedWindow(OUTPUT_WIN, WINDOW_AUTOSIZE);
namedWindow(MATCH_WIN, WINDOW_AUTOSIZE);
templ_match match_info;
match_info.OUTPUT_t = OUTPUT_WIN;
match_info.MATCH_t = MATCH_WIN;
match_info.image_src = image;
match_info.image_tem = test1;
int current_Method = 0;
createTrackbar("Algo_type", MATCH_WIN, ¤t_Method, 5, on_match, &match_info);
on_match(current_Method, &match_info);
}