views:

1610

answers:

3

Hi all,

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. /update

Does anyone have any ideas?

Thanks!

=-N42-=

+1  A: 

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.

Jacob
The version is OpenCV2.0.
Nerf42
If so you should consider using the `Mat` class and the `imread` function - the new C++ interface is **much** better :)
Jacob
+1  A: 

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.

Catalin Iacob
That did the job. The .jpg was in the debug directory with the executable instead of in the project directory with the vcproj file. I don't know why the explicit path declaration would not work, I would guess there is always the possibility I did not pass the explicit path correctly. Thanks very much everyone!
Nerf42
A: 

try add HAVE_JPEG into preprocessor definitions

oleole