Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV with Android Template Matching

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 * ");
        }

    };
click to hide/show revision 2
retagged

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

berak gravatar image

OpenCV with Android Template Matching

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 * ");
        }

    };