Displaying images and videos is a critical aspect of many computer vision tasks. Python is a powerful language and is widely used in the field of computer vision because of its simplicity and ease of use.
But, how can we display images and videos using Python? In this article, we will explore the OpenCV library for displaying images and videos in Python.
OpenCV waitKey function
The waitKey function is an essential function in the OpenCV library that waits for a keystroke event. The function parameter is the time in milliseconds.
When the function is called, it waits for the specified time before returning. If there is a keystroke event during this time, it returns the ASCII code for the pressed key.
Else, it returns -1.
Code example for displaying images
To display an image using OpenCV in Python, we first need to read the image using imread function, followed by displaying it using the imshow function. The destroyAllWindows function is used to close the window.
Let us take a look at an example below:
import cv2
image = cv2.imread('image.jpg')
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
In this example, we have loaded the image ‘image.jpg’ using the imread function. We have then displayed the image using the imshow function and added a title “Image”.
The waitKey function waits for the user to press any key before closing the window using the destroyAllwindows function.
Code example for playing videos
Playing videos in OpenCV is not much different from displaying images. We use the VideoCapture function to read the video from a file.
The read function is then used to read each frame from the video. Finally, we display the video using the imshow function.
The q key is used to stop the video, the release function is used to release the video object, and the destroyAllWindows function is used to close the window. Let us examine the code example below:
import cv2
video = cv2.VideoCapture('video.mp4')
while True:
ret, frame = video.read()
if ret:
cv2.imshow('Video', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
video.release()
cv2.destroyAllWindows()
In this example, we have used a while loop to read each frame of the video using the read function. The condition ‘ret’ indicates that a frame was successfully read from the video.
The if statement then displays each frame using the imshow function. We have also set a time delay of 25 milliseconds using the waitKey function.
This function waits for a given amount of time until a keystroke event occurs, which is detected using the ‘& 0xFF == ord(‘q’)’ condition. This condition checks if the user has pressed the ‘q’ key which stops the video.
Finally, the release and destroyAllWindows functions are used to release the video object and close the window, respectively.
Loading and displaying images
To load an image in OpenCV, we use the imread function, which takes the image file name as a parameter and returns a NumPy array of the image pixels.
import cv2
image = cv2.imread('image.jpg')
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
In this example, we have loaded the image ‘image.jpg’ using the imread function. The imshow function is used to display the image, and the waitKey function waits for the user to press any key before closing the window using the destroyAllWindows function.
Playing videos
To play a video, we use the VideoCapture function by passing the video file path as a parameter. Then, we loop through the frames of the video using the read() function, and the imshow() function is used to display each frame.
import cv2
video = cv2.VideoCapture('video.mp4')
while True:
ret, frame = video.read()
if ret:
cv2.imshow('Video', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
video.release()
cv2.destroyAllWindows()
In this code example, we have used a while loop to read each frame of the video using the read function. The condition ret checks if a frame was successfully read.
Inside the loop, we use imshow to display each frame. The waitKey function waits for a given amount of time until a keystroke event occurs which is detected using the ‘& 0xFF == ord(‘q’)’ condition.
This condition checks if the user pressed the ‘q’ key, which stops the video. Finally, the release and destroyAllWindows functions are used to release the video object and close the window.
Conclusion
In conclusion, displaying images and videos using OpenCV in Python is a simple process. The OpenCV library provides various functions to read, display images, and play videos in Python.
We have seen how the waitKey function works, and how to load and display an image using the imread and imshow functions. We have also learned how to play a video using the VideoCapture and read functions, and display each frame using the imshow function.
With these functions, we can now integrate image and video manipulation into our computer vision applications in Python.
Capturing Keystroke Events
Sometimes we may need to capture keystroke events while displaying images or videos. Keystroke events allow us to pause, resume, or stop the displayed media.
The OpenCV library has a waitKey function that waits for a keystroke event and returns the ASCII code of the pressed key. In this subtopic, we will have a deep understanding of keystroke events and how to capture them using Python.
Understanding Keystroke Events
A keystroke event is an action that occurs when a user presses or releases a key on a computer keyboard. The waitKey function provided by OpenCV waits for a keystroke event and returns the ASCII code of the key that was last pressed.
The waitKey function has an optional parameter that specifies the amount of time to wait in milliseconds. It is essential to know that each key has a unique ASCII code value which is a numeric representation of the key.
Code Example for Capturing Keystroke Events
To capture keystroke events in OpenCV, we need to use the waitKey function while displaying an image or video. Here is an example:
import cv2
image = cv2.imread('image.jpg')
cv2.imshow('Image', image)
while True:
key = cv2.waitKey(0)
if key == 27:
break
elif key == ord('s'):
cv2.imwrite('saved_image.jpg', image)
print('Image saved as saved_image.jpg')
elif key == ord('q'):
break
cv2.destroyAllWindows()
In this example, we have loaded and displayed an image, just like in our previous examples. We have then used a while loop to wait for a keystroke event using the waitKey function.
Inside the loop, we have used conditional statements to check the ASCII value of the pressed key. If the ESC key (ASCII value 27) is pressed, the loop is broken.
If the ‘s’ key is pressed, we save the image to disk. If the ‘q’ key is pressed, the loop is broken to exit the program.
The destroyAllWindows function is used to close the window.
Closing Displayed Windows
When working with images and videos, we often need to close displayed windows. We can close the windows manually or automatically.
In this subtopic, we will explore how to close windows manually and automatically.
Closing Windows Manually
To close a window manually, we can use the destroyAllWindows function provided by OpenCV. The following code example demonstrates how we can use this function:
import cv2
image = cv2.imread('image.jpg')
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
In this example, we have loaded and displayed an image using the imshow function. The waitKey function waits for the user to press any key before closing the window using the destroyAllWindows function.
If we do not use the waitKey function before the destroyAllWindows function, the window will be displayed only briefly, and users may not see the image.
Closing Windows Automatically
In video playback, we may need to close the window automatically when we reach the end of the video. We can achieve this by using a break statement along with the VideoCapture, read, release, and destroyAllWindows functions.
Let us see how it works in the code example below:
import cv2
video = cv2.VideoCapture('video.mp4')
while True:
ret, frame = video.read()
if ret:
cv2.imshow('Video', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
video.release()
cv2.destroyAllWindows()
In this example, we have used a while loop to read each frame of the video using the read function. The condition ret checks if a frame was successfully read.
Inside the loop, we use imshow to display each frame. The waitKey function waits for a given amount of time until a keystroke event occurs, which is detected using the ‘& 0xFF == ord(‘q’)’ condition.
This condition checks if the user pressed the ‘q’ key, which stops the video. If the video ends or reaches the end, the loop is broken using the break statement, followed by releasing the video object using the release function and closing the window using the destroyAllWindows function.
Conclusion
Capturing keystroke events and closing displayed windows are essential aspects of working with images and videos using OpenCV in Python. The waitKey function allows us to detect keystroke events while displaying media, enabling us to pause, resume or save the displayed media.
We also learned how to close windows manually using the destroyAllWindows function and automatically using a break statement and the release and destroyAllWindows functions. By applying the concepts discussed in this article, we can create more interactive and automated applications with OpenCV in Python.
In summary, this article has explored how to display images and videos in Python using the OpenCV library. We have covered topics such as the OpenCV waitKey function, capturing keystroke events, and closing displayed windows manually or automatically.
By learning how to use these tools, we can create more interactive and automated applications with OpenCV in Python. The key takeaway from this article is the importance of understanding these essential functions and how they can be utilized to achieve desired outcomes in computer vision applications.
In conclusion, the ability to capture keystroke events and close displayed windows is crucial for displaying images and videos effectively in our applications.