template matching in multiple images

asked 2013-04-02 01:59:50 -0600

Aditya gravatar image

updated 2013-04-02 03:28:04 -0600

Guanta gravatar image

My application does the following http://answers.opencv.org/question/10388/template-matching-in-multiple-images/

When i am reading the image for the first time , it is working. But for the second time onwards, its giving the following error.

OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) in unknown function, file ......\src\o pencv\modules\imgproc\src\templmatch.cpp, line 249.

My code is as folllows :

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

using namespace std;
using namespace cv;
int match_method;
Mat img;

int arr[2][2];
float MatchingMethod( int, void*, Mat i, Mat t , int p , int q, float ft)
{
  Mat img_display;
  Mat result;
  i.copyTo( img_display );

  int result_cols =  i.cols - t.cols + 1;
  int result_rows = i.rows - t.rows + 1;

  result.create( result_cols, result_rows, CV_32FC1 );

  matchTemplate( i, t, result, match_method );
  normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

  double minVal;
  double maxVal; 

  Point minLoc; 
  Point maxLoc;
  Point matchLoc;

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

  matchLoc = maxLoc;

  arr[p][q] = matchLoc.x;
  arr[p][q+1] = matchLoc.y;
  ft=result.at<float>(matchLoc.x, matchLoc.y);
  return ft;
}



void main (int , char** argv)
{
    FILE *fp;
    Mat temp[5];
    int i=0,j=0,z=0;
    char a[10];
    char buffer[120];
    cout<< " Program starting " << endl ;
    img = imread( argv[1], 1 );
    fp=fopen("input.txt", "r");
    waitKey(0);
    if( fp==NULL )
    {   
        printf( "Error Opening file ");
    }

    fread(buffer,80,1,fp);
    while(buffer[i]!='\0')
    {
        for(i=0;buffer[i]!= ' ' && buffer[i]!= '\0' ;i++)
        {
            a[j++]=buffer[i];
        }
        a[j]='\0';

        temp[z++] = imread (a, 1);
        cout << " image reading successful " << endl;
        Mat temp1= temp[z];
        cout << " success " ;
        i++;
        float ft1=0.0;
        ft1= MatchingMethod( 0, 0, img, temp1, 0, 0 , ft1);
        cout<< "x" <<arr[0][0] << " y "<< arr[0][1]<< "ft1 "<< ft1<<endl;
    }
    waitKey(0);
    return;
}

Any help will be appreciated.

If this doesnt work, the last thing i can do is call the program for all 5( or maybe 20) images, which will degrade the performance of my app. tremandously. and i dont want to do that.

Thank you.

edit retag flag offensive close merge delete