【问题标题】:How to get the exact match Using Template Matching in OpenCV (in Android)?如何使用 OpenCV 中的模板匹配(在 Android 中)获得完全匹配?
【发布时间】:2013-08-25 12:16:02
【问题描述】:

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

【问题讨论】:

    标签: android opencv template-matching


    【解决方案1】:

    行:

    Core.normalize(mResult, mResult8u, 0, 255, Core.NORM_MINMAX, CvType.CV_8U);
    

    标准化结果。它使任何范围等于 0-255。 如果您想要真正的相关值,请使用非标准化 mResult。

    【讨论】:

    • 谢谢。您的意思是将 mResult 传递给 Core.minMaxLoc() ?我试过了。但我得到了相同的结果。
    • 你有没有查看坐标 matchLoc 中矩阵 mResult 中的值是什么? TM_SQDIFF_NORMED 应在 0-1 范围内给出结果。
    • 不。我总是得到0-255。我想我在这里错过了一些东西。但我不知道是什么...
    • 如果 TM_SQDIFF_NORMED 那么范围将是元素值的所有范围,因为它是规范的 :) 。当您使用 TM_SQDIFF(或其他非规范指标)时,范围是否相同? mResult 有什么类型的矩阵元素?它应该是 CV_32FC1。
    猜你喜欢
    • 2018-10-02
    • 2015-04-20
    • 2013-02-28
    • 2013-10-27
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多