简体   繁体   English

Android OpenCV以16位深度加载灰度PNG

[英]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. 我对opencv4android非常陌生,并尝试加载16位深度的灰度png图像。

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]. 但是png被读取为8bit,其值在[0,255]中。 I tried the CV_LOAD_IMAGE_ANYDEPTH flag but same. 我尝试了CV_LOAD_IMAGE_ANYDEPTH标志,但相同。 I tried to create an alpha channel or loading the image as raw but did not manage to have it work. 我试图创建一个Alpha通道或将图像作为原始图像加载,但没有设法使其正常工作。

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: 我还尝试将图像复制到设备上并以imread读取,如下所示,希望imread可以加载16位通道:

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

When I check for file.exists() it does exist. 当我检查file.exists()它确实存在。 The Mat returned is not null but it is empty. 返回的Mat不为null,但为空。

Can anyone suggest a way to load the image properly? 谁能建议一种正确加载图像的方法?

I just managed to get it to work by: 我只是设法通过以下方式使其起作用:

1) Added in Manifest: 1)在清单中添加:

<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 2)使用CV_LOAD_IMAGE_ANYDEPTH标志加载图像

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

Values are now in the proper 16 bit range. 现在值在正确的16位范围内。

use this code it will give you a rgb image 使用此代码,它会给你一个RGB图像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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