Java中带有openCV的matchTemplate [英] matchTemplate with openCV in java
问题描述
我有这样的代码:
Mat img = Highgui.imread(inFile);
Mat templ = Highgui.imread(templateFile);
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
Imgproc.matchTemplate(img, templ, result, Imgproc.TM_CCOEFF);
/////Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
for (int i = 0; i < result_rows; i++)
for (int j = 0; j < result_cols; j++)
if(result.get(i, j)[0]>?)
//match!
我需要解析输入图像以查找模板图像的多次出现.我想要这样的结果:
I need to parse the input image to find multiple occurrencies of the template image. I want to have a result like this:
result[0][0]= 15%
result[0][1]= 17%
result[x][y]= 47%
如果我使用TM_COEFF,则所有结果均为[-xxxxxxxx.xxx,+ xxxxxxxx.xxx]
If i use TM_COEFF all results are [-xxxxxxxx.xxx,+xxxxxxxx.xxx]
如果我使用TM_SQDIFF,则所有结果均为xxxxxxxx.xxx
If i use TM_SQDIFF all results are xxxxxxxx.xxx
如果我使用TM_CCORR,则所有结果均为xxxxxxxx.xxx
If i use TM_CCORR all results are xxxxxxxx.xxx
如何检测匹配或不匹配? if的正确条件是什么? 如果我将矩阵归一化,则应用程序会将值设置为1,并且我将无法检测到模板是否未存储到图像中(全部不匹配).
How can i detect a match or a mismatch? What is the right condition into the if? If i normalized the matrix the application set a value to 1 and i can't detect if the template isn't stored into the image (all mismatch).
预先感谢
推荐答案
您可以在方法名称后附加"_NORMED"(例如,在C ++中为CV_TM_COEFF_NORMED;在Java中可能略有不同),以获取适合您目的的值
You can append "_NORMED" to the method names (For instance: CV_TM_COEFF_NORMED in C++; could be slightly different in Java) to get a sensible value for your purpose.
明智"是指您将获得0到1范围内的值,该值可以乘以100以达到您的目的.
By 'sensible', I mean that you will get values in the range of 0 to 1 which can be multiplied by 100 for your purpose.
注意:对于CV_TM_SQDIFF_NORMED,它将在-1到0的范围内,并且您必须从1中减去该值才能理解它,因为在此方法中使用的是最低值.
Note: For CV_TM_SQDIFF_NORMED, it will be in the range -1 to 0, and you will have to subtract the value from 1 in order to make sense of it, because the lowest value if used in this method.
提示:您可以使用Java的minMaxLoc()等效项来获取最小值和最大值.与matchtemplate结合使用时,此功能非常有用. 我相信位于类Core内的"minMaxLoc".
Tip: you can use the java equivalent of minMaxLoc() in order to get the minimum and maximum values. It's very useful when used in conjunction with matchtemplate. I believe 'minMaxLoc' that is located inside the class Core.
这是一个C ++实现:
Here's a C++ implementation:
matchTemplate( input_mat, template_mat, result_mat, method_NORMED );
double minVal, maxVal;
double percentage;
Point minLoc; Point maxLoc;
minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
if( method_NORMED == CV_TM_SQDIFF_NORMED )
{
percentage=1-minVal;
}
else
{
percentage=maxVal;
}
有用的C ++文档: 匹配模板描述以及可用方法: http://docs.opencv.org/modules /imgproc/doc/object_detection.html MinMaxLoc文档: http://docs.opencv.org/modules/core /doc/operations_on_arrays.html?highlight=minmaxloc#minmaxloc
Useful C++ docs: Match template description along with available methods: http://docs.opencv.org/modules/imgproc/doc/object_detection.html MinMaxLoc documentation: http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=minmaxloc#minmaxloc
这篇关于Java中带有openCV的matchTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!