Home Android - OpenCV Template Matching with threshold
Reply: 1

Android - OpenCV Template Matching with threshold

cacev000
1#
cacev000 Published in 2017-12-07 20:07:00Z

I am new to OpenCV and I have been looking at tutorials plus the questions around here but I have not been able to understand and set a threshold to the template matching.

This is the code I am currently using. Function that will be triggered when picture selected

BitmapDrawable drawable = (BitmapDrawable) imgView.getDrawable();
Bitmap viewBitmap = drawable.getBitmap();
Bitmap bitmapMatch = BitmapFactory.decodeResource(getResources(), R.drawable.template_1);

run(viewBitmap, bitmapMatch, "result.png", Imgproc.TM_CCOEFF_NORMED);

Run function

public void run(Bitmap inFile, Bitmap templateFile, String outFile, int match_method) {
        System.out.println("\nRunning Template Matching");

        Mat img = new Mat();
        Utils.bitmapToMat(inFile, img);
        Mat templ = new Mat();
        Utils.bitmapToMat(templateFile, templ);

        // / Create the result matrix
        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);

        // / Do the Matching and Normalize
        Imgproc.matchTemplate(img, templ, result, match_method);
//        Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

        // / Localizing the best match with minMaxLoc
        Core.MinMaxLocResult mmr = Core.minMaxLoc(result);

        Point matchLoc;
        if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
            matchLoc = mmr.minLoc;
        } else {
            matchLoc = mmr.maxLoc;
        }

        Log.d(TAG, "point: " + mmr.maxVal);

        // / Show me what you got
//        Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
//                matchLoc.y + templ.rows()), new Scalar(255, 255, 255));

        // Save the visualized detection.
        System.out.println("Writing "+ outFile);

        if (match >= 0.8)
            SaveImage(img,outFile);
        else
            Log.d(TAG, "No Match Found");
    }

I would like to add a threshold at the if statement if (match >= 0.8) in order to save the image if the match is equal or over the threshold (0.8). If not, the image will not be saved.

Please help and thank you.

MFisherKDX
2#
MFisherKDX Reply to 2017-12-07 20:27:29Z

Core.MinMaxLocResult contains maxLoc, maxVal, minLoc, and minVal values. Just assign match=mmr.maxVal or match=mmr.minVal depending on context.

Alternatively, you could index in the result Mat to get your value and then test if it's over 0.8 as so:

double[] resultVal = result.get(matchLoc.y, matchLoc.x);
if (resultVal[0] >= 0.8) ...
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301242 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO