Tips and Tricks of OpenCV cv2.waitKey() Tutorial with Examples

Introduction

In this article, we will see a quick tutorial about the OpenCV cv2.waitKey() function. We will first understand what this function does along with its syntax and then understand how useful it can be while displaying images and videos in OpenCV. All these things will be covered with the help of examples for better understanding.

What is cv2 waitkey() function in OpenCV ?

cv2 waikey() waits for the pressed key event before going to the next set of operations. Its syntax is as follows –

Syntax

cv2.waitKey(delay)

It waits for ‘delay’ milliseconds for any positive value of ‘delay’. And for any negative value of ‘delay’ or when ‘delay’ = 0, the function waits infinitely for a key event.

Simple Example of cv2.waitKey()

In this small trivial example, the numbers 1,2,3 are printed at an interval of 5000 ms or 5 seconds as 5000 is passed as a parameter to cv2 waitKey() function.

In [0]:

import cv2

print("1");
cv2.waitKey(5000)
print("2");
cv2.waitKey(5000)
print("3");

Out[0]:

1
2
3

 

Examples of Displaying Images with the help of cv2.waitKey()

Example 1

In this example, the image is read using imread() function, and then it is displayed with imshow() function. After this, the execution waits for 10000 ms or 10secs due to cv2.waitKey() function. Finally, after 10 secs the image window is closed with destroyAllWindows() function.

In [1]:

import cv2

img=cv2.imread("dog.jpg")
cv2.imshow('Image',img)
cv2.waitKey(10000)
cv2.destroyAllWindows()

Example 2

In this example, 0 is passed to cv2 waitKey() function which means the execution is going to wait infinitely till a key is pressed, after which the image window is closed.

Usually, for all practical purposes, people use cv2 waitKey(0) to close the image display instead of giving any other values in milliseconds that we saw in the first example.

In [2]:

import cv2

img=cv2.imread("dog.jpg")
cv2.imshow('Image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Example of Displaying Video with the help of cv2 waitKey()

The below example shows how cv2.waitKey() can be used to display a video feed from a webcam. Here the video feed will be closed once the x key is pressed.

In [3]:

vid = cv2.VideoCapture(0)

while(True):

  ret, frame = vid.read()

  cv2.imshow('frame', frame)

  # when 'x' key is pressed the video capture stops
  if cv2.waitKey(1) & 0xFF == ord('x'):
    break

vid.release()

cv2.destroyAllWindows()

 

As you can notice we are using cv2.waitKey(1) here and you may wonder why 1 millisecond is passed as a parameter to the cv2 waitkey() function. This is because cv2.waitKey(1) will ensure the frame of video is displayed for at least 1 ms.

If you would use cv2.waitKey(0) then a single frame of the video will open and it will wait for you to press a key and then open the next frame and so on. It won’t show you continuous feed.

Hence cv2.waitKey(0) is ideal for displaying image and cv2.waitKey(1) is ideal for displaying video.

 

Reference – Stackoverflow

  • MLK

    MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *