Example usage for org.opencv.android Utils bitmapToMat

List of usage examples for org.opencv.android Utils bitmapToMat

Introduction

In this page you can find the example usage for org.opencv.android Utils bitmapToMat.

Prototype

public static void bitmapToMat(Bitmap bmp, Mat mat) 

Source Link

Document

Short form of the bitmapToMat(bmp, mat, unPremultiplyAlpha=false).

Usage

From source file:ve.ucv.ciens.ccg.nxtar.MainActivity.java

License:Apache License

@Override
public CalibrationData findCalibrationPattern(byte[] frame) {
    if (ocvOn) {//from   ww w  .j ava 2  s.  c  o m
        boolean found;
        float points[] = new float[ProjectConstants.CALIBRATION_PATTERN_POINTS * 2];
        Bitmap tFrame, mFrame;
        Mat inImg = new Mat(), outImg = new Mat();
        CalibrationData data = new CalibrationData();

        // Decode the input frame and convert it to an OpenCV Matrix.
        tFrame = BitmapFactory.decodeByteArray(frame, 0, frame.length);
        Utils.bitmapToMat(tFrame, inImg);

        // Attempt to find the calibration pattern in the input frame.
        found = findCalibrationPattern(inImg.getNativeObjAddr(), outImg.getNativeObjAddr(), points);

        // Encode the output image as a JPEG image.
        mFrame = Bitmap.createBitmap(outImg.cols(), outImg.rows(), Bitmap.Config.RGB_565);
        Utils.matToBitmap(outImg, mFrame);
        mFrame.compress(CompressFormat.JPEG, 100, outputStream);

        // Prepare the output data structure.
        data.outFrame = outputStream.toByteArray();
        data.calibrationPoints = found ? points : null;

        // Clean up memory.
        tFrame.recycle();
        mFrame.recycle();
        outputStream.reset();

        return data;

    } else {
        Gdx.app.debug(TAG, CLASS_NAME + ".findCalibrationPattern(): OpenCV is not ready or failed to load.");
        return null;
    }
}

From source file:ve.ucv.ciens.ccg.nxtar.MainActivity.java

License:Apache License

@Override
public void calibrateCamera(float[][] calibrationSamples, byte[] frame) {
    if (ocvOn) {//w ww  . jav  a 2  s . c o  m
        float[] calibrationPoints = new float[ProjectConstants.CALIBRATION_PATTERN_POINTS * 2
                * ProjectConstants.CALIBRATION_SAMPLES];
        int w = ProjectConstants.CALIBRATION_PATTERN_POINTS * 2;
        Bitmap tFrame;
        Mat inImg = new Mat();

        // Save the calibration points on a one dimensional array for easier parameter passing
        // to the native code.
        for (int i = 0; i < ProjectConstants.CALIBRATION_SAMPLES; i++) {
            for (int j = 0, p = 0; j < ProjectConstants.CALIBRATION_PATTERN_POINTS; j++, p += 2) {
                calibrationPoints[p + (w * i)] = calibrationSamples[i][p];
                calibrationPoints[(p + 1) + (w * i)] = calibrationSamples[i][p + 1];
            }
        }

        // Decode the input image and convert it to an OpenCV matrix.
        tFrame = BitmapFactory.decodeByteArray(frame, 0, frame.length);
        Utils.bitmapToMat(tFrame, inImg);

        // Attempt to obtain the camera parameters.
        double error = calibrateCameraParameters(cameraMatrix.getNativeObjAddr(),
                distortionCoeffs.getNativeObjAddr(), inImg.getNativeObjAddr(), calibrationPoints);
        Gdx.app.log(TAG,
                CLASS_NAME + "calibrateCamera(): calibrateCameraParameters retured " + Double.toString(error));
        cameraCalibrated = true;

    } else {
        Gdx.app.debug(TAG, CLASS_NAME + ".calibrateCamera(): OpenCV is not ready or failed to load.");
    }
}

From source file:ve.ucv.ciens.ccg.nxtar.MainActivity.java

License:Apache License

@Override
public byte[] undistortFrame(byte[] frame) {
    if (ocvOn) {//from www. ja va 2s  . c o  m
        if (cameraCalibrated) {
            byte undistortedFrame[];
            Bitmap tFrame, mFrame;
            Mat inImg = new Mat(), outImg = new Mat();

            // Decode the input frame and convert it to an OpenCV Matrix.
            tFrame = BitmapFactory.decodeByteArray(frame, 0, frame.length);
            Utils.bitmapToMat(tFrame, inImg);

            // Apply the undistort correction to the input frame.
            Imgproc.undistort(inImg, outImg, cameraMatrix, distortionCoeffs);

            // Encode the output image as a JPEG image.
            mFrame = Bitmap.createBitmap(outImg.cols(), outImg.rows(), Bitmap.Config.RGB_565);
            Utils.matToBitmap(outImg, mFrame);
            mFrame.compress(CompressFormat.JPEG, 100, outputStream);

            // Prepare the return frame.
            undistortedFrame = outputStream.toByteArray();

            // Clean up memory.
            tFrame.recycle();
            mFrame.recycle();
            outputStream.reset();

            return undistortedFrame;

        } else {
            Gdx.app.debug(TAG, CLASS_NAME + ".undistortFrame(): Camera has not been calibrated.");
            return null;
        }
    } else {
        Gdx.app.debug(TAG, CLASS_NAME + ".undistortFrame(): OpenCV is not ready or failed to load.");
        return null;
    }
}