Image and Video Processing with OpenCV

OpenCV (Open Source Computer Vision Library) is a leading open-source library for computer vision and machine learning. It aims to provide a common infrastructure for computer vision applications and facilitate the rapid use of machine perception in commercial products.

Setting Up OpenCV

To use OpenCV with Python, you need to install the OpenCV package:

pip install opencv-python

Basic Image Processing

Reading, Displaying, and Writing Images

Here’s how you can read, display, and save an image:


import cv2
# Read the image
image = cv2.imread('path_to_image.jpg')
# Display the image
cv2.imshow('Image', image)
cv2.waitKey(0)
# Save the image
cv2.imwrite('output.jpg', image)

Basic Operations on Images

Operations such as cropping, resizing, and rotating:

See also  Image Blending with OpenCV's addWeighted Function


# Cropping
cropped_image = image[y:y+h, x:x+w]

# Resizing
resized_image = cv2.resize(image, (new_width, new_height))

# Rotating
center = (w / 2, h / 2)
matrix = cv2.getRotationMatrix2D(center, angle, scale)
rotated_image = cv2.warpAffine(image, matrix, (w, h))

Basic Video Processing

Processing video frames using OpenCV:


# Capture video
cap = cv2.VideoCapture('path_to_video.mp4')

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

Processing Images and Videos Frame by Frame

Processing images and videos frame by frame is a common task in video analysis. This section will guide you through the basics of handling video frames using OpenCV.

Reading and Processing Video Frames

To process a video, you need to capture it using OpenCV’s VideoCapture, and then iterate over each frame. Here’s an example:


import cv2
# Capture the video
cap = cv2.VideoCapture('path_to_video.mp4')

while cap.isOpened():
   ret, frame = cap.read()
   if not ret:
      break
   # Process each frame
   # For example, convert to grayscale
   gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
   cv2.imshow('Frame', gray_frame)
   if cv2.waitKey(1) & 0xFF == ord('q'):
      break
cap.release()
cv2.destroyAllWindows()

This script reads a video file, converts each frame to grayscale, and displays it. The loop breaks when the video ends, or the user presses ‘q’.

Saving Processed Frames

You can also save the processed frames to create a new video. Here’s how to do it:


# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

while cap.isOpened():
   # ... (frame processing)
   out.write(gray_frame) # Write the frame
# ... (rest of the loop)

This manual provides a basic introduction to image and video processing with OpenCV in Python. For more advanced techniques and applications, further exploration of OpenCV’s documentation and resources is recommended.