OpenCV cvLoadImage IOError

Category:UncategorizedTag: , , :

I’ve been working on a project that involves computer vision algorithms.  Fortunately, really smart people have done all the hard work and produced OpenCV.  Even better, there are a set of Python bindings, which makes a big difference because my project is written using Python.

The bad news:  I’m writing this on a Windows box and support for some things is sketchy or just non-existent.  Also, using Python vs. this C library puts a big distance between “what you think is happening” and “what is happening,” so debugging leaves you wondering if the problem is your code, Eclipse somehow messing things up, Python, the Python bindings, somewhere within openCV, or just broken or incomplete support for Windows.

So imagine my confusion when this simple program:

import cv
img = cv.LoadImage("img.tif")

Generates IOError: [Errno 2] No such file or directory: ‘img.tif’

I mean, what’s simpler than just loading a file from disk?! 

I found many people reporting this error, but no solutions other than “The file or directory doesn’t exist or isn’t readable…”

So, after checking (and rechecking) that the file does indeed exist, and then after using the full path to the file and getting the same error, I tried to find where the error was originating through all these layers.

I tried opening the image file using PIL and then converting the image over to OpenCV format, which did not generate an error, except that the resulting image wouldn’t quite survive the process – so I guess that doesn’t count as “working.”  Displaying the image using Windows Photo Viewer worked fine.  Out of desperation I grabbed a random image off of the Internet and tried to open it with OpenCV — and it worked.

Hmm, where did my original images come from?  I screen-grabbed them off of another application, and then to save time – rather than waiting for Gimp to open – I just used MSPAINT.EXE to … oh, damn.  “Short Cuts make for Long Journeys.”  The original file was corrupt, just enough so that openCV would be unable to load it, but not so much that other programs and libs couldn’t load “something.”

So, ignore the text of that error message and read it as “openCV, for some reason that it won’t describe, could not load your image.  Maybe the file is not there, or maybe you messed it up by being sloppy?” 

Re-capturing the image using Gimp and PNG fixed the problem.

6 thoughts on “OpenCV cvLoadImage IOError

  1. A similar issue helped me make the choice for between using Watir or Watin/Watij. The main problem I had with using Watir was that debugging Ruby code is somewhat difficult when you are using someone else’s library. Its very difficult to determine where exactly is the uninformative error message coming from. With the Java and C# versions of the WatiX framework, even though some of the syntax is not as easy, the errors messages are much more clear and down to a lower level. Perhaps its just my understanding of Java and C# over Ruby, but for me the choice came down to errors like the one you posted above. There is a major trade-off between static and dynamically typed languages when it comes to debugging and # of reasons why something can go wrong.

  2. @John Sonmez
    While I agree with you that this layer-on-layer stuff makes debugging tough, the underlying lack-of-meaningful-error issue was due the typical error handling in the granddaddy of static languages, C – “if (!whatever) return 0;”

    The python wrapper just echos the [non-helpful] ERRNO and message it got from C.

  3. Example code from openCV source, taken wildly out of context, and with numerous syntax liberties added for blog publishing:

    // gets called by loadImage()
    ImageDecoder findDecoder( const string& filename ) {
      FILE* f= fopen( filename.c_str(), "rb" );
      if( !f )
        // NB: can't open file = empty ImageDecoder
        return ImageDecoder(); 
      // ..bunch of stuff happens.. and then..
      for( i = 0; i [ decoders.size(); i   ) {
        if( decoders[i].checkSignature(signature) )
          // NB: all good, return correct decoder
          return decoders[i].newDecoder();
      // NB: couldn't find decoder due to bad data,
      // **take same action as if the file couldnt be opened**
      return ImageDecoder();
  4. @John Sonmez
    LOL! Yes, yes it is.

    I picked that image as a placeholder for facebook or twitter or some such social network long time ago, with the intent of replacing it with something else… and just haven’t gotten around to it yet.

    AND, I’m in the process of moving some code from Python to Visual C to see if that makes any of my problems go away, or just generates new one…

  5. LOL! This happened to me with video streaming players. Somehow I corrupted the original video when sending it to the client. All I got was “video not found error”. Two days of debugging and troubleshooting. finally found it using a hex compare. Original file with streamed file. The header was corrupted.

Comments are closed.

Find me


