Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

C++ openCV waitKey(0) not working?

 #include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int isSquare(String fileName);
int main() {
    String imgName = "C:/A.jpg";
    isSquare(imgName);
}
int isSquare(String fileName) {
    Mat img;
    img = cv::imread(fileName, IMREAD_COLOR);
    if (img.empty()) {
        cout << "Could not open or find the image" << endl;
        return -1;
    }
    //namedWindow("display", WINDOW_AUTOSIZE);
    imshow("display", img);
    waitKey(0);
    cout << "hi";
    destroyWindow("display");
    return 0;
}

Hi, I'm currently messing around with openCV 3.30, C++, VS2015. Now I'm trying to open an image, but the display window just kept disappear when I execute above code. I commented namedWindow("display", WINDOW_AUTOSIZE); because openCV document says cv:imshow() will automatically create one, and if I un-comment that line I got one gray window, one image window like this. enter image description here

I don't want to got that gray window, and key input for waitKey(0) works only when I focus on gray window, not on image window. So I made that line as a comment. but if I do that, when I execute that code the image window disappears instantly as if I don't have waitKey(0) code. Clearly waitKey(0) is not working because cout<<"hi"; after waitKey(0) was executed.

Am I missing something? Is the document wrong and using namedWindow necessary? All I wanted was to get rid of that gray window... any words of wisdom is appreciated, thanks.

plus, I got these responses from stackoverflow:

image description