1

I tried reading an image from drawable folder. I am using eclipse ide. I ran the below code but the image was not loaded. The code is taken from here

Mat image =  new Mat(new Size(500,500 ),CvType.CV_8U);// Change CvType as you need.
            image = Highgui.imread("icon.png");
            if(image.empty()) {
                Log.i(TAG, "Empty image!");
            }

My Screenshot of my drawable folder is below : enter image description here

How can I load this image?

4

3 回答 3

5

You can just simply do this

Mat m = Utils.loadResource(MainActivity.this, R.drawable.img, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m, bm);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageBitmap(bm);
于 2015-04-28T16:00:45.043 回答
1

I found an example for this work.

InputStream inpT = getResources().openRawResource(R.drawable.imgt);
mTemp = readInputStreamIntoMat(inpT);

private static Mat readInputStreamIntoMat(InputStream inputStream) throws IOException {
    // Read into byte-array
    byte[] temporaryImageInMemory = readStream(inputStream);

    // Decode into mat. Use any IMREAD_ option that describes your image appropriately
    Mat outputImage = Highgui.imdecode(new MatOfByte(temporaryImageInMemory), Highgui.IMREAD_GRAYSCALE);

    return outputImage;
}

private static byte[] readStream(InputStream stream) throws IOException {
    // Copy content of the image to byte-array
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[16384];

    while ((nRead = stream.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();
    byte[] temporaryImageInMemory = buffer.toByteArray();
    buffer.close();
    stream.close();
    return temporaryImageInMemory;
}
于 2015-03-24T16:33:46.250 回答
0

should be doing instead is:

  Mat m = Highgui.imread(file.getAbsolutePath());
于 2015-03-24T12:22:45.737 回答