Java openCV - after using Imgproc.matchTemplate method, how do i examine the result?
Asked Answered
T

2

5

i am calling:

Imgproc.matchTemplate(image, templ, result, 0);

and the result of the match is in a Mat instance. I couldn't find any documentation for this class. If i understand right, the result contains a matrix of probabilities. How can i find the Maximum of the probabilities? I can't even understand how a Mat instance looks like and what it contains.

Thanks Eyal

Tetherball answered 12/3, 2012 at 12:27 Comment(3)
I think this is the document you are after: Doc. And here is some sample code in C++: Sample.Grady
Yes, i have seen this documentation, i just wonder how i JAVA i can find the maximum value in the result (which is a Mat instance)Tetherball
A quick way to find the maximum value would be using cv::minMaxLoc function, which I'm sure you can find a Java equivalent. Even if you can't find a Java function to search for max value, write a function to find the max value is not a big deal.Grady
T
3

In order to test the results, one should use the function minMaxLoc that is located inside the class Core. The method returns an instance of MinMaxLocResult, and it has many options inside.

Tetherball answered 13/3, 2012 at 18:32 Comment(0)
C
8

lena.png:

enter image description here

pattern.png:

enter image description here

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

        Mat img = Highgui.imread(inFile);
        Mat templ = Highgui.imread(templateFile);

        // / 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());
        Highgui.imwrite("out2.png", result);

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

        Point matchLoc;
        if (match_method == Imgproc.TM_SQDIFF
                || match_method == Imgproc.TM_SQDIFF_NORMED) {
            matchLoc = mmr.minLoc;
            System.out.println(mmr.minVal);
        } else {
            matchLoc = mmr.maxLoc;
            System.out.println(mmr.maxVal);
        }

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

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

    }
}

public class TemplateMatching {
    public static void main(String[] args) {
        System.loadLibrary("opencv_java249");
        new MatchingDemo().run("lena.png", "pattern.png", "output.png", Imgproc.TM_CCOEFF);
    }
}
Contemplation answered 17/10, 2014 at 6:25 Comment(0)
T
3

In order to test the results, one should use the function minMaxLoc that is located inside the class Core. The method returns an instance of MinMaxLocResult, and it has many options inside.

Tetherball answered 13/3, 2012 at 18:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.