Correct way to convert between Bitmap and Mat in OpenCV on Android?

I'm currently trying to port a bit of outdated code from iPhone to Android. This code uses the OpenCV library to process some images. And I can't figure out how to do the conversion of the Mat and Android Bitmap classes.

This code shows a very simplified example that loads a bitmap into Mat and then converts it back to Bitmap. The resulting image looks strange - it is filled with blue and white pixels. And the original is a regular PNG image ...

  Mat img = Utils.loadResource(context, resId);
  Bitmap tmp = Bitmap.createBitmap(img.rows(), img.cols(),  
  Bitmap.Config.ARGB_8888);               
  Utils.matToBitmap(img, tmp);
+5
source share
1 answer

Currently matToBitmap is a bit buggy, I read that I intend to fix it in a future version.

, :

mMat = Utils.loadResource(this, resId, Highgui.CV_LOAD_IMAGE_COLOR);
Imgproc.cvtColor(mMat, result, Imgproc.COLOR_RGB2BGRA);
bmp = Bitmap.createBitmap(result.cols(), result.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(result, bmp);
mImageView.setImageBitmap(bmp);

, , .

, , , .

+13

All Articles