Ask Your Question

zijuber's profile - activity

2013-04-10 22:32:15 -0600 commented answer Template Matching

You were right on target. I included my images in the wrong folder and banging my head on the table for the past 1 week. Thanks for the help! (:

2013-04-10 19:41:13 -0600 received badge  Editor (source)
2013-04-10 12:00:45 -0600 asked a question Template Matching

Hello,

I am trying to learn template matching using OpenCV. I started off with trying to compile the code located in this address that was provided by OpenCV.

http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

The code complied after I configured OpenCV with Visual Studio 10 Ulimate edition. When I run the compiled exe, it crashes with some weird message about line 929 NULL pointer or similar. I traced down the problem to this line:

img = imread( argv[1], 1 );
templ = imread( argv[2], 1 );

In above two, I replaced the argv[1] and argv[2] with their respective "image_name.jpg" and then I did not receive the same crash message anymore. Now I was getting a different message. I traced down the problem to the following line:

matchTemplate( img, templ, result, match_method );

It just doesn't work for me. Was I supposed to modify that line? Please help. Also, when I comment out that line I traced another problem to the following line.

imshow( image_window, img_display );

What am I doing wrong? Please help!

Thanks!

Here's the code I am trying to compile...

I get the following error when I don't comment-out matchTemplate() function

Unhandled exception at 0x74cac41f in object.exe: Microsoft C++ exception: cv::Exception at memory location 0x0037dcdc..

The code that was build :

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/core/core.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/// Global Variables
Mat img; Mat templ; Mat result;
const char* image_window = "Source Image";
const char* result_window = "Result window";

int match_method;
int max_Trackbar = 5;

/// Function Headers
void MatchingMethod( int, void* );

/**
 * @function main
 */
int main( int, char** argv )
{

    img = imread( "Template_Matching_Original_Image.jpg", 1 );
    templ = imread( "Template_Matching_Template_Image.jpg", 1 );

  /// Load image and template
  img = imread( argv[1], 1 );
  templ = imread( argv[2], 1 );

  cout << "Hello\n";

  /// Create windows
  namedWindow( image_window, CV_WINDOW_AUTOSIZE );
  namedWindow( result_window, CV_WINDOW_AUTOSIZE );

  /// Create Trackbar
  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 0;

}

void MatchingMethod( int, void*)
{
  /// Source image to display
  Mat img_display;
  img.copyTo( img_display );

  /// Create the result matrix
  int result_cols =  img.cols - templ.cols + 1;
  int result_rows = img.rows - templ.rows + 1;

  result.create( result_cols, result_rows, CV_32FC1 );

  /// Do the Matching and Normalize
  matchTemplate( img, templ, result, match_method );
  normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

  /// Localizing the best match with minMaxLoc
  double minVal; double maxVal; Point minLoc; Point maxLoc;
  Point matchLoc;

  minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );

  /// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
  if( match_method  == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED )
    { matchLoc = minLoc; }
  else
    { matchLoc = maxLoc; }

  /// 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 ...
(more)