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 = imread("template.jpg");
Mat sourceImage = 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);
}
Tell us about the specific errors that you get. Also, it would help if you had correct indentation! The lines in the for loops are all over the place, makes reading the code harder than necessary.
You should replace your working C code by your not working cpp code.
@Notas & @Mathieu Barnachon ... please see my updated question and help me to solve the issue.
you can check rotation and scale invariant method using logpolar transform code in python http://stackoverflow.com/questions/16294700/fft-based-image-registration-in-python
look in debugger if your images properly read (have size)