Skip to content

Instantly share code, notes, and snippets.

@qx
Last active June 13, 2019 11:03
Show Gist options
  • Save qx/10645f148702fc74cd67b948e6714174 to your computer and use it in GitHub Desktop.
Save qx/10645f148702fc74cd67b948e6714174 to your computer and use it in GitHub Desktop.
test
/**
* @file MatchTemplate_Demo.cpp
* @brief Sample code to use the function MatchTemplate
* @author OpenCV team
*/
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
//! [declare]
/// Global Variables
bool use_mask;
Mat img; Mat templ; Mat mask; Mat result;
const char* image_window = "Source Image";
const char* result_window = "Result window";
int match_method;
int max_Trackbar = 5;
//! [declare]
/// Function Headers
void MatchingMethod( int, void* );
bool match_one(int argc, char** argv, int& value1)
{
img = imread("main.jpg", IMREAD_COLOR );
templ = imread( "chessb.png", IMREAD_COLOR );
if(argc > 3) {
use_mask = true;
mask = imread( argv[3], IMREAD_COLOR );
}
if(img.empty() || templ.empty() || (use_mask && mask.empty()))
{
cout << "Can't read one of the images" << endl;
value1 = -1;
return true;
}
namedWindow( image_window, WINDOW_AUTOSIZE );
const char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );
MatchingMethod( 0, 0 );
waitKey(0);
return false;
}
String multipy_name = "multipy_name";
void match_multipy_base(int i, void*)
{
Mat3b img = imread("main.jpg");
Mat3b templ = imread("chessb.png");
// img = imread("main.jpg", IMREAD_COLOR);
// templ = imread("chessb.png", IMREAD_COLOR);
Mat1b img_gray;
Mat1b templ_gray;
cvtColor(img, img_gray, COLOR_BGR2GRAY);
cvtColor(templ, templ_gray, COLOR_BGR2GRAY);
Mat1f result;
matchTemplate(img, templ, result, match_method);
double thresh = 0.7;
threshold(result, result, thresh, 1., THRESH_BINARY);
Mat1b resb;
result.convertTo(resb, CV_8U, 255);
vector<vector<Point>> contours;
findContours(resb, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); ++i)
{
Mat1b mask(result.rows, result.cols, uchar(0));
drawContours(mask, contours, i, Scalar(255), FILLED);
Point max_point;
double max_val;
minMaxLoc(result, NULL, &max_val, NULL, &max_point, mask);
rectangle(img, Rect(max_point.x, max_point.y, templ.cols, templ.rows), Scalar(0, 255, 0), 2);
}
imshow(multipy_name, img);
};
void match_multipy()
{
namedWindow(multipy_name, WINDOW_AUTOSIZE);
const char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
createTrackbar(trackbar_label, multipy_name, &match_method, max_Trackbar, match_multipy_base);
match_multipy_base(0, 0);
waitKey(0);
};
/**
* @function main
*/
int main( int argc, char** argv )
{
// int value1;
// if (match_one(argc, argv, value1)) return value1;
match_multipy();
return 0;
}
/**
* @function MatchingMethod
* @brief Trackbar callback
*/
void MatchingMethod( int, void* )
{
//! [copy_source]
/// Source image to display
Mat img_display;
img.copyTo( img_display );
//! [copy_source]
//! [create_result_matrix]
/// Create the result matrix
int result_cols = img.cols - templ.cols + 1;
int result_rows = img.rows - templ.rows + 1;
result.create( result_rows, result_cols, CV_32FC1 );
//! [create_result_matrix]
//! [match_template]
/// Do the Matching and Normalize
bool method_accepts_mask = (TM_SQDIFF == match_method || match_method == TM_CCORR_NORMED);
if (use_mask && method_accepts_mask)
{ matchTemplate( img, templ, result, match_method, mask); }
else
{ matchTemplate( img, templ, result, match_method); }
//! [match_template]
//! [normalize]
normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
//! [normalize]
//! [best_match]
/// Localizing the best match with minMaxLoc
double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;
minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
//! [best_match]
//! [match_loc]
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if( match_method == TM_SQDIFF || match_method == TM_SQDIFF_NORMED )
{ matchLoc = minLoc; }
else
{ matchLoc = maxLoc; }
//! [match_loc]
//! [imshow]
/// Show me what you got
rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
// rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
imshow( image_window, img_display );
// imshow( result_window, result );
//! [imshow]
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment