5

I'm currently trying to migrate a bit of legacy code from iPhone to Android. This code uses the OpenCV library to do some image processing. And I cannot understand how to do the conversion between Mat and Android Bitmap classes.

This code shows a very simplified example which loads a bitmap into a Mat, then converts it back to Bitmap. The resulting image looks weird - it's filled with blue and white pixels. And the original is a normal PNG image...

  Mat img = Utils.loadResource(context, resId);
  Bitmap tmp = Bitmap.createBitmap(img.rows(), img.cols(),  
  Bitmap.Config.ARGB_8888);               
  Utils.matToBitmap(img, tmp);
Anton
  • 4,395
  • 7
  • 32
  • 46

1 Answers1

14

Currently matToBitmap is a bit buggy, I've read they intend to fix it in a future release.

I can show it how I worked around it for a color image:

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

Basically i perform that space color conversion first or like you said the result will be a weird blend of random colors.

Answer if it works, I think it does.

greven
  • 633
  • 7
  • 18
  • How do you get the `resId`? I'm trying to convert a camera-intent-obtained image from bmp to Mat, process, then back again. And thus all the information available regarding the bmp is the path name, and the bmp itself. – Noha Kareem Jan 29 '13 at 06:10
  • @user1446598 Are you using openCV in Android? – greven Feb 02 '13 at 02:57
  • Yes. The problem got resolved - I have found a SO post, that I can't find at the moment, that helped with the procedure. I still don't know how to obtain a resID from a camera-captured photo though (unlike with drawable items), but the approach suggested used the path name instead. – Noha Kareem Feb 09 '13 at 15:09
  • What do you mean that matToBitmap is buggy? Maybe they solved the problem by now. Anyway Anton was having errors because his code was wrong, not due to a bug. – Giuseppe Dini Jan 03 '16 at 21:23