使用javacv中的匹配模板在图像中的特定区域中查找图像 [英] Finding image in specific area in image using Matching Template in javacv

查看:412
本文介绍了使用javacv中的匹配模板在图像中的特定区域中查找图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在另一个特定区域内找到图像的位置。我正在使用javacv来解决这个问题。但是我的代码在执行 cvMatchTemplate 函数时出错。我想我错过了使用 cvSetImageROI

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);
}

这是错误:

    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

任何帮助?

推荐答案

我发现设置结果图像的roi时遇到了问题,这是错误的行:

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));

它应该是这样的:

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

我不知道为什么,但我认为这是因为 cvMatchTemplate 功能需要结果维度等于源维度减去模板维度加上一个像素。

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.

这篇关于使用javacv中的匹配模板在图像中的特定区域中查找图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆