简体   繁体   English

openCV imshow 不在屏幕上渲染图像

[英]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.我是 openCV 的新手,最近获得了 openCV 2.4.7 的预编译版本,并成功地将其与 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.显然库似乎工作正常,但是当我尝试使用 imshow 显示图像时,它会显示窗口但不显示其中的图像。

{
    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.任何帮助将不胜感激。

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. OpenCV 需要抽取消息来显示和更新窗口,而在waitKey函数内部就是这样做的所有机制。

As the documentation says, waitKey only works if you have a HighGUI window open, so in your code, you probably need to do this:正如文档所说, waitKey仅在您打开 HighGUI 窗口时才有效,因此在您的代码中,您可能需要执行以下操作:

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

I suggest removing the cv::namedWindow statement, and adding我建议删除cv::namedWindow语句,并添加

cv::waitKey();

after the cv:imshow statement.cv:imshow语句之后。 You can also check whether the dimensions of the window are correct.您还可以检查窗口的尺寸是否正确。

For Python users, using cv2.wait(0) is the solution.对于 Python 用户,使用cv2.wait(0)是解决方案。 So, the overall format goes like this所以,整体格式是这样的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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