Ask Your Question
0

OpenCV with Android Template Matching

asked 2013-10-10 16:26:39 -0600

Bella gravatar image

updated 2013-10-11 01:22:32 -0600

berak gravatar image

Hello...I am trying to develop a template matching android application, I am using the Camera API preview; how can I get the template image (crop it) and save it ? do I need a database to save template images ? or just save them to a specific folder? Thanks in advance.

    private PictureCallback mPicture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.d("EyeSeeJD", "on picture taken starts ");
            jpegData = new Mat(1, data.length, CvType.CV_8UC1);
            jpegData.put(0, 0, data);
            Mat bgrMat = Highgui.imdecode(jpegData, Highgui.IMREAD_COLOR);
            Imgproc.resize(bgrMat, bgrMat, new Size(620, 344));
            Imgproc.cvtColor(bgrMat, bgrMat, Imgproc.COLOR_BGR2GRAY);
            Imgproc.GaussianBlur(bgrMat, bgrMat, new Size(15, 15), 1, 0);
            Imgproc.equalizeHist(bgrMat, bgrMat);
            Imgproc.Canny(bgrMat, bgrMat, 100, 200);
            MatOfByte matOfByte = new MatOfByte();

            // encoding to png, so that your image does not lose information
            // like with jpeg.
            Highgui.imencode(".png", bgrMat, matOfByte);
            byte[] byteArray = matOfByte.toArray();
System.out.println();

            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            Log.d("EyeSeeJD", "file get media file ");

            if (pictureFile == null) {
                Log.d("EyeSeeJD",
                        "Error creating media file, check storage permissions ");
                return;
            }

            try {
                Log.d("EyeSeeJD", "File try save ");

                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(byteArray);
                fos.close();
                Log.d("EyeSeeJD", "File saved ");

            } catch (FileNotFoundException e) {
                Log.d("EyeSeeJD", "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d("EyeSeeJD", "Error accessing file: " + e.getMessage());
            }
            mCamera.startPreview();
            Log.d("EyeSeeJD", "start view after saving * ");
        }

    };
edit retag flag offensive close merge delete

Comments

1

Hi @Bella, what did you mean by get the template image? Just get a roi from it?

Daniil Osokin gravatar imageDaniil Osokin ( 2013-10-11 02:18:10 -0600 )edit

Thanks @Daniil yes get a roi I want to make a template matching between this template (roi) and many images

Bella gravatar imageBella ( 2013-10-11 09:46:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-10-11 14:37:22 -0600

Daniil Osokin gravatar image

Hi Bella, to get a submat use Mat.submat method. You can save Mat with Highgui.imwrite.

P.S. OpenCV's JAVA API is pretty similar with C++ API: http://docs.opencv.org/java/. Also there is a tutorial "Template Matching" for quick reference.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-10-10 16:26:39 -0600

Seen: 2,566 times

Last updated: Oct 11 '13