简体   繁体   中英

MS Visual Studio 2012 and OpenCV 2.4.5 - Image is not displayed

I'm using MS Visual Studio 2012 and OpenCV 2.4.5. I tried to run the following code. I'm

using OpenCV for the first time. I got this piece of code from the internet. I just want to

check whether OpenCV is working fine on my laptop.

As the output, a window is popped up ( gray-colured blank window) but the image is not

displayed in it. Can you point out where I have gone wrong ?

#include "stdafx.h"

#include "opencv/cv.h"    

#include "opencv2/highgui/highgui.hpp" 

int main(int argc, char** argv)
{

    IplImage* img = cvLoadImage( "image.jpg" ); 

    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );

    cvShowImage("Example1", img);

    cvWaitKey(0);

    cvReleaseImage( &img );

    cvDestroyWindow( "Example1" );

    return 0;
}

clearly , it did not find your image.

try an absolute path instead

sidenote:

you're trying to use the outdated c-api. it's only being kept around for maintenance / portability reasons,

you should not develop any new code like that !

you should not develop any new code like that !

you should not develop any new code like that !

(was that clear enough ?)

instead use the c++ api:

#include "opencv2/core/core.hpp"    
#include "opencv2/highgui/highgui.hpp" 

int main(int argc, char** argv)
{

    cv::namedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cv::Mat img = cv::imread( "d:/some/dir/some.png" ); 
    if ( img. empty() )  // only idiots *don't check*  resource loading ...
        return -1; 

    cv::imshow("Example1", img);
    cv::waitKey(0);

    // no cleanup required with c++ ..
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM