How to process an image using OpenCV onPictureTaken?

I am trying to process the image as soon as the picture is taken, i.e. in the onPictureTaken() . As far as I understand, I have to convert the byte array to the OpenCV matrix, but the whole application freezes when I try to do this. Basically everything I did was the following:

 @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 used the OpenCV4Android 3 tutorial as a basis.

Update 1: I also tried evaluating the bytes (without any success) as follows:

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

Update 2: Presumably this should work, but it is not for me.

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

And this option was not:

 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 also did not work for me:

 Mat mat = new MatOfByte(bytes); 
+5
source share
1 answer

I have some help from my colleague. He managed to fix the problem by following these steps:

 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()); 

Hope this helps all of you who are also struggling with this.

0
source

Source: https://habr.com/ru/post/1247715/


All Articles