开发者

Display an image 2nd edition

开发者 https://www.devze.com 2023-04-09 09:35 出处:网络
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:

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(开发者_StackOverflow社区 &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!


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号