Why is imread() always returning null data?

13,184

Solution 1

To debug your issue you should try to confirm that the image path is correct.

As suggested in the comments, try specifying the full absolute path of the file. Remember to to use escape slashes if you are on windows (e.g. c:\a.bmp will need to be "c:\a.bmp")

OR

If you are executing your application from Visual Studio then you can configure the working directory to be that of the bitmap too! (OpenCV cvLoadImage() does not load images in visual studio debugger?)

You can also try using cvLoadImage instead of imread. If cvLoadImage can open the file then it is possible that you have a mix of release and debug libraries causing you an issue as per:

Solution 2

The OpenCV documentation has mentioned imread() would return an empty matrix ( Mat::data==NULL ) if the image file cannot be read.

http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imread

You should check if the "a.bmp" file is in your current working directory. The IDE (visual studio) may set executable's working directory (Debug/... Release/... ) on purpose. Using an absolute path to "a.bmp", or starting executable in an expected directory from command line would also help, provided that "a.bmp" is a valid BMP file and you have the right file system permission to read it.

Solution 3

I was having the same issue on visual studion and imread returning null data.

img = cv::imread("c:\\data\\a.bmp",1);

Note the \\ delimiters to enable windows to correctly parse the path.

Share:
13,184
Tanmay Verma
Author by

Tanmay Verma

Updated on July 10, 2022

Comments

  • Tanmay Verma
    Tanmay Verma almost 2 years

    Here is my code:

        #include<opencv\cv.h>
        #include<opencv\highgui.h>
        using namespace cv;
        using namespace std;
    
        int main()
             {
              Mat src;
              //src.create(200,500,CV_8UC3);
              src = imread( "a.bmp", 1 );
              namedWindow( "Display window", WINDOW_AUTOSIZE );
              if(!src.data)                              
                     cout<<"Could not open or find the image" << std::endl ;
              else
                    imshow( "Display window", src);
              waitKey(0);
              return 0;
             }
    

    It is always executing the if part
    when I am using src.create instead of imread() it shows an empty image.

  • Tanmay Verma
    Tanmay Verma over 9 years
    I tried everything, but all failed. Well i am using OpenCV-2.4.9 on extracting it is having two directories one is build and another is source and in both directories it contains include folder. and in additional include directories i included only bulid's include, is that correct?
  • Tanmay Verma
    Tanmay Verma over 9 years
    I tried everything, but all failed. Well i am using OpenCV-2.4.9 on extracting it is having two directories one is build and another is source and in both directories it contains include folder. and in additional include directories i included only bulid's include, is that correct?
  • dexjq23
    dexjq23 over 9 years
    Have you tried the absolute path as we suggested? Like imread("C:\\path\\to\\a.bmp") for example. If you get file path correct, the library issue @Fuzz noted is probably another source to cause your problem. I haven't developed with OpenCV on Visual Studio yet, but I think that bug may still lurk.
  • Fuzz
    Fuzz over 9 years
    If the include directories and libraries weren't right then you would be able to compile or link your application. If you're having trouble with absolute paths then find out the full path if your executable by using the example here: stackoverflow.com/questions/298510/… Then put the bitmap in the working directory that it tells you.
  • Mauker
    Mauker over 7 years
    My problem was that I used the release libs on debug mode. Thanks for the last link!