简体   繁体   English

通过OpenCV或JavaCV进行模板匹配

[英]Template matching via OpenCV or JavaCV

I need to implement a template matching method with Java. 我需要使用Java实现模板匹配方法。 I found OpenCV and JavaCV for this problem. 我发现此问题的OpenCV和JavaCV。 To start with OpenCV I found some Tutorials at http://www.tutorialspoint.com/ (Don´t know why, but the most only create a black image). 从OpenCV开始,我在http://www.tutorialspoint.com/上找到了一些教程(不知道为什么,但是最多只能创建一个黑色图像)。 No matter... 不管...

Now I wanted to try this: 现在,我想尝试一下:

http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

But I have no Idea how I can use it with Java (OpenCV and JavaCV are furnished in Eclipse). 但是我不知道如何在Java中使用它(Eclipse中提供了OpenCV和JavaCV)。

For the case of JavaCV, I found the following Code: 对于JavaCV,我发现以下代码:

IplImage src = cvLoadImage("new.png");
IplImage tmp = cvLoadImage("old.png");

IplImage result = cvCreateImage(cvSize(src.width()-tmp.width()+1, src.height()-    tmp.height()+1), IPL_DEPTH_32F, 1);
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];

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

//Get the Max or Min Correlation Value      
cvMinMaxLoc(result, min_val, max_val, minLoc, maxLoc, null);

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

cvRectangle(src, maxLoc, point, CvScalar.WHITE, 2, 8, 0);//Draw a Rectangle for Matched   Region

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

But however I pass the files I get a NullPointerException. 但是,尽管我传递了文件,但我得到了NullPointerException。

Has anyone an example how I can use template matching with OpenCV or JavaCV or is there a simple approach doing this task without any library? 有没有人举过一个例子,说明如何将模板与OpenCV或JavaCV配合使用,或者有一种简单的方法无需任何库即可完成此任务?

Personally I try to do everything with OpenCV's Java bindings if possible. 我个人尽可能尝试使用OpenCV的Java绑定进行所有操作。 I fall back to JavaCV when something doesn't work with OpenCV's Java bindings. 当某些内容无法与OpenCV的Java绑定一起使用时,我会回到JavaCV。 JavaCV seems nice and it's developer seems to put out new releases soon after OpenCV releases, but in the long run my guess is that OpenCV's java bindings will be better supported. JavaCV看起来不错,并且它的开发人员似乎在OpenCV发布后不久就发布了新版本,但从长远来看,我猜测是将更好地支持OpenCV的Java绑定。

The only area I've found so far that doesn't work in OpenCV are some facial recognition and classification features. 到目前为止,我发现唯一在OpenCV中不起作用的区域是一些面部识别和分类功能。

Here's a little OpenCV Java for Template matching. 这是用于模板匹配的一些OpenCV Java。 I've left out the loop to swap in multiple templates and find the best match by looking at the results in mmr. 我省略了循环以交换多个模板,并通过查看mmr中的结果来找到最佳匹配。

Mat mat = ...
Mat matTemplate = ...
// Create the result matrix
int resultCols = mat.cols() - matTemplate.cols() + 1;
int resultRows = mat.rows() - matTemplate.rows() + 1;
if ( resultCols > 0 && resultRows > 0 ) {   
Mat result = new Mat(resultRows, resultCols, CvType.CV_8UC1);
// Do the Matching
Imgproc.matchTemplate(mat, matTemplate, result, Imgproc.TM_CCOEFF_NORMED);
//  Normalize???
// Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);

I'm sure you can find some good resources on how to with Template Matching with OpenCV in Java. 我敢肯定,您可以找到有关Java中OpenCV模板匹配方法的一些很好的资源。 I know I did. 我知道我做到了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM