[FIXED] How to read an image into a Mat object using opencv c++ and android studio ndk?

Issue

I have searched for a few hours now and can’t seem to find an answer to my question(s). I have written the following lines of code in the android ndk(c++) and I am using the needed opencv libraries to accomplish the task.

void opening_images(){
    Mat image ;
    sillyString = "I have changed";
    String imagePath = "//drawable//ring.png";

    image = imread(imagePath,IMREAD_COLOR);

    if(image.empty()){
        sillyString = "Image not loaded";

    }
    else {
        sillyString = "Image loaded";

    }
}

I have tested this code in Qt with opencv and it works fine.At the moment the program in android studio returns the “Image not loaded” string. I think the main problem which is present is, the fact that I don’t completely understand how to work with the file paths? In android studio I have included a picture under res/drawable/ring.png. I am able to view this image using the java side of the app.

Question 1: Is the specified imagePath = “//drawable//ring.png” correct to access the ring.png file ?
Question 2: Is there any permissions needed allowing the ndk to access res folders ?
Question 3: Is there any similar methods to assign an image to a Mat object?

Any help will be appreciated.

Edit:

The directories

Solution

Since this was my first time using Android Studio there was much to learn. It took me a while but here is the answers to the questions that I posted.

Question 1: Is the specified imagePath = “//drawable//ring.png” correct to access the ring.png file ?

This is most definitely not the correct path to use when accessing images for the purpose of image processing etc.The drawable folder can still be used to update ex. an image view by setting the src of the image view to the image

 imageView.setImageResource(R.drawable.ring);

When working with images and Mat objects, I found it best to use the Android debugging bridge to copy the files to the SD card of the device. This link will provided you with the necessary steps to install the adb https://www.howtogeek.com/125769/how-to-install-and-use-abd-the-android-debug-bridge-utility/

When the images are done copying to the SD card. The file path can be found by making use of the built in Java functionEnvironment.getExternalStorageDirectory(). Which looks something like /storage/emulated/0/YOUR_file it depends on the location which was used to copy the files to.

Useful tip: Download ES File Explorer for the device to help navigate through the external or internal storage.

Question 2: Is there any permissions needed allowing the ndk to access res folders ?
The method which I used didn’t need any permissions. However at the moment the NDK side cannot directly read an image from the SD card, the image must be passed from the Java side by making use of assets or by passing the address of the image which was read into the Mat object(Java side).

Read and write permission is needed in order to access the SD card. This must be set in the manifest.xml and must be correctly implemented in the code. There are many great tutorials on YouTube.

Question 3: Is there any similar methods to assign an image to a Mat object?
This question seems redundant now, there are many ways to skin a cat.

In short, I think it is easier to stick to the Java side when using Opencv4Android and some form of image processing is needed.To get you started in java here is a small snippet from my code.

Mat image;
String imageInSD = Environment.getExternalStorageDirectory()+"/Pictures/Data/Birds/"(ImageFolders[i])+"/"+String.valueOf(id)+".png";

image = Imgcodecs.imread(imageInSD,Imgcodecs.IMREAD_COLOR);

Good luck!!

Answered By – Johan Fick

Answer Checked By – Timothy Miller (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published