0

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);
        }
ssdehero
  • 786
  • 1
  • 11
  • 26

1 Answers1

0

The line:

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

normalizes the result. It makes any range equals to 0-255. If you want real correlation values use non normalized mResult.

Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42
  • Thank you. you mean pass mResult to Core.minMaxLoc() ? I tried. But I got same result. – ssdehero Aug 22 '13 at 18:30
  • Have you look what value is in matrix mResult in coordinates matchLoc? TM_SQDIFF_NORMED should give result in range 0-1. – Andrey Smorodov Aug 22 '13 at 18:42
  • nope. Always i'm getting 0-255. I think I have missed something here. But I don't know what... – ssdehero Aug 23 '13 at 16:31
  • If TM_SQDIFF_NORMED then range will be all range of element values, because it normed :) . When you use TM_SQDIFF (or other not normed metrics) is range the same? And what type of matrix element mResult has ? it should be CV_32FC1. – Andrey Smorodov Aug 23 '13 at 16:48