user967493
user967493

Reputation: 143

Display an image 2nd edition

I have a problem with my c++ opencv program. It should show a picture I loaded in, but after debugging only a grey window pops up. Here is my code:

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

    return 0;
}

The .jpg file is in the project folder.

Can anybody tell me what I have to do to get the picture to be shown. Help would be much appreciated!

Upvotes: 0

Views: 130

Answers (1)

SSteve
SSteve

Reputation: 10738

You should check to see if you are successfully loading the file. Try this:

int main(int argc, char* argv[])
{
    IplImage* img = cvLoadImage( "IMG_7321_.jpg" );
    if (!img) {
        fprintf(stderr, "Image not found\n");
        return -1;
    }
    cvNamedWindow( "IMG_7321_", CV_WINDOW_AUTOSIZE );
    cvShowImage("IMG_7321_", img);
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow( "IMG_7321_" );

    return 0;
}

Upvotes: 1

Related Questions