C++ OpenCV imread not working in Android
Asked Answered
K

4

5

I am trying to read an image in my C++ code

LOGD("Loading image '%s' ...\n", (*inFile).c_str());;
Mat img = imread(*inFile, CV_LOAD_IMAGE_GRAYSCALE);
CV_Assert(img.data != 0);

and get the following output:

09-25 17:08:24.798: D/IRISREC(12120): Loading image '/data/data/com.example.irisrec/files/input/osoba1.jpg' ...
09-25 17:08:24.798: E/cv::error()(12120): OpenCV Error: Assertion failed (img.data != 0) in int wahet_main(int, char**), file jni/wahet.cpp, line 4208

The file exists. But strange is, that if I try to preview the image using Root File Browser it is just black. I copied the files there manually.

EDIT:

The code works fine under Windows with .png and .jpg format. I am just trying to port an existing C++ project for Iris Recognition to Android.

Kira answered 25/9, 2014 at 15:28 Comment(0)
K
1

I was able to solve this issue only by copying the image files in code.I stored them in my asset folder first and copied them to internal storage following this example. If someone can explain this to me please do this.

Kira answered 30/9, 2014 at 12:17 Comment(2)
imread can't read from a zip file(your apk). that's why you have to copy it elsewhere first.Anacreon
And rethinking your answer - I am not sure if data/data folder has anything to do with the apk?Kira
B
6

imread() determines the type of file based on its content not by the file extension. If the header of the file is corrupted, it makes sense that the method fails.

Here are a few things you could try:

  • Copy those images back to the computer and see if they can be opened by other apps. There's a chance that they are corrupted in the device;
  • Make sure there is a file at that location and that your user has permission to read it;
  • Test with types of images (jpg, png, tiff, bmp, ...);

  • For testing purposes it's always better to be more direct. Get rid of inFile:

Example:

Mat img = imread("/data/data/com.example.irisrec/files/input/osoba1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!img.data) {
    // Print error message and quit
}
Bratcher answered 29/9, 2014 at 3:56 Comment(1)
They are not corrupted when I copy them back and I don't see any problems with the permission to the Android internal storage. I tried with jpg and png. Both not working. The code is working fine under windows. Also I do not want to change the C++ code as I am trying to port a whole project to Android. It is an Iris Recognition Toolkit with far too many lines of code to adapt. And I am sure that the file path is correct.Kira
R
1

When debugging, first try to get more data on the problem.

  • It's an unfortunate design that imread() doesn't provide any error info. The docs just say that it'll fail "because of missing file, improper permissions, unsupported or invalid format".
  • Use the debugger to step into the code if you can. Can you tell where it fails?
  • Search for known problems, stackoverflow.com/search?q=imread, e.g. imread not working in OpenCV.

Then generate as many hypotheses as you can. For each one, think of a way to test it. E.g.

  • The image file is malformed (as @karlphillip offered). -- See if other software can open the file.
  • The image file is not a supported format. -- Verify the file format on your desktop. Test that desktop OpenCV can read it. Check the docs to verify the image formats that AndroidCV can read.
  • The image file is not at the expected path. -- Write code to test if there's a file at that path, and verify its length.
  • The image file does not have read permission. -- Write code to open the file for reading.
  • A problem with the imread() arguments. -- Try defaulting the second argument.
Raney answered 29/9, 2014 at 7:41 Comment(2)
Please see my comment to the other answer.Kira
Oh yeah and I will try to debug the C++ code. Couldn't set up the debugger correctly yetKira
K
1

I was able to solve this issue only by copying the image files in code.I stored them in my asset folder first and copied them to internal storage following this example. If someone can explain this to me please do this.

Kira answered 30/9, 2014 at 12:17 Comment(2)
imread can't read from a zip file(your apk). that's why you have to copy it elsewhere first.Anacreon
And rethinking your answer - I am not sure if data/data folder has anything to do with the apk?Kira
C
1

It could be a permission issue.You would have to request the permission from Java code in your Activity class like this in Android 6.0 or above. Also make sure that in your AndroidManifest.xml, you have the the following line :

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

In your activity file add this:

if (PermissionUtils.requestPermission(
                    this,
                    HOME_SCREEN_ACTIVITY,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
     Mat image = Imgcodecs.imread(filePath,Imgcodecs.IMREAD_COLOR);
}

I struggled a long time to find this and I was getting Mat object null for all the time before.

Clearstory answered 9/8, 2017 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.