views:

1947

answers:

5
#include "cv.h"
#include "highgui.h"
#include <stdio.h>


int main(int argc, char* argv[]){
    cvNamedWindow("Window1", CV_WINDOW_AUTOSIZE);
    IplImage* image = 0;
    ->->image = cvLoadImage(argv[1]);<-<-
    if(!image) printf("Unable to load image!");
    cvShowImage("Window1", image);
    char c = cvWaitKey(0);
    cvReleaseImage(&image);
    cvDestroyWindow("Window1");
    return 0;
}

If I replace the indicated line with cvLoadImage("247.png") I get a blank window and image remains equal to zero

If I run the exe and give it 247.png as an argument, it's just dandy. If I put the "247.png" right into the code and build and run it Visual Studio 2008, it fails. If I build and run from the command prompt, it works.

Why is this? I'm a little bit weary of moving forwards without getting this down.

+2  A: 

Are you certain "247.png" is in the current working directory when you have the name hardcoded?

Run the program under something like Process Monitor to see what file is really being opened (or what file I/O errors there might be).

After your edit to add more information to the question (the problem occurs when run from VS2008) this is almost certainly your problem. The current directory that VS starts the program under is not the directory that has the "247.png" file.

Michael Burr
A: 

Can you check to see what your cwd is?

You can #include <direct.h> and use the _getcwd function to see what it is. That will probably point you to the culprit.

FreeMemory
A: 

Any follow ups to this email? How did you get it working? I am running the process from the same directory as the image, seems like the image is being read in fine (image!=null).

A: 

The problem appears to have been that Visual Studio was copying the executable over into a temporary directory, as Mr. Burr suggested -- compiling, putting it in my working folder, but executing a copy in a different folder.

@dammitimmad: I would suggest telling your IDE to Compile, and then running the executable yourself, completely separate from the IDE. Alternately, there could be complications relating to OpenCV loading the image. Can the image be displayed at all?

Thanks all. I'll definitely keep that cwd trick in mind for later!

Lansen Q
A: 

Under Project->Properties->Configuration Properties->Debugging there is a field "Working Directory". Set that to the directory you want to execute in and that should fix the problem.

MSN