Trending September 2023 # Working Of Waitkey() In Opencv # Suggested October 2023 # Top 12 Popular | Speedmintonvn.com

Trending September 2023 # Working Of Waitkey() In Opencv # Suggested October 2023 # Top 12 Popular

You are reading the article Working Of Waitkey() In Opencv updated in September 2023 on the website Speedmintonvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Working Of Waitkey() In Opencv

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.

Start Your Free Software Development Course

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.

#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.

You're reading Working Of Waitkey() In Opencv

Update the detailed information about Working Of Waitkey() In Opencv on the Speedmintonvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!