简体   繁体   English

OpenCV将加载图像但不显示它

[英]OpenCV will load image but not display it

I am using CMake and OpenCV with C++ and am just trying to run a simple program: 我正在将CMake和OpenCV与C ++一起使用,只是试图运行一个简单的程序:

#include "opencv/highgui.h"
#include "opencv/highgui.hpp"
#include "opencv/cv.h"
#include "opencv/cxcore.h"
#include "opencv/cxcore.hpp"
#include <stdio.h>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{

   Mat image = imread("test.jpg", CV_LOAD_IMAGE_UNCHANGED);
   if (!image.data) //check whether the image is loaded or not
   {
         cout << "Image cannot be loaded." << endl;
   }
   else
   {
        cout<<"Image can be loaded."<<endl;
        cout<<"Size is "<<image.size().height<<","<<image.size().width<<endl;
        namedWindow( "Display window", CV_WINDOW_AUTOSIZE );
        imshow( "Display Image", image );
    }
}

When I cmake, I get no errors, and when I run the program by doing ./test, I get "Image can be loaded" along with the correct size of the image. 当我cmake时,我没有任何错误,并且当我通过执行./test运行该程序时,我得到“可以加载图像”以及正确的图像大小。

Why won't my program create a new window which displays the image? 我的程序为什么不创建一个显示图像的新窗口?

Thank you! 谢谢!

use cv::waitKey() after imshow. 在imshow之后使用cv::waitKey() This is needed to proceed openCV rendering. 这是进行openCV渲染所必需的。

use waitKey(0) to pause until a key is pressed or waitKey(1) to pause as short as possible. 使用waitKey(0)暂停直到按下某个键,或者使用waitKey(1)暂停尽可能短。

For further reference . 供进一步参考

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

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