Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load images with OpenCV from assets folder in Android

I'm stuck trying to load an image placed in assets folder with OpenCV 3.0 in Android. I've read a lot of answers here, but I can't figure out what I'm doing wrong.

"my image.jpg" is place directly in the assets folder created by Android Studio. This is the code I'm using. I've checked and the library has been loaded correctly.

        Mat imgOr = Imgcodecs.imread("file:///android_asset/myimage.jpg");
        int height = imgOr.height();
        int width = imgOr.width();
        String h = Integer.toString(height);
        String w = Integer.toString(width);

        if (imgOr.dataAddr() == 0) {
            // If dataAddr() is different from zero, the image has been loaded
            // correctly
            Log.d(TAG, "WRONG UPLOAD");
        }

        Log.d(h, "height");
        Log.d(w, "width");

When I try to run my app, this is what I get:

08-21 18:13:32.084 23501-23501/com.example.android D/MyActivity: WRONG UPLOAD
08-21 18:13:32.085 23501-23501/com.example.android D/0: height
08-21 18:13:32.085 23501-23501/com.example.android D/0: width

It seems like the image has no dimensions. I guess because it has not been loaded correctly. I'va also tried to load it placing it in the drawable folder, but it doesn't work anyway and I'd prefer to use the assets one. Anyone can please help me and tell me how to find the right path of the image?

Thanks

like image 519
andraga91 Avatar asked Aug 21 '16 16:08

andraga91


1 Answers

Problem: imread needs absolute path and your assets are inside a apk, and the underlying c++ classes cannot read from there.

Option 1: load image into Mat without using imread from drawable folder.

                InputStream stream = null;
                Uri uri = Uri.parse("android.resource://com.example.aaaaa.circulos/drawable/bbb_2");
                try {
                    stream = getContentResolver().openInputStream(uri);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;

                Bitmap bmp = BitmapFactory.decodeStream(stream, null, bmpFactoryOptions);
                Mat ImageMat = new Mat();
                Utils.bitmapToMat(bmp, ImageMat);

Option 2: copy image to cache and load from absolute path.

File file = new File(context.getCacheDir() + "/" + filename);
if (!file.exists())
try {

InputStream is = context.getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();

FileOutputStream fos = new FileOutputStream(file);

fos.write(buffer);
fos.close();
} catch (Exception e) {
throw new RuntimeException(e);
}

if (file.exists()) {
 image = cvLoadImage(file.getAbsolutePath(), type);
}
like image 105
jlgsoftware Avatar answered Sep 21 '22 05:09

jlgsoftware