OpenCV cvLoadImage IOError
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:
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.