Search code examples
javaopencvimage-processingjavacvtemplate-matching

Finding image in specific area in image using Matching Template in javacv


I am trying to find the location of image inside a specific area in another. I am using javacv to do this issue. But my code is giving an error when executing cvMatchTemplate function. I think I am miss using cvSetImageROI.

This is how I am using it:

    public static void main(String c[]) {

    IplImage src = cvLoadImage("test.jpg", 0);
    IplImage tmp = cvLoadImage("tmp.png", 0);
    IplImage result = cvCreateImage(cvSize(src.width() - tmp.width() + 1, src.height() - tmp.height() + 1),
                           IPL_DEPTH_32F,1);
    cvZero(result);

    cvSetImageROI(src, new CvRect(22, 50, 30, 30));
    cvSetImageROI(result, new CvRect(22, 50, 30, 30));

    //Match Template Function from OpenCV
    cvMatchTemplate(src, tmp, result, CV_TM_CCORR_NORMED);

    double[] min_val = new double[2];
    double[] max_val = new double[2];
    CvPoint minLoc = new CvPoint();
    CvPoint maxLoc = new CvPoint();

    cvMinMaxLoc(result, min_val, max_val, minLoc, maxLoc,
            null);
    CvPoint point = new CvPoint();
    point.x(maxLoc.x() + tmp.width());
    point.y(maxLoc.y() + tmp.height());
    cvRectangle(src, maxLoc, point, CvScalar.RED, 2, 8, 0);
    cvShowImage("Lena Image", src);
    cvWaitKey(0);
    cvReleaseImage(src);
    cvReleaseImage(tmp);
    cvReleaseImage(result);
}

This is the error:

    OpenCV Error: Assertion failed (result.size() == cv::Size(std::abs(img.cols - templ.cols) 
    + 1,std::abs(img.rows - templ.rows) + 1) && result.type() == CV_32F) in unknown function,
    file ..\..\..\src\opencv\modules\imgproc\src\templmatch.cpp, line 384

Any Help?


Solution

  • I found the problem it was in setting the roi of the result image, this is the wrong line :

       cvSetImageROI(result, new CvRect(22, 50, 30, 30));
    

    It should be like this :

       cvSetImageROI(result, new CvRect(22, 50, 30 - tmp.width() + 1, 30 - tmp.height() + 1));
    

    I am not sure why but I think it is because cvMatchTemplate function takes the result dimension equal to the source dimensions minus the template dimensions plus one pixel.