Template Matching in Android using openCV
Asked Answered
I

1

4

I'm trying to match an image with the camera input in Android using template matching. When i try this with static 2 images like in here: OpenCV Template Matching example in Android, everything works just fine. But when I try to use the captured images from the camera, I do not get the correct result. Following is the code that I have written:

  String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();


                Mat img = Highgui.imread(baseDir + "/mediaAppPhotos/img2.png");
                Mat templ = Highgui.imread(baseDir+ "/mediaAppPhotos/chars.png");


                int result_cols = img.cols() - templ.cols() + 1;
                int result_rows = img.rows() - templ.rows() + 1;
                Mat result = new Mat(result_cols, result_rows, CvType.CV_32FC1);

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

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

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

                // / 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 " + baseDir+ "/mediaAppPhotos/result.png");
                Highgui.imwrite(baseDir + "/mediaAppPhotos/result.png", img);

I want to this template matching to work when the image is captured from the camera as well. Any help is greatly appreciated!

Instantaneous answered 18/10, 2013 at 15:39 Comment(5)
It would help if you could describe what is wrong with the camera image result.Prologue
If I cut off a part of the source image and use it as the template image to do the matching, then it works fine. But if I use the same template to match the same source image, captured with a slight height or position change, then it does not work!Instantaneous
Make sure your template is smaller than the image you are matching.Writer
Good point, may be a memory issue case. Anything useful in the logs ?Gershon
@Writer : can you please provide some suggestions on this question ?Polymeric
I
0

maybe is like this:

https://play.google.com/store/apps/details?id=in.mustafaak.imagematcher&hl=es_419

code available in github

Investigation answered 20/5, 2015 at 21:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.