OpenCV Video Processing

A video is composed of a series of consecutive image frames, each frame being a static image.

The core of video processing is to process these image frames. Common video processing tasks include video reading, video playback, video saving, video frame processing, and so on.

Applications of Video Processing

  • Video analysis: Through video processing techniques, motion, objects, events, etc., in a video can be analyzed.
  • Video enhancement: Perform denoising, enhancement, stabilization, and other processing on the video to improve video quality.
  • Video editing: Perform operations such as cutting, splicing, and adding special effects to the video.
  • Real-time monitoring: Monitor scenes in real time through cameras, and perform object detection, behavior analysis, etc.

OpenCV providescv2.VideoCaptureandcv2.VideoWritertwo classes, used for video reading and writing respectively. In addition, OpenCV also provides a rich set of 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 acv2.VideoCaptureobject, and specify the path of the video file.

Example

import cv2

# Create a VideoCapture object to read the video file
cap = cv2.VideoCapture('example.mp4')

# Check whether the video was opened successfully
if not cap.isOpened():
    print("Error: Could not open video.")
    exit()

# Read a video frame
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' 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 read video directly from a camera by simply settingcv2.VideoCapturethe parameter to the camera index (usually 0):

Example

import cv2

# Create a VideoCapture object to read camera video
cap = cv2.VideoCapture(0)

# Check whether the camera was opened successfully
if not cap.isOpened():
    print("Error: Could not open camera.")
    exit()

# Read a video frame
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' to exit
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break

# Release resources
cap.release()
cv2.destroyAllWindows()

Video Frame Processing

Basic Operations on Frames

After reading video frames, various image processing operations can be performed on each frame.

For example, the frame can be converted to a grayscale image:

Example

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, it is sometimes necessary to save the processed frames as a new video file.

You can use thecv2.VideoWriterclass to achieve this:

Example

import cv2

cap = cv2.VideoCapture('example.mp4')

# Get the frame rate and size 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 a variety of object detection algorithms, such as Haar feature classifiers, HOG + SVM, etc.

The following is an example of face detection using a Haar feature classifier:

Example

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 computing the differences between frames.

The following is a simple motion detection example:

Example

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)
   
    # Compute the difference between the current frame and the previous frame
    frame_diff = cv2.absdiff(prev_gray, gray_frame)
   
    # Binarize the difference image
    _, thresh = cv2.threshold(frame_diff, 30, 255, cv2.THRESH_BINARY)
   
    # Display the motion detection result
    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

FunctionFunction/MethodDescription
Read videocv2.VideoCapture()Read a video file or camera.
Read video frame by framecap.read()Read video frame by frame.
Get video propertiescap.get(propId)Get video properties (such as width, height, frame rate, etc.).
Save videocv2.VideoWriter()Create a video writer object and save the video.
Video frame processingImage processing functions (such ascv2.cvtColor())Perform image processing on video frames.
Object trackingcv2.TrackerKCF_create()Use object tracking algorithms to track objects in video.
Motion detectioncv2.createBackgroundSubtractorMOG2()Use background subtraction algorithms to detect moving objects in video.

cv2.VideoCapture

Definition:
cv2.VideoCaptureUsed to capture video frames from a video file or camera.

Syntax:

cv2.VideoCapture(source)

Parameter description:

  • source: Video file path or camera index (usually 0 for the default camera).

Example

import cv2

# Open the default camera
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break
    cv2.imshow('Frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

cv2.VideoWriter

Definition:
cv2.VideoWriterUsed to write video frames to a video file.

Syntax:

cv2.VideoWriter(filename, fourcc, fps, frameSize)

Parameter description:

  • filename: Output video file name.
  • fourcc: Video codec (such ascv2.VideoWriter_fourcc(*'XVID'))。
  • fps: Frame rate.
  • frameSize: Frame size (width, height).

Example

import cv2

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    out.write(frame)
    cv2.imshow('Frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()

cv2.cvtColor

Definition:
cv2.cvtColorUsed to convert an image from one color space to another color space.

Syntax:

cv2.cvtColor(src, code)

Parameter description:

  • src: Input image.
  • code: Color space conversion code (such ascv2.COLOR_BGR2GRAY)。

Example

import cv2

img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.resize

Definition:
cv2.resizeUsed to resize an image.

Syntax:

cv2.resize(src, dsize)

Parameter description:

  • src: Input image.
  • dsize: Output image size (width, height).

Example

import cv2

img = cv2.imread('image.jpg')
resized = cv2.resize(img, (320, 240))
cv2.imshow('Resized Image', resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.Canny

Definition:
cv2.CannyUsed for edge detection.

Syntax:

cv2.Canny(image, threshold1, threshold2)

Parameter description:

  • image: Input image.
  • threshold1: First threshold.
  • threshold2: Second threshold.

Example

import cv2

img = cv2.imread('image.jpg', 0)
edges = cv2.Canny(img, 100, 200)
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.findContours

Definition:
cv2.findContoursUsed to find contours in an image.

Syntax:

cv2.findContours(image, mode, method)

Parameter description:

  • image: Input image.
  • mode: Contour retrieval mode (such ascv2.RETR_TREE)。
  • method: Contour approximation method (such ascv2.CHAIN_APPROX_SIMPLE)。

Example

import cv2

img = cv2.imread('image.jpg', 0)
contours, _ = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
cv2.imshow('Contours', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.drawContours

Definition:
cv2.drawContoursUsed to draw contours in an image.

Syntax:

cv2.drawContours(image, contours, contourIdx, color, thickness)

Parameter description:

  • image: Input image.
  • contours: List of contours.
  • contourIdx: Contour index (-1 means draw all contours).
  • color: Contour color.
  • thickness: Contour line width.

Example

import cv2

img = cv2.imread('image.jpg', 0)
contours, _ = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
cv2.imshow('Contours', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.putText

Definition:
cv2.putTextUsed to draw text on an image.

Syntax:

cv2.putText(image, text, org, fontFace, fontScale, color, thickness)

Parameter description:

  • image: Input image.
  • text: The text to be drawn.
  • org: Coordinates of the bottom-left corner of the text.
  • fontFace: Font type (such ascv2.FONT_HERSHEY_SIMPLEX)。
  • fontScale: Font scaling factor.
  • color: Text color.
  • thickness: Text line width.

Example

import cv2

img = cv2.imread('image.jpg')
cv2.putText(img, 'Hello, OpenCV!', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2.imshow('Text', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Other Extensions