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.

bitmask
  • 32,434
  • 14
  • 99
  • 159
Nerf42
  • 191
  • 1
  • 3
  • 12
  • 1
    On what platform does you compile the program? Can you check if the image is actually loaded if(img == NULL) { std::cout << "Trouble" << std::endl; } – midtiby Jan 22 '10 at 08:21
  • More testing shows the executable works fine outside the visual studio environment provided the OpenCV .dll files are present in the same directory, but indeed, img is a null pointer after cvLoadImage() is called when running in the debug environment. – Nerf42 Jan 22 '10 at 08:31
  • The problem might be that your program is not executed in the same directory as the image file resides in. (this is a problem as you use a relative path) – midtiby Jan 22 '10 at 08:35
  • This seems to be a problem related the debugger. I replaced the relative path with an explicit one and still the img variable comes back a null pointer after cvLoadImage(), but the executable runs correctly (image is displayed) when run standalone (with appropriate files local). – Nerf42 Jan 22 '10 at 08:41
  • Which version of OpenCV are you using? – Jacob Jan 22 '10 at 13:14
  • OpenCV2.0 is the version being used. – Nerf42 Jan 22 '10 at 16:20

4 Answers4

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.

Catalin Iacob
  • 644
  • 5
  • 18
  • 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 Jan 22 '10 at 16:18
  • @Catalin_lacob I need your help. I tried what you said. fopen does not return NULL, neither does cvLoadImage, bt cvShowImage shows a greybox...... – John Demetriou Feb 18 '13 at 18:33
  • never mind, found it, since I did not have waitkey and cvrelease afterwards it did not work, do not know why though – John Demetriou Feb 18 '13 at 19:10
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.

Jacob
  • 34,255
  • 14
  • 110
  • 165
  • If so you should consider using the `Mat` class and the `imread` function - the new C++ interface is **much** better :) – Jacob Jan 22 '10 at 16:24
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

Jeremy
  • 23
  • 7
0

Try to add HAVE_JPEG into preprocessor definitions.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
oleole
  • 1