Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using other keys for the waitKey() function of opencv

Tags:

python

opencv

I'm working on a program (python ,opencv) in which I use the spacebar to go to the next frame, and Esc to exit the program. These are the only two keys i've got working. I tried to find out about more keys , tried various codes for them but didnt work. especially arrow keys.

I found this about waitkey, but it doesn't work.

So my question is, How do I catch other keys besides esc and spacebar to trigger certain functions in my python-opencv program?

like image 292
md1hunox Avatar asked Jan 24 '13 04:01

md1hunox


People also ask

What does waitKey do in OpenCV?

waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed.

Is cv2 waitKey necessary?

4. cv2. waitKey() This function is very important, without this function cv2.

What happens if we pass 0 to waitKey ()?

waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). 2. waitKey(1) will display a frame for 1 ms, after which display will be automatically closed.


2 Answers

You can use ord() function in Python for that.

For example, if you want to trigger 'a' key press, do as follows :

if cv2.waitKey(33) == ord('a'):    print "pressed a" 

See a sample code here: Drawing Histogram

UPDATE :

To find the key value for any key is to print the key value using a simple script as follows :

import cv2 img = cv2.imread('sof.jpg') # load a dummy image while(1):     cv2.imshow('img',img)     k = cv2.waitKey(33)     if k==27:    # Esc key to stop         break     elif k==-1:  # normally -1 returned,so don't print it         continue     else:         print k # else print its value 

With this code, I got following values :

Upkey : 2490368 DownKey : 2621440 LeftKey : 2424832 RightKey: 2555904 Space : 32 Delete : 3014656 ...... # Continue yourself :) 
like image 137
Abid Rahman K Avatar answered Sep 21 '22 18:09

Abid Rahman K


The keycodes returned by waitKey seem platform dependent. However, it may be very educative, to see what the keys return (and by the way, on my platform, Esc does not return 27...)

The integers thay Abid's answer lists are mosty useless to the human mind (unless you're a prodigy savant...). However, if you examine them in hex, or take a look at the Least Significant Byte, you may notice patterns...

My script for examining the return values from waitKey is below:

#!/usr/bin/env python  import cv2 import sys  cv2.imshow(sys.argv[1], cv2.imread(sys.argv[1])) res = cv2.waitKey(0) print('You pressed %d (0x%x), LSB: %d (%s)' % (res, res, res % 256,     repr(chr(res%256)) if res%256 < 128 else '?')) 

You can use it as a minimal, command-line image viewer.

Some results, which I got:

  • q letter:

    You pressed 1048689 (0x100071), LSB: 113 ('q')

  • Escape key (traditionally, ASCII 27):

    You pressed 1048603 (0x10001b), LSB: 27 ('\x1b')

  • Space:

    You pressed 1048608 (0x100020), LSB: 32 (' ')

This list could go on, however you see the way to go, when you get 'strange' results.

BTW, if you want to put it in a loop, you can just waitKey(0) (wait forever), instead of ignoring the -1 return value.

EDIT: There's more to these high bits than meets the eye - please see Andrew C's answer (hint: it has to do with keyboard modifiers like all the "Locks" e.g. NumLock).

My recent experience shows however, that there is a platform dependence - e.g. OpenCV 4.1.0 from Anaconda on Python 3.6 on Windows doesn't produce these bits, and for some (important) keys is returns 0 from waitKey() (arrows, Home, End, PageDn, PageUp, even Del and Ins). At least Backspace returns 8 (but... why not Del?).

So, for a cross platform UI you're probably restricted to W, A, S, D, letters, digits, Esc, Space and Backspace ;)

like image 25
Tomasz Gandor Avatar answered Sep 21 '22 18:09

Tomasz Gandor