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

空扰寡人 提交于 2019-12-25 06:31:29

问题


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?


回答1:


Hey see this example that I did with Javacv

public class Test {
static Image image;

public static void main(String[] args) throws Exception {
    int width = Integer.parseInt(args[3]);
    int height = Integer.parseInt(args[4]);

    IplImage src = cvLoadImage(
            args[0], 0);
    IplImage tmp = cvLoadImage(
            args[1], 0);

    IplImage result = cvCreateImage(
            cvSize(src.width() - tmp.width() + 1,
                    src.height() - tmp.height() + 1), IPL_DEPTH_32F, src.nChannels());

    cvZero(result);

    // 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];
    DoublePointer min_val = new DoublePointer();
    DoublePointer max_val = new DoublePointer();

    CvPoint minLoc = new CvPoint();
    CvPoint maxLoc = new CvPoint();

    cvMinMaxLoc(result, min_val, max_val, minLoc, maxLoc, null);

    // Get the Max or Min Correlation Value
    // System.out.println(Arrays.toString(min_val));
    // System.out.println(Arrays.toString(max_val));

    CvPoint point = new CvPoint();
    point.x(maxLoc.x() + tmp.width());
    point.y(maxLoc.y() + tmp.height());
    // cvMinMaxLoc(src, min_val, max_val,0,0,result);

    cvRectangle(src, maxLoc, point, CvScalar.RED, 2, 8, 0);// Draw a
                                                            // Rectangle for
                                                            // Matched
                                                            // Region
    CvRect rect = new CvRect();
    rect.x(maxLoc.x());
    rect.y(maxLoc.y());
    rect.width(tmp.width() + width);
    rect.height(tmp.width() + height);
    cvSetImageROI(src, rect);
    IplImage imageNew = cvCreateImage(cvGetSize(src), src.depth(),
            src.nChannels());
    cvCopy(src, imageNew);
    cvSaveImage(args[2], imageNew);

    cvShowImage("Lena Image", src);
    cvWaitKey(0);
    cvReleaseImage(src);
    cvReleaseImage(tmp);
    cvReleaseImage(result);

}

full reference here we need 4 default parameters like this "C:\Users\Waldema\Desktop\bg.jpg" "C:\Users\Waldema\Desktop\logosiemens.jpg" "C:\Users\Waldema\Desktop\imageToFind.jpg" 100 200

configurable in the Run configurations of common IDEs. I think that will help




回答2:


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.



来源:https://stackoverflow.com/questions/21292991/finding-image-in-specific-area-in-image-using-matching-template-in-javacv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!