Overview
OpenCV is a powerful computer vision library that is widely used in areas such as image processing and video processing. This article will explain in detail how to use OpenCV to read and display images and videos in Python, and demonstrate the entire process with specific code examples.
Environmental preparation
Before you begin, make sure that the OpenCV library is installed. If not installed, you can install it through the following command:
pip install opencv-python
Complete code
import cv2 def read_img(): """ Read pictures :return: """ img = (r'./resources/') ('lena img', img) (0) def read_video(): """ Read video/camera :return: """ # The video path is to obtain the resource, the id number is the camera id, and 0 is the default first camera id cap = (r'./resources/video.mp4') # cap = (0) # cap propId 0-18 (3, 800) #3 is wide (4, 600) #4 is high (10, 5) #10 is brightness while True: success, img = () if success: ('video', img) else: break if (1) & 0xFF == ord('q'): # If there is a button, return to the ASCII code of the key, return to -1 if there is no button. # Get the key to return to the ASCII binary 8 digits, and exit the loop for the key 'q' break () () if __name__ == '__main__': # read_img() read_video()
Sample code details
1. Read and display images
import cv2 def read_img(): """ Read pictures :return: """ img = (r'./resources/') ('lena img', img) (0)
Read the image:
img = (r'./resources/')
Use the () function to read the image file. The path uses the original string (r'...') to avoid the problem of escaping characters.
Show image:
('lena img', img) (0)
Use the () function to display the image. The first parameter is the window name, and the second parameter is the image to be displayed.
(0) Make the program exit after waiting for the user to press the key. 0 means waiting indefinitely.
2. Read and display video
def read_video(): """ Read video/camera :return: """ # The video path is to obtain the resource, the id number is the camera id, and 0 is the default first camera id cap = (r'./resources/video.mp4') # cap = (0) # cap propId 0-18 (3, 800) #3 is wide (4, 600) #4 is high (10, 5) #10 is brightness while True: success, img = () if success: ('video', img) else: break if (1) & 0xFF == ord('q'): # If there is a button, return to the ASCII code of the key, return to -1 if there is no button. # Get the key to return to the ASCII binary 8 digits, and exit the loop for the key 'q' break () ()
Read video or camera:
cap = (r'./resources/video.mp4') # cap = (0)
Use the () function to read a video file or camera. Pass in video file path or camera ID (for example, 0 means the default camera).
Set video properties:
(3, 800) #3 is wide(4, 600) #4 is high(10, 5) #10 is brightness
Use the () method to set the video capture properties. Parameters 3 represents width, 4 represents height, and 10 represents brightness.
Read and display video frames:
while True: success, img = () if success: ('video', img) else: break
Use the () method to read the video frame. If the read is successful, success is True and img is the image of the current frame; otherwise, success is False, indicating that the video has ended or the reading has failed.
Key detection:
if (1) & 0xFF == ord('q'): break
Use (1) Wait for 1 millisecond, if a key is pressed, return to the ASCII code of the key. & 0xFF is used to get the last 8 digits of the ASCII code of the key. ord('q') returns the ASCII code of the character 'q'. If the key is ‘q’, the loop is exited.
Release resources:
() ()
Use () to release the video capture object.
Use () to close all OpenCV windows.
Main function
if __name__ == '__main__': # read_img() read_video()
Main function:
if __name__ == '__main__': Make sure the following code is executed only when the script is run directly.
Call the read_img() or read_video() functions to read and display images or videos.
Summarize
This article details how to use OpenCV to read and display images and videos in Python, and shows the entire process with specific code examples. By using functions such as (), (), (), (), and (), we can easily process image and video data.
This is the article about using OpenCV to read and display images and videos. For more related OpenCV reading and display images and video content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!