C++ openCV imshow 不在屏幕上渲染图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20561430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:12:24  来源:igfitidea点击:

openCV imshow not rendering image on screen

c++visual-studio-2010opencv

提问by Usama

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.

任何帮助将不胜感激。

回答by Roger Rowland

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 waitKeyfunction is all of the mechanism to do so.

后者是行不通的。OpenCV 需要抽取消息来显示和更新窗口,而在该waitKey函数内部就是这样做的所有机制。

As the documentation says, waitKeyonly 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);

回答by Totoro

I suggest removing the cv::namedWindowstatement, and adding

我建议删除该cv::namedWindow语句,并添加

cv::waitKey();

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

cv:imshow声明。您还可以检查窗口的尺寸是否正确。