Ask Your Question
0

Template Matching

asked 2013-04-10 12:00:45 -0600

zijuber gravatar image

updated 2013-04-11 05:05:42 -0600

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)
edit retag flag offensive close merge delete

Comments

Could you please post the complete debug/build output in your topic, and applying the code layout to it? Solving problems without the exact error is nearly impossible.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-10 12:06:39 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2013-04-10 12:07:07 -0600

berak gravatar image

you always should check resources:

Mat img; imread("im1.jpg",1);
if ( img.empty() )
{
     cerr << "whaa!" << endl;
     return 0;
}

same for the templ image

edit flag offensive delete link more

Comments

1

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! (:

zijuber gravatar imagezijuber ( 2013-04-10 22:32:15 -0600 )edit

Please accept his answer as correct, so this topic show solved.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-11 05:03:20 -0600 )edit
1

answered 2013-04-10 12:34:29 -0600

unxnut gravatar image

Following up on berak's answer, you can specify the working directory in Visual Studio. That way, you will be sure that the images are available when you try to read them (provided you have put them there) and reduce the probability of error.

edit flag offensive delete link more

Comments

However, be aware that the working directory is only usefull in debugging. When creating a standalone application, be aware that the working directory isn't same for all systems.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-10 13:22:59 -0600 )edit
1

That is correct. However, it is invaluable when you are trying to learn and run the tutorials from within Visual Studio. It does not have any effect when you run the stand-alone app outside of Visual Studio. The idea is to cut down uncertainty when you are trying to learn.

unxnut gravatar imageunxnut ( 2013-04-10 13:33:38 -0600 )edit

Agreed, just a remark :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-10 13:36:24 -0600 )edit

Question Tools

Stats

Asked: 2013-04-10 12:00:45 -0600

Seen: 1,568 times

Last updated: Apr 11 '13