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.
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:
there is no rpoblem in your code....(tested in both cases winodws 10 VS 2015 opencv 3.3-dev
thanks, I really hate this kind of situation :(
whty have you got a weird char in the name of cmd window?
you mean that W-like char? it's just same as '/' in korean language Windows.
Same problem, VS2017, openCV3.4
Interestingly, the name of the 2nd window (as defined in imshow("...this name...")) does not matter, i.e.:
namedWindow("D1", WINDOW_AUTOSIZE);
imshow("D2", image);
waitKey(0);
does exactly the same as:
namedWindow("D1", WINDOW_AUTOSIZE);
imshow("D1", image);
waitKey(0);
which is: creating 2 windows, the 'namedWindow' being gray. In both cases, waitKey(0) responds to the gray window only.
@rdg65 please do not post answers here, if you have a question or comment, thank you.