Updated April 7, 2023
Introduction to OpenCV waitKey
The binding function on the keyboard, which waits for a specified number of milliseconds for any keyboard event to happen, is called waitKey() function in OpenCV, and if the value 0 or any negative value is provided as a parameter to the waitKey() function, it is a special value that causes the currently running thread to wait infinitely for the key event to happen and in case if no keyboard events happen before the time specified in milliseconds as the parameter to the waitKey() function, the waitKey() function returns -1 and passing 0 as an argument to the waitKey() function is very suitable while displaying images.
Syntax to define waitKey() function in OpenCV:
waitKey(delay)
Where,
- delay specifies the time in milliseconds; the currently running thread waits for the keyboard events to happen.
Working of waitKey() in OpenCV
- To display a given image or a frame from a given video for a certain amount of time, we make use of a function called waitKey() function in OpenCV.
- The time for which the currently running thread should wait for the keyboard events to happen is specified in milliseconds and passed as an argument to the waitKey() function.
- The image or video to be displayed using waitKey() function disappears once the delay time specified in milliseconds as an argument to the waitKey() function is over.
- By passing the value 0 or any negative value as an argument to the waitKey() function, the currently running thread waits infinitely for the keyboard events to happen. As a result, the image or video to be displayed is displayed as long as no key is pressed on the keyboard.
- The waitKey() function returns -1 if no keyboard events happen within the time specified in milliseconds as an argument to the waitKey() function.
Examples of OpenCV waitKey
Given below are the examples of OpenCV waitKey:
Example #1
In python, the OpenCV program demonstrates the waitKey() function to display an image as the output of the program until a key is pressed on the keyboard.
Code:
#importing all the required modules
import cv2 as cv
#reading the image that is to be displayed as the output on the screen until a keyboard event takes place, using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/images/educba.jpg')
#using waitKey() function to display the image as the output on the screen until a keyboard event takes place
cv.imshow('Image', imageread)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
In the above program, the necessary module cv2 is imported. Then we are reading the image that is to be displayed as the output on the screen until a keyboard event takes place, using imread() function. We are then using the waitKey() function to display the image as the output on the screen until a keyboard event occurs. The output is shown in the snapshot above.
Example #2
In python, the OpenCV program demonstrates the waitKey() function to display an image as the output of the program until a key is pressed on the keyboard.
Code:
#importing all the required modules
import cv2 as cv
#reading the image that is to be displayed as the output on the screen until a keyboard event takes place, using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/images/educbalogo.jpg')
#using waitKey() function to display the image as the output on the screen until a keyboard event takes place
cv.imshow('Image', imageread)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
In the above program, the necessary module cv2 is imported. Then we are reading the image that is to be displayed as the output on the screen until a keyboard event takes place, using imread() function. We are then using the waitKey() function to display the image as the output on the screen until a keyboard event occurs. The output is shown in the snapshot above.
Example #3
In python, the OpenCV program demonstrates the waitKey() function to display an image as the output of the program until a key is pressed on the keyboard.
Code:
#importing all the required modules
import cv2 as cv
#reading the image that is to be displayed as the output on the screen until a keyboard event takes place, using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/images/logo.png')
#using waitKey() function to display the image as the output on the screen until a keyboard event takes place
cv.imshow('Image', imageread)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
In the above program, the necessary module cv2 is imported. Then we are reading the image that is to be displayed as the output on the screen until a keyboard event takes place, using imread() function. We are then using waitKey() function to display the image as the output on the screen until a keyboard event occurs. The output is shown in the snapshot above.
Example #4
In python, the OpenCV program demonstrates the waitKey() function to display an image as the output of the program until a key is pressed on the keyboard.
Code:
#importing all the required modules
import cv2 as cv
#reading the image that is to be displayed as the output on the screen until a keyboard event takes place, using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/images/plane.jpg')
#using waitKey() function to display the image as the output on the screen until a keyboard event takes place
cv.imshow('Image', imageread)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
In the above program, the necessary module cv2 is imported. Then we are reading the image that is to be displayed as the output on the screen until a keyboard event takes place, using imread() function. We are then using the waitKey() function to display the image as the output on the screen until a keyboard event occurs. The output is shown in the snapshot above.
Recommended Articles
We hope that this EDUCBA information on “OpenCV waitKey” was beneficial to you. You can view EDUCBA’s recommended articles for more information.