Ask Your Question
1

Angle and Scale Invariant Template matching

asked 2013-07-25 02:05:41 -0500

Sharath gravatar image

updated 2013-07-29 23:24:31 -0500

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

Comments

2

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.

Notas gravatar imageNotas ( 2013-07-25 02:54:06 -0500 )edit
1

You should replace your working C code by your not working cpp code.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-07-25 19:00:53 -0500 )edit

@Notas & @Mathieu Barnachon ... please see my updated question and help me to solve the issue.

Sharath gravatar imageSharath ( 2013-07-29 04:36:26 -0500 )edit

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

mrgloom gravatar imagemrgloom ( 2013-07-29 05:39:44 -0500 )edit

look in debugger if your images properly read (have size)

mrgloom gravatar imagemrgloom ( 2013-07-30 00:21:10 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-07-29 05:48:40 -0500

I can't belived this is working:

Mat templateImage = ("template.jpg");

It won't on my computer.

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

I assume copy/paste issue?

Don't use cvCreateMat with the CPP interface:

Mat graySourceImage(sourceImage.rows,sourceImage.cols,CV_32FC1);

This is more "c++" like, and a compact way of writing it.

Idem for:

sourceImage.copyTo(destinationImage); //Old
destinationImage = sourceImage.clone(); //Become

For the rotation, I suggest looking at this way, with wrapAffine), adapted from that sample.

Point center = Point( ori.cols/2, ori.rows/2 );
double angle = -50.0;
double scale = 1.;
/// Get the rotation matrix with the specifications above
Mat rot_mat( 2, 3, CV_32FC1 );
rot_mat = getRotationMatrix2D( center, angle, scale );
/// Rotate the warped image
warpAffine( ori, dst, rot_mat, ori.size() );

The more you use OpenCV function, the more you will enjoy for free the speed up available in these function. You could also use your GPU to make all the process, and thanks to these functions, you don't need CPU/GPU transfers!

After all these fixes, if it doesn't work, try to see all your intermediate images and be sure they are properly loaded.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-07-25 02:05:41 -0500

Seen: 9,501 times

Last updated: Jul 29 '13