我是 OpenCV 的新手。我做了一个应用程序,它告诉用户给定的图像是否给定了模板。我使用了这段代码。它完美匹配。但它不会检测给定图像是否没有匹配的模板。当我阅读它时,它达到了最高值并向我显示了一个错误的结果(显示在图像中的其他地方)。我阅读了有关 minVal 和 maxVal 的信息。但我仍然无法很好地理解它的作用。因为我调试了代码。每次 minVal 为 0.0 而 maxVal 为 255.0 (即使图像是否包含模板,我的意思是每次。)。我被困在这里。请帮助我只显示正确的结果。
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);
}