#include <iostream>
#include <opencv2/opencv.hpp>
using namespace Std;
using namespace CV;
Mat img1, Img2, IMG3, Img4,img_result, Img_gray1, Img_gray2, Img_gray3, Img_hsv1, Img_hsv2, Img_hsv3;
Matnd Img_hist1, Img_hist2, Img_hist3;
Char win1[] = "Window1";
Char win2[] = "Window2";
Char win3[] = "WINDOW3";
Char win4[] = "WINDOW4";
Char win5[] = "WINDOW5";
int threshold_value = 0;
int max_value = 255;
RNG rng (12345);
int demo_match_template ();
String converttostring (double D);
void Demo_match (int,void*);
int index = 0;
int match_threshold = 20;
int max_track = 255;
int match_method = Cv_tm_sqdiff;
Template Matching
int Demo_match_template ()
{
Namedwindow (Win1, cv_window_autosize);
Namedwindow (win2, cv_window_autosize);
Namedwindow (Win3, cv_window_autosize);
IMG1 = Imread ("d://images//1//p5.jpg");
Img2 = Imread ("d://images//1//p5_1.jpg");
if (Img1.empty () | | | Img2.empty ())
{
cout << "Could not load image ..." << Endl;
return 0;
}
Imshow (Win1, IMG1);
Createtrackbar ("Trace bar", Win1, &match_threshold, Max_track, Demo_match);
Demo_match (0,0);
Imshow (Win1, IMG1);
return 0;
}
void Demo_match (int, void*)
{
int width = img1.cols-img2.cols + 1;
int height = img1.rows-img2.rows + 1;
IMG3 = Mat (WIDTH,HEIGHT,CV_32FC1);
Matchtemplate (IMG1, Img2, IMG3, Match_method, Mat ());
Normalization
Normalize (IMG3, IMG3, 0, 1, Norm_minmax,-1,mat ());
Point Minloc;
Point Maxloc;
Point Temploc;
Double min, Max;
Minmaxloc (IMG3, &min, &max, &minloc, &maxloc, Mat ());
if (Match_method==cv_tm_sqdiff | | match_method==cv_tm_sqdiff_normed)
{
Temploc = Minloc;
}
Else
{
Temploc = Maxloc;
}
Img1.copyto (IMG4);
Draw a rectangle
Rectangle (IMG4, Rect (temploc.x, Temploc.y,img2.cols,img2.rows), Scalar (0,0,255), 2,8);
Rectangle (IMG3, Rect (temploc.x, Temploc.y, Img2.cols, Img2.rows), Scalar (0, 0, 255), 2, 8);
Imshow (WIN2,IMG2);
Imshow (WIN3,IMG4);
}
String converttostring (double D)
{
Ostringstream OS;
if (OS << D)
{
return Os.str ();
}
Return "Invalid conversion ...";
}
int main ()
{
Demo_match_template ();
Waitkey (0);
return 0;
}
OPENCV match Template (contour match)