OPENCV match Template (contour match)

Source: Internet
Author: User
Tags scalar

#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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.