Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process an image with OpenCV onPictureTaken?

I've been trying to process an image as soon as the picture was taken i.e. in the onPictureTaken() callback. As it was to my understanding I should convert the byte array to an OpenCV matrix, but the entire app freezes when I try to do that. Basically all I did was this:

@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
    Log.w(TAG, "picture taken!");

    if (bytes != null) {
        Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        Mat matImage = new Mat();

        // This is where my app freezes.
        Utils.bitmapToMat(image, matImage);

        Log.w(TAG, matImage.dump());
    }

    mCamera.startPreview();
    mCamera.setPreviewCallback(this);
}

Does anyone know why it freezes and how to solve it?

Note: I've used the OpenCV4Android tutorial 3 as a base.

Update 1: I've also tried to parste the bytes (without any success) as follows:

Mat mat = Imgcodecs.imdecode(
    new MatOfByte(bytes), 
    Imgcodecs.CV_LOAD_IMAGE_UNCHANGED
);

Update 2: Supposedly this should work, it did not for me though.

Mat mat = new Mat(1, bytes.length, CvType.CV_8UC3);
mat.put(0, 0, bytes);

Nor did this variant:

Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC1);
mat.put(0, 0, bytes);

Update 3: This did not work for me either:

Mat mat = new MatOfByte(bytes);
like image 781
Mathijs Avatar asked Nov 08 '22 16:11

Mathijs


1 Answers

I've got some help of a colleague of mine. He managed to fix the problem by doing the following:

BitmapFactory.Options opts = new BitmapFactory.Options(); // This was missing.
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);

Mat mat = new Mat();
Utils.bitmapToMat(bitmap, mat);

// Note: when the matrix is to large mat.dump() might also freeze your app.
Log.w(TAG, mat.size()); 

Hopefully this'll help all of you who are also struggling with this.

like image 62
Mathijs Avatar answered Nov 14 '22 23:11

Mathijs