2

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.

2
  • Why do you use 1 as second argument of impread instead of "bmp"? What is format 1 means?
    – Fumu 7
    Sep 22, 2014 at 1:24
  • Try to give full path to your image. It can't read the file a.bmp, so that it goes into if. If you use create, it just shows a blank image. This is assuming your imread() format is correct. I haven't checked that.
    – Autonomous
    Sep 22, 2014 at 3:29

4 Answers 4

8

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:

3
  • 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? Sep 22, 2014 at 17:05
  • 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.
    – Fuzz
    Sep 23, 2014 at 5:31
  • My problem was that I used the release libs on debug mode. Thanks for the last link!
    – Mauker
    Oct 10, 2016 at 18:59
4

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.

2
  • 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? Sep 22, 2014 at 16:58
  • 1
    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.
    – dexjq23
    Sep 23, 2014 at 1:20
1

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.

0

Your opencv libs should match the configi.e., Debug or Release of your application in Visual Studio or whatever build you are using (If you are using pre-built binaries) for WINDOWS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.