Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

c++ - openCV imshow not rendering image on screen

I am new to openCV, have recently obtained a pre-compiled version of openCV 2.4.7 and was successfully able to integrate it with visual studio 2010.

Apparently library seems to work fine, but when I'm trying to display image using imshow it displays the window but doesn't display image in it.

{
    cv::Mat image = cv::imread("F:/office_Renzym/test3.jpg",CV_LOAD_IMAGE_UNCHANGED);

    if(image.empty())
    {
        cout<<"image not loaded";
    }
    else
    {
        cv::namedWindow( "test", CV_WINDOW_AUTOSIZE );
        cv::imshow("test",image);
    }   
}

Any help would be highly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must have:

cv::waitKey(0);

instead of:

system("pause");

The latter just doesn't work. OpenCV needs to pump messages to get the window displayed and updated, and inside that waitKey function is all of the mechanism to do so.

As the documentation says, waitKey only works if you have a HighGUI window open, so in your code, you probably need to do this:

cv::Mat image = cv::imread("F:/office_Renzym/test3.jpg",CV_LOAD_IMAGE_UNCHANGED);

if(image.empty())
{
    cout<<"image not loaded";
}
else
{
    cv::namedWindow( "test", CV_WINDOW_AUTOSIZE );
    cv::imshow("test",image);
    cv::waitKey(0);
}   

In case there's a problem with the image format, you might try loading like this:

cv::Mat image = cv::imread("F:/office_Renzym/test3.jpg",CV_LOAD_IMAGE_COLOR);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...