Ank
Ank

Reputation: 1904

Convert OpenCV mat to Android Bitmap

I am trying to convert an OpenCV mat to android Bitmap but that gives me images with a bluish tint(yellow colour turns to blue)! Even though I am not doing any processing on the image! I have no clue why this is happening. Below is some relevant code:

File file = new File(imgDecodableString);
image = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_COLOR);
resultBitmap = Bitmap.createBitmap(image.cols(),  image.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(image, resultBitmap);
Bitmap mResult = resultBitmap;
ImageView imgView = (ImageView) findViewById(R.id.imgView);
imgView.setImageBitmap(mResult);
//imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));

I am new to android app development so I might be missing something simple. Thanks for help!

EDIT:

I am uploading images for reference.

Original image

Image I am getting

Upvotes: 2

Views: 8060

Answers (2)

Sujith Royal
Sujith Royal

Reputation: 822

Based on the given suggestions, I made a function which converts Mat to Bitmap. This function works perfectly.!

private static Bitmap convertMatToBitMap(Mat input){
    Bitmap bmp = null;
    Mat rgb = new Mat();
    Imgproc.cvtColor(input, rgb, Imgproc.COLOR_BGR2RGB);

    try {
        bmp = Bitmap.createBitmap(rgb.cols(), rgb.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(rgb, bmp);
    }
    catch (CvException e){
        Log.d("Exception",e.getMessage());
    }
    return bmp;
}

Upvotes: 5

ZdaR
ZdaR

Reputation: 22954

As suspected, the issue is with the RGB color convention, Android follows RGB color convention, but OpenCV follows BGR color convention, You can rectify it using Imgproc.cvtColor(mat, Imgproc.COLOR_BGR2RGBA), before displaying it in the ImageView.

Upvotes: 3

Related Questions