cv::waitKey not reading keyboard input properly
So recently, I started using OpenCV. I have created a test project to set everything up in Visual Studio 2019. When that project is working, I exported that as a template, and used that to create my actual project. However, I found that the template code is no longer working properly. I have made sure that the include directories are correct, and that the linker is also correct. The code is identical in both projects. The following code works in the original project but not in the new one. I was changing the code in both projects to try to identify the problem, but nothing that I have tried tells me anything helpful.
What happens when I run the code:
In the original project, the waitKey(0) function pauses the program correctly, but in the new project, the program continues to run even if I'm not pushing any buttons.
#include <opencv2/opencv.hpp>
#include <iostream>
int main()
{
cv::VideoCapture cap(1);
cv::Mat frame;
if (!cap.isOpened())
{
std::cout << "Error opening video capture." << std::endl;
return -1;
}
while (1) {
cap >> frame;
if (frame.empty())
break;
cv::imshow("Frame", frame);
char c = (char)cv::waitKey(0);
std::cout << (int)c << std::endl;
if (c == 27)
break;
}
cap.release();
cv::destroyAllWindows();
return 0;
}
Note that in the test project, the program correctly waits for keyboard input, while in the new one, the program just moves on without reading the keyboard.
So, I guess what I want is to see if anyone here have any idea on how to solve this issue.
You are comparing release and debug versions. Perhaps there is something wrong in the debug configuration.
I think problem in waitKey(0). AsI have the same issue. Don't use waitKey(0), use waitKey(1); Yes, FPS will little slow but no problem with Keys.
And why you cast return value from waitKey to char then to int and then in IF() again casts char to int and compare it with 27 while you can write int c = waitKey(0) and don't use the casting at all.