Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Angle and Scale Invariant Template matching

Function rotates the template image from 0 to 180(or upto 360 ) degrees to search all related matches(in all angles) in source image even with different scale. The function had been written in OpenCV C interface. When I tried to port it to openCV C++ interface , I am getting lot of errors . Some one please help me to port it to OpenCV C++ interface. Help would be appreciated greatly.

 void TemplateMatch()

{

int i, j, x, y, key;
double minVal;
char windowNameSource[] = "Original Image";
char windowNameDestination[] = "Result Image";
char windowNameCoefficientOfCorrelation[] = "Coefficient of Correlation Image";
 CvPoint minLoc;
 CvPoint tempLoc;

 IplImage *sourceImage = cvLoadImage("template_source.jpg", CV_LOAD_IMAGE_ANYDEPTH         | CV_LOAD_IMAGE_ANYCOLOR);
 IplImage *templateImage = cvLoadImage("template.jpg", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);



 IplImage *graySourceImage = cvCreateImage(cvGetSize(sourceImage), IPL_DEPTH_8U, 1); 
 IplImage *grayTemplateImage =cvCreateImage(cvGetSize(templateImage),IPL_DEPTH_8U,1);
 IplImage *binarySourceImage = cvCreateImage(cvGetSize(sourceImage), IPL_DEPTH_8U, 1); 
 IplImage *binaryTemplateImage = cvCreateImage(cvGetSize(templateImage), IPL_DEPTH_8U, 1); 
 IplImage *destinationImage = cvCreateImage(cvGetSize(sourceImage), IPL_DEPTH_8U, 3); 

 cvCopy(sourceImage, destinationImage);

 cvCvtColor(sourceImage, graySourceImage, CV_RGB2GRAY);
 cvCvtColor(templateImage, grayTemplateImage, CV_RGB2GRAY);

 cvThreshold(graySourceImage, binarySourceImage, 200, 255, CV_THRESH_OTSU );
 cvThreshold(grayTemplateImage, binaryTemplateImage, 200, 255, CV_THRESH_OTSU);

 int templateHeight = templateImage->height;
 int templateWidth = templateImage->width;

 float templateScale = 0.5f;

  for(i = 2; i <= 3; i++) 
   {

int tempTemplateHeight = (int)(templateWidth * (i * templateScale));
int tempTemplateWidth = (int)(templateHeight * (i * templateScale));

IplImage *tempBinaryTemplateImage = cvCreateImage(cvSize(tempTemplateWidth,                  tempTemplateHeight), IPL_DEPTH_8U, 1);

// W - w + 1, H - h + 1

IplImage *result = cvCreateImage(cvSize(sourceImage->width - tempTemplateWidth + 1,      sourceImage->height - tempTemplateHeight + 1), IPL_DEPTH_32F, 1);

cvResize(binaryTemplateImage, tempBinaryTemplateImage, CV_INTER_LINEAR);
float degree = 20.0f;
for(j = 0; j <= 9; j++) 
  {

 IplImage *rotateBinaryTemplateImage = cvCreateImage(cvSize(tempBinaryTemplateImage-  >width, tempBinaryTemplateImage->height), IPL_DEPTH_8U, 1);

   //cvShowImage(windowNameSource, tempBinaryTemplateImage);  
  //cvWaitKey(0);             

    for(y = 0; y < tempTemplateHeight; y++)
      {

     for(x = 0; x < tempTemplateWidth; x++)
      {
        rotateBinaryTemplateImage->imageData[y * tempTemplateWidth + x] = 255;

      }         
      }


   for(y = 0; y < tempTemplateHeight; y++)
     {

    for(x = 0; x < tempTemplateWidth; x++)
      {

   float radian = (float)j * degree * CV_PI / 180.0f;
   int scale = y * tempTemplateWidth + x;

   int rotateY = - sin(radian) * ((float)x - (float)tempTemplateWidth / 2.0f) + cos(radian) * ((float)y - (float)tempTemplateHeight / 2.0f) + tempTemplateHeight / 2;

  int rotateX = cos(radian) * ((float)x - (float)tempTemplateWidth / 2.0f) + sin(radian) * ((float)y - (float)tempTemplateHeight / 2.0f) + tempTemplateWidth / 2;


  if(rotateY < tempTemplateHeight && rotateX < tempTemplateWidth && rotateY >= 0 && rotateX  >= 0)

  rotateBinaryTemplateImage->imageData[scale] = tempBinaryTemplateImage->imageData[rotateY * tempTemplateWidth + rotateX];

 }

}


//cvShowImage(windowNameSource, rotateBinaryTemplateImage);
//cvWaitKey(0);

  cvMatchTemplate(binarySourceImage, rotateBinaryTemplateImage, result, CV_TM_SQDIFF_NORMED); 

  //cvMatchTemplate(binarySourceImage, rotateBinaryTemplateImage, result, CV_TM_SQDIFF);  

   cvMinMaxLoc(result, &minVal, NULL, &minLoc, NULL, NULL);
    printf(": %f%%\n", (int)(i * 0.5 * 100), j * 20, (1 - minVal) * 100);    

 if(minVal < 0.065) // 1 - 0.065 = 0.935 : 93.5% 

{

  tempLoc.x = minLoc.x + tempTemplateWidth;
  tempLoc.y = minLoc.y + tempTemplateHeight;
 cvRectangle(destinationImage, minLoc, tempLoc, CV_RGB(0, 255, 0), 1, 8, 0);

}

}

//cvShowImage(windowNameSource, result);
//cvWaitKey(0);

cvReleaseImage(&tempBinaryTemplateImage);
cvReleaseImage(&result);

}


 // cvShowImage(windowNameSource, sourceImage);
 // cvShowImage(windowNameCoefficientOfCorrelation, result); 

cvShowImage(windowNameDestination, destinationImage);
key = cvWaitKey(0);


cvReleaseImage(&sourceImage);
cvReleaseImage(&templateImage);
cvReleaseImage(&graySourceImage);
cvReleaseImage(&grayTemplateImage);
cvReleaseImage(&binarySourceImage);
cvReleaseImage(&binaryTemplateImage);
cvReleaseImage(&destinationImage);

cvDestroyWindow(windowNameSource);
cvDestroyWindow(windowNameDestination);
cvDestroyWindow(windowNameCoefficientOfCorrelation);

 }

RESULT :

Template image : ... (/upfiles/13747355704074469.jpg)

ResultImage: above function put rectangles around the perfect matches (angle and scale invariant) in this image ..... (/upfiles/13747356555024643.jpg)

Now, I have been trying to port the code into C++ interface. If anyone need details please let me know .

Angle and Scale Invariant Template matching

Function rotates the template image from 0 to 180(or upto 360 ) degrees to search all related matches(in all angles) in source image even with different scale. The function had been written in OpenCV C interface. When I tried to port it to openCV C++ interface , I am getting lot of errors . Some one please help me to port it to OpenCV C++ interface. Help would be appreciated greatly.

 void TemplateMatch()

{

  {

int i, j, x, y, key;
double minVal;
char windowNameSource[] = "Original Image";
char windowNameDestination[] = "Result Image";
char windowNameCoefficientOfCorrelation[] = "Coefficient of Correlation Image";
 CvPoint minLoc;
 CvPoint tempLoc;

 IplImage *sourceImage = cvLoadImage("template_source.jpg", CV_LOAD_IMAGE_ANYDEPTH         | CV_LOAD_IMAGE_ANYCOLOR);
 IplImage *templateImage = cvLoadImage("template.jpg", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);



 IplImage *graySourceImage = cvCreateImage(cvGetSize(sourceImage), IPL_DEPTH_8U, 1); 
 IplImage *grayTemplateImage =cvCreateImage(cvGetSize(templateImage),IPL_DEPTH_8U,1);
 IplImage *binarySourceImage = cvCreateImage(cvGetSize(sourceImage), IPL_DEPTH_8U, 1); 
 IplImage *binaryTemplateImage = cvCreateImage(cvGetSize(templateImage), IPL_DEPTH_8U, 1); 
 IplImage *destinationImage = cvCreateImage(cvGetSize(sourceImage), IPL_DEPTH_8U, 3); 

 cvCopy(sourceImage, destinationImage);

 cvCvtColor(sourceImage, graySourceImage, CV_RGB2GRAY);
 cvCvtColor(templateImage, grayTemplateImage, CV_RGB2GRAY);

 cvThreshold(graySourceImage, binarySourceImage, 200, 255, CV_THRESH_OTSU );
 cvThreshold(grayTemplateImage, binaryTemplateImage, 200, 255, CV_THRESH_OTSU);

 int templateHeight = templateImage->height;
 int templateWidth = templateImage->width;

 float templateScale = 0.5f;

  for(i = 2; i <= 3; i++) 
   {

int tempTemplateHeight = (int)(templateWidth * (i * templateScale));
int tempTemplateWidth = (int)(templateHeight * (i * templateScale));

IplImage *tempBinaryTemplateImage = cvCreateImage(cvSize(tempTemplateWidth,                  tempTemplateHeight), IPL_DEPTH_8U, 1);

// W - w + 1, H - h + 1

IplImage *result = cvCreateImage(cvSize(sourceImage->width - tempTemplateWidth + 1,      sourceImage->height - tempTemplateHeight + 1), IPL_DEPTH_32F, 1);

cvResize(binaryTemplateImage, tempBinaryTemplateImage, CV_INTER_LINEAR);
float degree = 20.0f;
for(j = 0; j <= 9; j++) 
  {

 IplImage *rotateBinaryTemplateImage = cvCreateImage(cvSize(tempBinaryTemplateImage-  >width, tempBinaryTemplateImage->height), IPL_DEPTH_8U, 1);

   //cvShowImage(windowNameSource, tempBinaryTemplateImage);  
  //cvWaitKey(0);             

    for(y = 0; y < tempTemplateHeight; y++)
      {

     for(x = 0; x < tempTemplateWidth; x++)
      {
        rotateBinaryTemplateImage->imageData[y * tempTemplateWidth + x] = 255;

      }         
      }


   for(y = 0; y < tempTemplateHeight; y++)
     {

    for(x = 0; x < tempTemplateWidth; x++)
      {

   float radian = (float)j * degree * CV_PI / 180.0f;
   int scale = y * tempTemplateWidth + x;

   int rotateY = - sin(radian) * ((float)x - (float)tempTemplateWidth / 2.0f) + cos(radian) * ((float)y - (float)tempTemplateHeight / 2.0f) + tempTemplateHeight / 2;

  int rotateX = cos(radian) * ((float)x - (float)tempTemplateWidth / 2.0f) + sin(radian) * ((float)y - (float)tempTemplateHeight / 2.0f) + tempTemplateWidth / 2;


  if(rotateY < tempTemplateHeight && rotateX < tempTemplateWidth && rotateY >= 0 && rotateX  >= 0)

  rotateBinaryTemplateImage->imageData[scale] = tempBinaryTemplateImage->imageData[rotateY * tempTemplateWidth + rotateX];

 }

}


//cvShowImage(windowNameSource, rotateBinaryTemplateImage);
//cvWaitKey(0);

  cvMatchTemplate(binarySourceImage, rotateBinaryTemplateImage, result, CV_TM_SQDIFF_NORMED); 

  //cvMatchTemplate(binarySourceImage, rotateBinaryTemplateImage, result, CV_TM_SQDIFF);  

   cvMinMaxLoc(result, &minVal, NULL, &minLoc, NULL, NULL);
    printf(": %f%%\n", (int)(i * 0.5 * 100), j * 20, (1 - minVal) * 100);    

 if(minVal < 0.065) // 1 - 0.065 = 0.935 : 93.5% 

{

  tempLoc.x = minLoc.x + tempTemplateWidth;
  tempLoc.y = minLoc.y + tempTemplateHeight;
 cvRectangle(destinationImage, minLoc, tempLoc, CV_RGB(0, 255, 0), 1, 8, 0);

}

}

//cvShowImage(windowNameSource, result);
//cvWaitKey(0);

cvReleaseImage(&tempBinaryTemplateImage);
cvReleaseImage(&result);

}


 // cvShowImage(windowNameSource, sourceImage);
 // cvShowImage(windowNameCoefficientOfCorrelation, result); 

cvShowImage(windowNameDestination, destinationImage);
key = cvWaitKey(0);


cvReleaseImage(&sourceImage);
cvReleaseImage(&templateImage);
cvReleaseImage(&graySourceImage);
cvReleaseImage(&grayTemplateImage);
cvReleaseImage(&binarySourceImage);
cvReleaseImage(&binaryTemplateImage);
cvReleaseImage(&destinationImage);

cvDestroyWindow(windowNameSource);
cvDestroyWindow(windowNameDestination);
cvDestroyWindow(windowNameCoefficientOfCorrelation);

 }

RESULT :

Template image : ... (/upfiles/13747355704074469.jpg) (/upfiles/13747360744030719.jpg)

ResultImage: above function put rectangles around the perfect matches (angle and scale invariant) in this image ..... (/upfiles/13747356555024643.jpg)

Now, I have been trying to port the code into C++ interface. If anyone need details please let me know .

Angle and Scale Invariant Template matching

Below Function rotates the template image from 0 to 180(or upto 360 ) 360(using for loop to rotate image matrix 20 degrees in each loop) degrees to search all related matches(in matches present in all angles) angles in source image even with different scale. The function had been written in OpenCV C interface. When image. when I tried to port it to openCV C++ interface build the application , there is no errors . While debug the application I am getting lot of errors . Some one below error,

      Unhandled exception at 0x74ec812f in Template_Match.exe: Microsoft C++ exception:           cv::Exception at memory location 0x002e02a8..

Someone please help me to port it to OpenCV C++ interface. Help rectify the issue.Help would be appreciated greatly.

 /// function to rotate (0 to 360 degrees) template for template matching .

   void TemplateMatch()
matchTemplate()
    {
  int i,  j, x, y, key;
 double minVal;
 char windowNameSource[] = "Original Image";
 char windowNameDestination[] = "Result Image";
 char windowNameCoefficientOfCorrelation[] = "Coefficient of Correlation Image";
 CvPoint 
       Point minLoc;
 CvPoint       Point tempLoc;

 IplImage *sourceImage = cvLoadImage("template_source.jpg", CV_LOAD_IMAGE_ANYDEPTH         | CV_LOAD_IMAGE_ANYCOLOR);
 IplImage *templateImage = cvLoadImage("template.jpg", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);



 IplImage *graySourceImage = cvCreateImage(cvGetSize(sourceImage), IPL_DEPTH_8U, 1); 
 IplImage *grayTemplateImage =cvCreateImage(cvGetSize(templateImage),IPL_DEPTH_8U,1);
 IplImage *binarySourceImage = cvCreateImage(cvGetSize(sourceImage), IPL_DEPTH_8U, 1); 
 IplImage *binaryTemplateImage = cvCreateImage(cvGetSize(templateImage), IPL_DEPTH_8U, 1); 
 IplImage *destinationImage = cvCreateImage(cvGetSize(sourceImage), IPL_DEPTH_8U, 3); 

 cvCopy(sourceImage, destinationImage);

 cvCvtColor(sourceImage, graySourceImage, CV_RGB2GRAY);
 cvCvtColor(templateImage, grayTemplateImage, CV_RGB2GRAY);

 cvThreshold(graySourceImage, binarySourceImage, 200, 255, CV_THRESH_OTSU );
 cvThreshold(grayTemplateImage, binaryTemplateImage, 200, 255, CV_THRESH_OTSU);

 int templateHeight = templateImage->height;
 int templateWidth = templateImage->width;

 float templateScale = 0.5f;

  for(i = 2; i <= 3; i++) 
   {

int tempTemplateHeight = (int)(templateWidth * (i * templateScale));
int tempTemplateWidth = (int)(templateHeight * (i * templateScale));

IplImage *tempBinaryTemplateImage = cvCreateImage(cvSize(tempTemplateWidth,                  tempTemplateHeight), IPL_DEPTH_8U, 1);

// W - w + 1, H - h + 1

IplImage *result = cvCreateImage(cvSize(sourceImage->width - tempTemplateWidth + 1,      sourceImage->height - tempTemplateHeight + 1), IPL_DEPTH_32F, 1);

cvResize(binaryTemplateImage, tempBinaryTemplateImage, CV_INTER_LINEAR);
        Mat templateImage = ("template.jpg");
         Mat sourceImage = ("sourceImage.jpg");

         Mat graySourceImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC1);
         Mat grayTemplateImage = cvCreateMat(templateImage.rows,templateImage.cols,CV_32FC1);
         Mat binarySourceImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC1);
         Mat binaryTemplateImage = cvCreateMat(templateImage.rows,templateImage.cols,CV_32FC1);
         Mat destinationImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC3);

         sourceImage.copyTo(destinationImage);
         cvtColor(sourceImage,graySourceImage,CV_RGB2GRAY);
         cvtColor(templateImage,grayTemplateImage,CV_RGB2GRAY);

         threshold(graySourceImage,binarySourceImage,0.8,1.0,CV_THRESH_TOZERO);
     threshold(grayTemplateImage,binaryTemplateImage,0.8,1.0,CV_THRESH_TOZERO);

        float degree = 20.0f;
 for(j = 0; j <= 9; 18; j++) 
   {

 IplImage *rotateBinaryTemplateImage = cvCreateImage(cvSize(tempBinaryTemplateImage-  >width, tempBinaryTemplateImage->height), IPL_DEPTH_8U, 1);

   //cvShowImage(windowNameSource, tempBinaryTemplateImage);  
  //cvWaitKey(0);             

           Mat tempBinaryTemplateImage = cvCreateMat(templateImage.rows, templateImage.cols,     CV_32FC1);
           Mat result = cvCreateMat(sourceImage.rows - templateImage.rows + 1, sourceImage.cols -templateImage.cols + 1, CV_32FC1);
           resize(binaryTemplateImage, tempBinaryTemplateImage,Size(),0.5,0.5,CV_INTER_AREA);

           Mat rotateBinaryTemplateImage = cvCreateMat(tempBinaryTemplateImage.rows, tempBinaryTemplateImage.cols, CV_32FC1);

          for(y = 0; y < tempTemplateHeight; templateImage.cols; y++)
       {
       for(x = 0; x < tempTemplateWidth; templateImage.rows; x++)
       {
        rotateBinaryTemplateImage->imageData[y * tempTemplateWidth + x] = 255;

      }         
      }


   for(y = 0; y < tempTemplateHeight; y++)
     {

    for(x = 0; x < tempTemplateWidth; x++)
      {

    float radian = (float)j * degree * CV_PI / 180.0f;
    int scale = y * tempTemplateWidth templateImage.rows + x;
     int rotateY = - sin(radian) * ((float)x - (float)tempTemplateWidth (float)templateImage.rows / 2.0f) + cos(radian) * ((float)y - (float)tempTemplateHeight (float)templateImage.cols / 2.0f) + tempTemplateHeight templateImage.cols / 2;
    int rotateX = cos(radian) * ((float)x - (float)tempTemplateWidth (float)templateImage.rows / 2.0f) + sin(radian) * ((float)y - (float)tempTemplateHeight (float)templateImage.cols / 2.0f) + tempTemplateWidth templateImage.rows / 2;

    if(rotateY < tempTemplateHeight templateImage.cols && rotateX < tempTemplateWidth templateImage.rows && rotateY >= 0 && rotateX  >= 0)

  rotateBinaryTemplateImage->imageData[scale] = tempBinaryTemplateImage->imageData[rotateY             rotateBinaryTemplateImage.data[scale] = tempBinaryTemplateImage.data[rotateY * tempTemplateWidth templateImage.rows + rotateX];
   }

}


//cvShowImage(windowNameSource, rotateBinaryTemplateImage);
//cvWaitKey(0);

  cvMatchTemplate(binarySourceImage,                   }    

         matchTemplate(binarySourceImage, rotateBinaryTemplateImage, result, CV_TM_SQDIFF_NORMED); 

  //cvMatchTemplate(binarySourceImage, rotateBinaryTemplateImage, result, CV_TM_SQDIFF);  

   cvMinMaxLoc(result, CV_TM_CCOEFF_NORMED); 
         minMaxLoc(result, &minVal, NULL, &minLoc, NULL, NULL);
    printf(": %f%%\n", (int)(i * 0.5 * 100), j * 20, (1 - minVal) * 100);    

  if(minVal < 0.065) // 1 - 0.065 = 0.935 : 93.5% 

0.8)  
            {
    tempLoc.x = minLoc.x + tempTemplateWidth;
templateImage.rows;
              tempLoc.y = minLoc.y + tempTemplateHeight;
 cvRectangle(destinationImage, templateImage.cols;
              rectangle(destinationImage, minLoc, tempLoc, CV_RGB(0, 255, 0), 1, 8, 0);
  }

 }

//cvShowImage(windowNameSource, result);
//cvWaitKey(0);

cvReleaseImage(&tempBinaryTemplateImage);
cvReleaseImage(&result);

}


 // cvShowImage(windowNameSource, sourceImage);
 // cvShowImage(windowNameCoefficientOfCorrelation, result); 

cvShowImage(windowNameDestination,              imshow(windowNameDestination, destinationImage);
 key = cvWaitKey(0);


cvReleaseImage(&sourceImage);
cvReleaseImage(&templateImage);
cvReleaseImage(&graySourceImage);
cvReleaseImage(&grayTemplateImage);
cvReleaseImage(&binarySourceImage);
cvReleaseImage(&binaryTemplateImage);
cvReleaseImage(&destinationImage);

cvDestroyWindow(windowNameSource);
cvDestroyWindow(windowNameDestination);
cvDestroyWindow(windowNameCoefficientOfCorrelation);

waitKey(0);   
            }

RESULT :

Template image : (/upfiles/13747360744030719.jpg)

ResultImage: above function put rectangles around the perfect matches (angle and scale invariant) in this image ..... (/upfiles/13747356555024643.jpg)

Now, I have been trying to port the code into C++ interface. If anyone need details please let me know .

Angle and Scale Invariant Template matching

Below Function rotates the template image from 0 to 360(using for loop to rotate image matrix 20 degrees in each loop) degrees to search all matches present in all angles in source image. when I build the application , there is no errors . While debug the application I am getting below error,

 Unhandled exception at 0x74ec812f in Template_Match.exe: Microsoft C++ exception:  cv::Exception at memory location 0x002e02a8..

Someone please help me to rectify the issue.Help would be appreciated greatly.

 /// function to rotate (0 to 360 degrees) template for template matching .

   void matchTemplate()
    {
       int  j, x, y, key;

       double minVal;
       char windowNameSource[] = "Original Image";
       char windowNameDestination[] = "Result Image";
       char windowNameCoefficientOfCorrelation[] = "Coefficient of Correlation Image";

       Point minLoc;
       Point tempLoc;

         Mat templateImage = ("template.jpg");
         Mat sourceImage = ("sourceImage.jpg");

         Mat graySourceImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC1);
         Mat grayTemplateImage = cvCreateMat(templateImage.rows,templateImage.cols,CV_32FC1);
         Mat binarySourceImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC1);
         Mat binaryTemplateImage = cvCreateMat(templateImage.rows,templateImage.cols,CV_32FC1);
         Mat destinationImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC3);

         sourceImage.copyTo(destinationImage);
         cvtColor(sourceImage,graySourceImage,CV_RGB2GRAY);
         cvtColor(templateImage,grayTemplateImage,CV_RGB2GRAY);

         threshold(graySourceImage,binarySourceImage,0.8,1.0,CV_THRESH_TOZERO);
     threshold(grayTemplateImage,binaryTemplateImage,0.8,1.0,CV_THRESH_TOZERO);

        float degree = 20.0f;
        for(j = 0; j <= 18; j++) 
         {
           Mat tempBinaryTemplateImage = cvCreateMat(templateImage.rows, templateImage.cols,     CV_32FC1);
           Mat result = cvCreateMat(sourceImage.rows - templateImage.rows + 1, sourceImage.cols -templateImage.cols + 1, CV_32FC1);
           resize(binaryTemplateImage, tempBinaryTemplateImage,Size(),0.5,0.5,CV_INTER_AREA);

           Mat rotateBinaryTemplateImage = cvCreateMat(tempBinaryTemplateImage.rows, tempBinaryTemplateImage.cols, CV_32FC1);

          for(y = 0; y < templateImage.cols; y++)
            {
           for(x = 0; x < templateImage.rows; x++)
             {
                float radian = (float)j * degree * CV_PI / 180.0f;
                int scale = y * templateImage.rows + x;
                int rotateY = - sin(radian) * ((float)x - (float)templateImage.rows / 2.0f) + cos(radian) * ((float)y - (float)templateImage.cols / 2.0f) + templateImage.cols / 2;
                int rotateX = cos(radian) * ((float)x - (float)templateImage.rows / 2.0f) + sin(radian) * ((float)y - (float)templateImage.cols / 2.0f) + templateImage.rows / 2;

             if(rotateY < templateImage.cols && rotateX < templateImage.rows && rotateY >= 0 && rotateX  >= 0)
            rotateBinaryTemplateImage.data[scale] = tempBinaryTemplateImage.data[rotateY * templateImage.rows + rotateX];
                   }
                  }    

         matchTemplate(binarySourceImage, rotateBinaryTemplateImage, result, CV_TM_CCOEFF_NORMED); 
         minMaxLoc(result, &minVal, NULL, &minLoc, NULL, NULL);

         if(minVal < 0.8)  
            {
              tempLoc.x = minLoc.x + templateImage.rows;
              tempLoc.y = minLoc.y + templateImage.cols;
              rectangle(destinationImage, minLoc, tempLoc, CV_RGB(0, 255, 0), 1, 8, 0);
            }

          }
             imshow(windowNameDestination, destinationImage);
             key = waitKey(0);   
            }
click to hide/show revision 5
fixed code formating

Angle and Scale Invariant Template matching

Below Function rotates the template image from 0 to 360(using for loop to rotate image matrix 20 degrees in each loop) degrees to search all matches present in all angles in source image. when I build the application , there is no errors . While debug the application I am getting below error,

Unhandled exception at 0x74ec812f in Template_Match.exe: Microsoft C++ exception: cv::Exception at memory location 0x002e02a8..

Someone please help me to rectify the issue.Help would be appreciated greatly.

 /// function to rotate (0 to 360 degrees) template for template matching .
 void matchTemplate()
 {
     int  j, x, y, key;

     double minVal;
     char windowNameSource[] = "Original Image";
     char windowNameDestination[] = "Result Image";
     char windowNameCoefficientOfCorrelation[] = "Coefficient of Correlation Image";

     Point minLoc;
     Point tempLoc;

     Mat templateImage = ("template.jpg");
     Mat sourceImage = ("sourceImage.jpg");

     Mat graySourceImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC1);
     Mat grayTemplateImage = cvCreateMat(templateImage.rows,templateImage.cols,CV_32FC1);
     Mat binarySourceImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC1);
     Mat binaryTemplateImage = cvCreateMat(templateImage.rows,templateImage.cols,CV_32FC1);
     Mat destinationImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC3);

     sourceImage.copyTo(destinationImage);
     cvtColor(sourceImage,graySourceImage,CV_RGB2GRAY);
     cvtColor(templateImage,grayTemplateImage,CV_RGB2GRAY);

     threshold(graySourceImage,binarySourceImage,0.8,1.0,CV_THRESH_TOZERO);
     threshold(grayTemplateImage,binaryTemplateImage,0.8,1.0,CV_THRESH_TOZERO);

     float degree = 20.0f;
     for(j = 0; j <= 18; j++) 
     {
         Mat tempBinaryTemplateImage = cvCreateMat(templateImage.rows, templateImage.cols,     CV_32FC1);
         Mat result = cvCreateMat(sourceImage.rows - templateImage.rows + 1, sourceImage.cols -templateImage.cols + 1, CV_32FC1);
         resize(binaryTemplateImage, tempBinaryTemplateImage,Size(),0.5,0.5,CV_INTER_AREA);

         Mat rotateBinaryTemplateImage = cvCreateMat(tempBinaryTemplateImage.rows, tempBinaryTemplateImage.cols, CV_32FC1);

         for(y = 0; y < templateImage.cols; y++)
         {
            for(x = 0; x < templateImage.rows; x++)
             {
                float radian = (float)j * degree * CV_PI / 180.0f;
                int scale = y * templateImage.rows + x;
                int rotateY = - sin(radian) * ((float)x - (float)templateImage.rows / 2.0f) + cos(radian) * ((float)y - (float)templateImage.cols / 2.0f) + templateImage.cols / 2;
te
                int rotateX = cos(radian) * ((float)x - (float)templateImage.rows / 2.0f) + sin(radian) * ((float)y - (float)templateImage.cols / 2.0f) + templateImage.rows / 2;

temp

                if(rotateY < templateImage.cols && rotateX < templateImage.rows && rotateY >= 0 && rotateX  >= 0)
             rotateBinaryTemplateImage.data[scale] = tempBinaryTemplateImage.data[rotateY * templateImage.rows + rotateX];
             }
                  }    

 }

        matchTemplate(binarySourceImage, rotateBinaryTemplateImage, result, CV_TM_CCOEFF_NORMED); 
 CV_TM_CCOEFF_NORMED);
        minMaxLoc(result, &minVal, NULL, &minLoc, NULL, NULL);

         if(minVal < 0.8)  
    0.8)
        {
             tempLoc.x = minLoc.x + templateImage.rows;
             tempLoc.y = minLoc.y + templateImage.cols;
             rectangle(destinationImage, minLoc, tempLoc, CV_RGB(0, 255, 0), 1, 8, 0);
         }

     }
     imshow(windowNameDestination, destinationImage);
     key = waitKey(0);   
            waitKey(0);
}

Angle and Scale Invariant Template matching

Below Function rotates the template image from 0 to 360(using for loop to rotate image matrix 20 degrees in each loop) degrees to search all matches present in all angles in source image. when I build the application , there is no errors . While debug the application I am getting below error,

Unhandled exception at 0x74ec812f in Template_Match.exe: Microsoft C++ exception: cv::Exception at memory location 0x002e02a8..

Someone please help me to rectify the issue.Help would be appreciated greatly.

/// function to rotate (0 to 360 degrees) template for template matching .
void matchTemplate()
{
    int  j, x, y, key;

    double minVal;
    char windowNameSource[] = "Original Image";
    char windowNameDestination[] = "Result Image";
    char windowNameCoefficientOfCorrelation[] = "Coefficient of Correlation Image";

    Point minLoc;
    Point tempLoc;

    Mat templateImage = ("template.jpg");
imread("template.jpg");
    Mat sourceImage = ("sourceImage.jpg");
imread("sourceImage.jpg");

    Mat graySourceImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC1);
    Mat grayTemplateImage = cvCreateMat(templateImage.rows,templateImage.cols,CV_32FC1);
    Mat binarySourceImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC1);
    Mat binaryTemplateImage = cvCreateMat(templateImage.rows,templateImage.cols,CV_32FC1);
    Mat destinationImage = cvCreateMat(sourceImage.rows,sourceImage.cols,CV_32FC3);

    sourceImage.copyTo(destinationImage);
    cvtColor(sourceImage,graySourceImage,CV_RGB2GRAY);
    cvtColor(templateImage,grayTemplateImage,CV_RGB2GRAY);

    threshold(graySourceImage,binarySourceImage,0.8,1.0,CV_THRESH_TOZERO);
    threshold(grayTemplateImage,binaryTemplateImage,0.8,1.0,CV_THRESH_TOZERO);

    float degree = 20.0f;
    for(j = 0; j <= 18; j++) 
    {
        Mat tempBinaryTemplateImage = cvCreateMat(templateImage.rows, templateImage.cols,     CV_32FC1);
        Mat result = cvCreateMat(sourceImage.rows - templateImage.rows + 1, sourceImage.cols -templateImage.cols + 1, CV_32FC1);
        resize(binaryTemplateImage, tempBinaryTemplateImage,Size(),0.5,0.5,CV_INTER_AREA);

        Mat rotateBinaryTemplateImage = cvCreateMat(tempBinaryTemplateImage.rows, tempBinaryTemplateImage.cols, CV_32FC1);

        for(y = 0; y < templateImage.cols; y++)
        {
            for(x = 0; x < templateImage.rows; x++)
            {
                float radian = (float)j * degree * CV_PI / 180.0f;
                int scale = y * templateImage.rows + x;
                int rotateY = - sin(radian) * ((float)x - (float)templateImage.rows / 2.0f) + cos(radian) * ((float)y - (float)templateImage.cols / 2.0f) + te
                int rotateX = cos(radian) * ((float)x - (float)templateImage.rows / 2.0f) + sin(radian) * ((float)y - (float)templateImage.cols / 2.0f) + temp

                if(rotateY < templateImage.cols && rotateX < templateImage.rows && rotateY >= 0 && rotateX  >= 0)
                    rotateBinaryTemplateImage.data[scale] = tempBinaryTemplateImage.data[rotateY * templateImage.rows + rotateX];
            }
        }

        matchTemplate(binarySourceImage, rotateBinaryTemplateImage, result, CV_TM_CCOEFF_NORMED);
        minMaxLoc(result, &minVal, NULL, &minLoc, NULL, NULL);

        if(minVal < 0.8)
        {
            tempLoc.x = minLoc.x + templateImage.rows;
            tempLoc.y = minLoc.y + templateImage.cols;
            rectangle(destinationImage, minLoc, tempLoc, CV_RGB(0, 255, 0), 1, 8, 0);
        }

    }
    imshow(windowNameDestination, destinationImage);
    key = waitKey(0);
}