what does waitKey (30) mean in OpenCV?
The function waitKey()
waits for a key event for a "delay" (here, 30 milliseconds). As explained in the OpenCV documentation, HighGui (imshow()
is a function of HighGui) need a call of waitKey regularly, in order to process its event loop.
Ie, if you don't call waitKey, HighGui cannot process windows events like redraw, resizing, input event, etc. So just call it, even with a 1ms delay :)
Using other keys for the waitKey() function of opencv
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 :)
Difference in output with waitKey(0) and waitKey(1)
From the doc:
1.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. Since the OS has a minimum time between switching threads, the function will not wait exactly 1
ms, it will wait at least 1
ms, depending on what else is running on your computer at that time.
So, if you use waitKey(0)
you see a still image until you actually press something while for waitKey(1)
the function will show a frame for at least 1
ms only.
What's 0xFF for in cv2.waitKey(1)?
0xFF
is a hexadecimal constant which is 11111111
in binary. By using bitwise AND (&
) with this constant, it leaves only the last 8 bits of the original (in this case, whatever cv2.waitKey(0)
is).
Why does the cv2.imshow() does not render without cv2.waitkey()?
From the documentation of cv2.imshow()
, the NOTE section mentions that the window is displayed for the amount of time indicated by the argument in cv2.waitKey()
. An argument of 0
indicates wait forever, so the image will be displayed forever unless you handle the keypress.
Controlling the duration for which the window needs to displayed is a useful aspect while debugging, displaying intermediate images, etc.
From the documentation of cv2.waitKey()
, the NOTE section mentions that 'This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.'
You can notice that without the cv2.waitKey()
, if you hover over the window that is displayed, the 'busy' cursor with the rolling wheel is displayed, indicating that the window is busy.
Can anyone tell me why the code below used waitKey(20) and what does this 0xFF == ord('q') mean?
The waitKey() function waits the specified amount of milliseconds and then returns the code of the key that was pressed, or -1 if no key was pressed.
To understand the expression better, let's add some parenthesis:
if (cv2.waitKey(20) & 0xFF) == ord('q')
The &
is a bitwise and operator which is used here for bit masking to get only the lowest eight bits (because 0xFF
is equal to 1111 1111
in binary). waitKey()
returns an int
which usually is a 32 bit or a 64 bit integer, depending on the architecture. So any "excess" bits are "eliminated" by the bitwise and. The ord()
function supposedly(!) returns the ordinal value of it's parameter, i. e. the ASCII code of 'q'
in that example.
In other words: It waits for 20 milliseconds for key presses and checks whether the pressed key is Q.
cv2.imshow() function is opening a window that always says not responding - python opencv
You missed one more line:
cv2.waitKey(0)
Then the window shows the image until you press any key on keyboard. Or you can pass as following:
cv2.waitKey(1000)
cv2.destroyAllWindows()
Here, window shows image for 1000 ms, or 1 second. After that, the window would disappear itself. But in some cases, it won't. So you can forcefully destroy it using cv2.destroyAllWindows()
Please read more tutorials first : http://docs.opencv.org/trunk/doc/py_tutorials/py_tutorials.html
OpenCV waitKey() function always returns 255 on mac
You normally have to and
the result with 255 to mask extraneous bits out:
key = cv::waitKey(1000) & 255;
Extraneous bits might be the shift or control key etc. They are OS-dependent.
The keypresses being detected also depends on which window you have selected - you need to have the image window selected (having the focus) IIRC for keypresses to be detected - rather than the Terminal window from which you launched your program.
Related Topics
Efficient String Concatenation in C++
/Usr/Lib/Libstdc++.So.6: Version 'Glibcxx_3.4.15' Not Found
Creating All Possible K Combinations of N Items in C++
Finding All the Subsets of a Set
Global Variable Within Multiple Files
What Are the Rules For Automatic Generation of Move Operations
How to Printf Uint64_T? Fails With: "Spurious Trailing '%' in Format"
Explicit Specialization of Template Class Member Function
How to Concatenate Two Strings in C++
Why Is 'This' a Pointer and Not a Reference
How to Concatenate Multiple C++ Strings on One Line
Embed Text File in a Resource in a Native Windows Application
Are Variable Length Arrays There in C++