6

I am trying to work out a simple hello world for OpenCV but am running out of ideas as to why it is not working.

When I compile and run this code:

#include <cv.h>
#include <highgui.h> 
int main(int argc, char* argv[])
{
 IplImage* img = cvLoadImage( "myjpeg.jpg" );
 cvNamedWindow( "MyJPG", CV_WINDOW_AUTOSIZE );
 cvShowImage("MyJPG", img);
 cvWaitKey(0);
 cvReleaseImage( &img );
 cvDestroyWindow( "MyJPG" );
 return 0;
}

I get a grey box about 200x200 instead of the indicated .jpg file. If I use a different jpg I get the same kind of window, and if I put an invalid filename in, I get a very tiny window (expected).

I am using Visual Studio 2008 under Windows 7 Professional.

Most of the sample programs seem to work fine, so I am doubly confused how that code loads the sample jpgs just fine but in the code above it does not work (even tried the sample jpeg).

Update

The executables produced by compiling work fine, however the Visual Studio 2008 debugger loads a null pointer into img every time I try to run the debugger - regardless if the file location is implicit or explicit.

4

4 回答 4

8

It really seems like there's a problem with the path to myjpeg.jpg since the current directory could be different when you're running under the debugger.

By default, the current directory that the Visual Studio debugger uses is the directory containing the .vcproj file, but you can change it in the project properties (Debugging -> Working Directory).

Are you 100% sure that you pass the absolute path correctly? Try to pass the same path to fopen and see if it also returns NULL. If so, then the path is incorrect.

If you want to see exactly what file is the library trying to open you can use Project Monitor with a filter on myjpeg.jpg.

于 2010-01-22T13:40:24.727 回答
1

Which version of OpenCV are you using? I've tried your code on the latest (OpenCV2.0) and it works fine. You can download OpenCV2.0 from here.

If you want the latest build, you can get check it out with SVN from here.

于 2010-01-22T13:18:59.550 回答
0

Try to add HAVE_JPEG into preprocessor definitions.

于 2010-06-25T12:04:21.313 回答
0

I encountered the same problem. The Debug version does not load the image, but when I compile and link it as a Release it works. I hope this helps

于 2012-11-02T22:56:20.507 回答