简体   繁体   中英

Android opencv load grayscale png with 16bit depth

I am very new to opencv4android and trying to load a grayscale png image with 16bit depth.

I tried:

Mat mat2DImg = Utils.loadResource(getBaseContext(), R.drawable.image, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

But the png is read as 8bit with values in [0,255]. I tried the CV_LOAD_IMAGE_ANYDEPTH flag but same. I tried to create an alpha channel or loading the image as raw but did not manage to have it work.

I also tried to copy the image on the device and read it with imread as follow hoping that imread could load the 16bit channel:

java.lang.String filename = "/storage/emulated/0/image.png";
Mat mat2DImg = Highgui.imread(filename, 0);

When I check for file.exists() it does exist. The Mat returned is not null but it is empty.

Can anyone suggest a way to load the image properly?

I just managed to get it to work by:

1) Added in Manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/ >

This removes the empty image issue.

2) Load image with the CV_LOAD_IMAGE_ANYDEPTH flag

rgbLoadedImage = Highgui.imread(filename, Highgui.CV_LOAD_IMAGE_ANYDEPTH);

Values are now in the proper 16 bit range.

use this code it will give you a rgb image

String filename = "/storage/emulated/0/image.png";

File file = new File(filename);

Mat image= Highgui.imread(filename, 0);//temp mat
Mat mat2DImg;
if (image.width() > 0) {

        mat2DImg = new Mat(image.size(), image.type());

        Imgproc.cvtColor(image, mat2DImg, Imgproc.COLOR_BGR2RGB);// you can use Imgproc.COLOR_BGR2GRAY to get a gray image

        Log.d(TAG, "mat2DImg: " + "chans: " + image.channels() + ", ("
                + image.width() + ", " + image.height() + ")");

        image.release();
        image = null;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM