I'm new to OpenCV. I doing a application which tells user that given image has given template or not. I used this code. It does matching perfectly. But it doesn't detect if the given image doesn't have the matching template. As I read it goes to the highest value and show me a false result(Shows somewhere else in image). I read about minVal and maxVal. But still I couldn't understand well even what it does. Because I debugged the code. each time minVal is 0.0 and maxVal is 255.0 (even image contains template or not, I mean everytime.). I'm stucked in here. Please help me to show only correct results.
Utils.bitmapToMat(bmp2, mFind);
Utils.bitmapToMat(bmp1, Input);
Utils.bitmapToMat(bmp3, mResult9u);
Imgproc.matchTemplate(mFind, Input, mResult, matcher);
Core.normalize(mResult, mResult8u, 0, 255, Core.NORM_MINMAX, CvType.CV_8U);
Point matchLoc;
MinMaxLocResult minMaxLoc = Core.minMaxLoc(mResult8u);
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if (matcher == Imgproc.TM_SQDIFF || matcher == Imgproc.TM_SQDIFF_NORMED)
{
matchLoc = minMaxLoc.minLoc;
}
else
{
matchLoc = minMaxLoc.maxLoc;
}
float thresholdMatchForMin = 0.02f;
float thresholdMatchForMax = 0.08f;
// minMaxLoc.minVal < thresholdMatchForMin ||
if (minMaxLoc.maxVal > thresholdMatchForMax)
{
/// Show me what you got
Core.rectangle(mResult9u, matchLoc,
new Point(matchLoc.x + Input.cols(), matchLoc.y + Input.rows()), new Scalar(0, 255, 255, 0));
Utils.matToBitmap(mFind, bmp2);
Utils.matToBitmap(mResult9u, bmp3);
Paint paint = new Paint();
paint.setColor(Color.RED);
Canvas canvas = new Canvas(bmp2);
canvas.drawRect(new Rect((int) matchLoc.x, (int) matchLoc.y, (int) (matchLoc.x + Input.cols()),
(int) (matchLoc.y + Input.rows())), paint);
}