Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

java - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...