Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap to Mat gives wrong colors back

So I make a bitmap from a blob with the next code:

byte[] blob = contact.getMP();
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Bitmap scalen = Bitmap.createScaledBitmap(bitmap, 320, 240, false);

and it gives back the next output, which is good

enter image description here

Then I do the following to make the bitmap into a Mat, but then my colors just change...

//Mat ImageMat = new Mat();
Mat ImageMat = new Mat(320, 240, CvType.CV_32F);
Utils.bitmapToMat(scalen, ImageMat);

I have no idea why, nor another way to make the bitmap into a Mat. What is wrong? enter image description here

like image 472
user1393500 Avatar asked May 18 '13 22:05

user1393500


1 Answers

The format of color channels in Android Bitmap are RGB But in opencv Mat, the channels are BGR by default.

So when you do Utils.bitmapToMat(), [B,G,R] values are stored in [R,G,B] channels. The red and blue channels are interchanged.

One possible solution is to apply cvtcolor on the opencv Mat you got as below: Imgproc.cvtColor(ImageMat, ImageMat, Imgproc.COLOR_BGR2RGB);

It worked for me.

like image 137
VK_nandi Avatar answered Sep 29 '22 07:09

VK_nandi