2

I am using openCV in C++ on OSX and Ubuntu systems.

I would like an equivalent function of setMouseCallback but for keyboard events (key pressed).

For what I have seen the way to do it in openCV is to use waitKey() but actually it does not work as a callback because stops the normal flow waiting for the event specified.

The problem is that I am streaming a video from the webcam so this solution is no good for me.

Is there any proper function to set a callback for keyboard events as is possible for mouse events?

roschach
  • 8,390
  • 14
  • 74
  • 124
  • From the [docs](https://docs.opencv.org/3.4.2/d7/dfc/group__highgui.html): "... sometimes there it is required to try functionality quickly and visualize the results. This is what the HighGUI module has been designed for" Basically (at least my my eyes), for anything other than prototypes where you don't really care about UI performance and don't need much other than show an image with few simple controls, you should pick a proper UI toolkit and implement it yourself. – Dan Mašek Aug 24 '18 at 22:52
  • However, I don't think the lack of keyboard callback is really an issue. You won't get around having to call `waitKey`, since that's what runs the message loop and calls the callbacks when appropriate events occur (i.e. waitKey not running => no callbacks). It exits immediately after getting a key event, so you could conceivably call it right again after you handle the keypress. | IMHO the more problematic part is the granularity of the wait, which will make it hard to keep a consistent frame rate (even if you offload VideoCapture to another thread, although that would help). – Dan Mašek Aug 24 '18 at 23:04
  • not exactly waitkey stops your flow of the program to wait that input. If u press the key while not waitng that event is lost/ – roschach Aug 25 '18 at 18:31
  • It might depend on platform, but at least on Windows, as long as I have the window focused, it gets the key events no matter what the program does, and responds to them on subsequent `waitKey` invocations (e.g. I have the program in `time.sleep`, hit 4 keys, and then can call `waitKey` 4 times getting the keycodes in sequence. Based on my knowledge of WinAPI and the windows HighGUI backend implementation, this is exactly what I would expect to happen. I would expect other backends to be similar, since apps aren't always just pumping messages, it would be odd if they missed input events. – Dan Mašek Aug 25 '18 at 19:43

1 Answers1

1

OpenCV does not provide a very robust UI layer, especially with respect to access to input devices. This is at least the case when it is compiled against the standard dependencies. You may want to look into compiling OpenCV with VTK (or use VTK on its own), which provides more I/O control. Or, more simply, use a more powerful input library that has access to the raw device data, like Gainput, which I have successfully used with libraries that provide NO I/O layer with their GUI methods.

Scott Mudge
  • 957
  • 6
  • 18
  • 1
    Thank you for your reply. Have u heard of VIZ? http://answers.opencv.org/question/184300/suggestion-drop-vtk-for-viz-and-use-simple-opengl/ here it suggests to use VIZ instead of VTK. What do you think? – roschach Aug 24 '18 at 21:55
  • I am following your suggestion but I am having problems compiling a project that includes the library. Can you take a look at the following post and see if you know the solution? Thank you. https://stackoverflow.com/questions/52058232/how-to-compile-project-using-vtk-library-using-makefile – roschach Aug 28 '18 at 12:57