Opencv Video
A video is composed of a series of consecutive image frames, and each frame is a static image.
The core of video processing is processing these image frames. Common video processing tasks include video reading, video playback, video saving, video frame processing, etc.
### Applications of Video Processing
* **Video Analysis**: Through video processing technology, motion, objects, events, etc. in videos can be analyzed.
* **Video Enhancement**: Process videos for denoising, enhancement, stabilization, etc., to improve video quality.
* **Video Editing**: Perform operations such as clipping, stitching, and adding special effects to videos.
* **Real-time Monitoring**: Monitor scenes in real-time through cameras, and perform object detection, behavior analysis, etc.
OpenCV provides two classes, `cv2.VideoCapture` and `cv2.VideoWriter`, which are used for reading and writing videos respectively. In addition, OpenCV provides rich image processing functions that can perform various operations on video frames.
## Video Reading and Playback
### Reading Video Files
To read a video file, you first need to create a `cv2.VideoCapture` object and specify the path to the video file.
## Example
```python
import cv2
# Create a VideoCapture object to read the video file
cap = cv2.VideoCapture('example.mp4')
# Check if the video is successfully opened
if not cap.isOpened():
print("Error: Could not open video.")
exit()
# Read video frames
while True:
ret, frame = cap.read()
# If the last frame is read, exit the loop
if not ret:
break
# Display the current frame
cv2.imshow('Video', frame)
# Press 'q' key to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()
### Reading Camera Video
In addition to reading video files, OpenCV can also directly read video from a camera by setting the parameter of `cv2.VideoCapture` to the camera index (usually 0):
## Example
```python
import cv2
# Create a VideoCapture object to read camera video
cap = cv2.VideoCapture(0)
# Check if the camera is successfully opened
if not cap.isOpened():
print("Error: Could not open camera.")
exit()
# Read video frames
while True:
ret, frame = cap.read()
# If the last frame is read, exit the loop
if not ret:
break
# Display the current frame
cv2.imshow('Camera', frame)
# Press 'q' key to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()
## Video Frame Processing
### Basic Frame Operations
After reading video frames, various image processing operations can be performed on each frame.
For example, you can convert a frame to a grayscale image:
## Example
```python
import cv2
cap = cv2.VideoCapture('example.mp4')
while True:
ret, frame = cap.read()
if not ret:
break
# Convert the frame to a grayscale image
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the grayscale frame
cv2.imshow('Gray Video', gray_frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
### Saving Frames
When processing video frames, sometimes it is necessary to save the processed frames as a new video file.
You can use the `cv2.VideoWriter` class to achieve this:
## Example
```python
import cv2
cap = cv2.VideoCapture('example.mp4')
# Get the frame rate and dimensions of the video
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Create a VideoWriter object to save the processed video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, fps, (width, height))
while True:
ret, frame = cap.read()
if not ret:
break
# Convert the frame to a grayscale image
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Write the grayscale frame to the output video
out.write(cv2.cvtColor(gray_frame, cv2.COLOR_GRAY2BGR))
# Display the grayscale frame
cv2.imshow('Gray Video', gray_frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
## Advanced Applications of Video Processing
### Object Detection in Video
OpenCV provides various object detection algorithms, such as Haar feature classifiers, HOG + SVM, etc.
The following is an example of face detection using Haar feature classifier:
## Example
```python
import cv2
# Load the Haar feature classifier
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture('example.mp4')
while True:
ret, frame = cap.read()
if not ret:
break
# Convert the frame to a grayscale image
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# Draw rectangles on the frame to mark faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display the frame with face markers
cv2.imshow('Face Detection', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
### Motion Detection in Video
Motion detection is an important application in video processing. Moving objects can be detected by calculating the difference between frames.
The following is a simple motion detection example:
## Example
```python
import cv2
cap = cv2.VideoCapture('example.mp4')
# Read the first frame
ret, prev_frame = cap.read()
prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
while True:
ret, frame = cap.read()
if not ret:
break
# Convert the current frame to a grayscale image
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Calculate the difference between the current frame and the previous frame
frame_diff = cv2.absdiff(prev_gray, gray_frame)
# Apply binary thresholding to the difference image
_, thresh = cv2.threshold(frame_diff, 30, 255, cv2.THRESH_BINARY)
# Display motion detection results
cv2.imshow('Motion Detection', thresh)
# Update the previous frame
prev_gray = gray_frame
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
* * *
## Common Functions
| **Function** | **Function/Method** | **Description** |
| --- | --- | --- |
| **Read Video** | `cv2.VideoCapture()` | Read video file or camera. |
| **Read Video Frame by Frame** | `cap.read()` | Read video frame by frame. |
| **Get Video Properties** | `cap.get(propId)` | Get video properties (such as width, height, frame rate, etc.). |
| **Save Video** | `cv2.VideoWriter()` | Create video writer object and save video. |
| **Video Frame Processing** | Image processing functions (such as `cv2.cvtColor()`) | Process video frames with image processing. |
| **Object Tracking** | `cv2.TrackerKCF_create()` | Use object tracking algorithm to track objects in video. |
| **Motion Detection** | `cv2.createBackgroundSubtractorMOG2()` | Use background subtraction algorithm to detect moving objects in video. |
### `cv2.VideoCapture`
**Definition**:
`cv2.VideoCapture` is used to capture video frames from video files or cameras.
**Syntax**:
```python
cv2.VideoCapture(source
YouTip