1

I am new to OpenCV in Android. I just wanted to make a samll test to the openCV lib in android. I downloaded an image .JPG and placed it inside the drawable folder, and I used the below code to read the image and display its size. when i run the below code i get the size of the image is 0x0 despite it is more than 400 kb.

why the size is 0x0 and how to get the right size?

code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                    Log.i("OpenCV", "OpenCV loaded successfully");

                    Mat image = readImageFromResources();
                    Log.i(TAG, "size: " + image.size());
                    Log.i(TAG, "rows: " + image.rows());
                    Log.i(TAG, "cols: " + image.cols());

                    break;
                default:
                    super.onManagerConnected(status);
                    break;
            }
        }

        private Mat readImageFromResources() {
            return Imgcodecs.imread(getResources().getDrawable(R.drawable.see).toString());
        }
    };
}
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Are you sure that the image is loaded correctly? Try `Mat image = new Mat(2,3,CV_8UC3)` (or whatever is the correct syntax in Java) and check the the size is correct – Miki Oct 06 '16 at 15:03
  • 1
    `Imgcodecs.imread(getResources().getDrawable(R.drawable.see).toString())` <= what this supposed to do? this doesn't make sens at all... you can also try: `Imgcodecs.imread("some random string")` – Selvin Oct 06 '16 at 15:08
  • @Selvin i googled how to read image in opencv using Android API and in one of the examples used the same syntax – Amrmsmb Oct 06 '16 at 15:09
  • *and in one of the examples used the same syntax* ? link or doesn't happend ... I never used OpenCV but `Imgcodecs.imread` seems to need path or bytes not some random string – Selvin Oct 06 '16 at 15:11
  • @Selvin see here: http://stackoverflow.com/questions/8131596/loading-an-image-using-opencv-in-android the answer of Mehmet Taha Meral – Amrmsmb Oct 06 '16 at 15:14

1 Answers1

0

First you have to add permissions in your AndroidManifest.xml to read the image:

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

Second, to load a resource in Android with OpenCV you should use the method Utils.loadResource() (see JavaDoc). So, your method should be something like this:

private Mat readImageFromResources() {
    Mat img = null;
    try {
        img = Utils.loadResource(this, R.drawable.see);
        Imgproc.cvtColor(img, img, Imgproc.COLOR_RGB2BGRA);
    } catch (IOException e) {
        Log.e(TAG,Log.getStackTraceString(e));
    }
    return img;
}

We need to convert the image because OpenCV stores its matrix internally in the BGR (blue, green, and red) format.

You can add a flags as third parameter to open the image in a particular way:

  • Imgcodecs.CV_LOAD_IMAGE_UNCHANGED
  • Imgcodecs.CV_LOAD_IMAGE_ANYCOLOR
  • Imgcodecs.CV_LOAD_IMAGE_ANYDEPTH
  • Imgcodecs.CV_LOAD_IMAGE_COLOR
  • Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE

For example: Utils.loadResource(this, R.drawable.see, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

If you then want to show the image in an ImageView, you can do:

private void showImg(Mat img) {
    final Bitmap bm = Bitmap.createBitmap(img.cols(), img.rows(),Bitmap.Config.RGB_565);
    Utils.matToBitmap(img, bm);
    final ImageView imageView = (ImageView) findViewById(R.id.img_view);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            imageView.setImageBitmap(bm);
        }
    });
}

Note: The code above is for OpenCV 3.x, if you are using OpenCV 2.x replace Imgcodecs by Highgui.

David Miguel
  • 12,154
  • 3
  • 66
  • 68