openCV imshow not rendering image on screen

29,613

Solution 1

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);

Solution 2

For Python users, using cv2.wait(0) is the solution. So, the overall format goes like this

    cv2.imwrite("DetectionResults.jpg", frame)
    cv2.imshow("DetectionResults", frame)
    cv2.waitKey(0)

Solution 3

I suggest removing the cv::namedWindow statement, and adding

cv::waitKey();

after the cv:imshow statement. You can also check whether the dimensions of the window are correct.

Share:
29,613

Related videos on Youtube

Usama
Author by

Usama

Enthusiast iOS developer

Updated on July 09, 2022

Comments

  • Usama
    Usama almost 2 years

    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.

    • Roger Rowland
      Roger Rowland over 10 years
      Do you have a call to waitKey after imshow?
    • Usama
      Usama over 10 years
      I would have included image with the question but as i have recently got myself registered here, so i can not include images yet
    • Usama
      Usama over 10 years
      yes have tried waitKey still no success
  • Totoro
    Totoro over 10 years
    From the above answer, the problem seems to have been in the image format.