Ask Your Question
0

cv::waitKey not reading keyboard input properly

asked 2019-11-17 20:50:10 -0600

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;
}

The output of the test project

The output of the new project

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.

edit retag flag offensive close merge delete

Comments

You are comparing release and debug versions. Perhaps there is something wrong in the debug configuration.

mvuori gravatar imagemvuori ( 2019-11-18 01:16:28 -0600 )edit

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.

TheEMcoder gravatar imageTheEMcoder ( 2019-11-19 07:12:00 -0600 )edit

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.

TheEMcoder gravatar imageTheEMcoder ( 2019-11-19 09:55:34 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-11-19 07:12:22 -0600

TheEMcoder gravatar image

updated 2019-11-19 09:53:46 -0600

I think problem in waitKey(0). As I have the same issue. Don't use waitKey(0), use waitKey(1); Yes, FPS will little slow but no problem with Keys. If it don't work please, use destroyWindow after all functions which assosiated with waitKey(0). Or use namedWindow("Frame",WINDOW_AUTOSIZE) before imshow; And why you are doing poliphormism. As you convert waitKey() value to Char, then to the Integer and then in if you using char->to int and then == 27) Why? Use waitKey as int and dont convert to char as you don't need in: Use this code:

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);

int c = cv::waitKey(0);
std::cout << (c << std::endl;
if (c == 27) 
    break;
 }

 cap.release();
 cv::destroyAllWindows();
return 0;
        }
edit flag offensive delete link more

Comments

It doesn't matter. If I used 1 it will not work. So I used 0. You have to test it. FPS is not problem. It is you to write a code.

supra56 gravatar imagesupra56 ( 2019-11-19 09:18:17 -0600 )edit

It will matter. If you don't use destroyWindow or namedWindow It won't get key while you don't close window with image.

TheEMcoder gravatar imageTheEMcoder ( 2019-11-19 09:31:07 -0600 )edit

If it don't work please, use destroyWindow after all functions which assosiated with waitKey(0). Or use namedWindow("Frame",WINDOW_AUTOSIZE) before imshow;

TheEMcoder gravatar imageTheEMcoder ( 2019-11-19 09:34:04 -0600 )edit

And why he 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 he can write int c = waitKey(0) and don't use the casting at all.

TheEMcoder gravatar imageTheEMcoder ( 2019-11-19 09:55:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-11-17 20:50:10 -0600

Seen: 4,424 times

Last updated: Nov 19 '19