How to Use OpenCV for Basic Computer Vision Tasks: A Practical Guide to Image Processing and Motion Detection

OpenCV provides Python bindings for high-performance image processing, enabling you to load images with cv2.imread, convert color spaces with cv2.cvtColor, detect features using cv2.ORB_create, and analyze motion through optical flow—all demonstrated in the Microsoft AI-For-Beginners curriculum.

OpenCV is the de facto standard library for computer vision in Python, wrapping efficient C++ implementations behind a simple NumPy-compatible API. According to the microsoft/AI-For-Beginners repository, the lessons/4-ComputerVision/06-IntroCV/OpenCV.ipynb notebook provides hands-on examples covering everything from color space conversions to real-time motion analysis. This guide extracts the essential workflows so you can implement fundamental computer vision tasks without getting lost in documentation.

Loading and Converting Images in OpenCV

OpenCV reads images in BGR format (Blue-Green-Red) by default, which requires conversion for correct display in Matplotlib or other RGB-based tools. In lessons/4-ComputerVision/06-IntroCV/OpenCV.ipynb, the cv2.imread function loads an image as a NumPy array, while cv2.cvtColor handles the color space transformation.

import cv2
import matplotlib.pyplot as plt

# Load image in BGR format

img_bgr = cv2.imread('data/braille.jpeg')

# Convert to RGB for proper visualization

img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)

plt.imshow(img_rgb)
plt.title('Original (RGB)')
plt.axis('off')
plt.show()

Pre-processing with Adaptive Thresholding

For document analysis and segmentation tasks, adaptive thresholding handles uneven lighting better than global thresholding. The cv2.adaptiveThreshold function computes thresholds for small regions of the image, producing cleaner binary results.

import cv2

# Convert to grayscale first

gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)

# Apply adaptive threshold

thr = cv2.adaptiveThreshold(
    gray, 255,
    cv2.ADAPTIVE_THRESH_MEAN_C,
    cv2.THRESH_BINARY_INV,
    blockSize=5, C=4)

plt.imshow(thr, cmap='gray')
plt.show()

Feature Detection Using ORB

The Oriented FAST and Rotated BRIEF (ORB) algorithm provides rotation-invariant feature detection without the patent restrictions of SIFT or SURF. In the notebook, cv2.ORB_create initializes the detector, and cv2.drawKeypoints visualizes the results.


# Create ORB detector with maximum 5000 features

orb = cv2.ORB_create(5000)
kp, des = orb.detectAndCompute(thr, None)

# Visualize keypoints

vis = cv2.drawKeypoints(thr, kp, None,
                       color=(0,255,0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

plt.imshow(vis, cmap='gray')
plt.title('ORB key-points')
plt.axis('off')
plt.show()

Geometric Transforms and Perspective Correction

To straighten documents or extract planar regions, OpenCV uses homography estimation via cv2.findHomography followed by cv2.warpPerspective. This computes a transformation matrix from four corresponding point pairs and warps the image accordingly.

import numpy as np

# Define source and destination points

src_pts = np.float32([[50,30], [500,30], [50,300], [500,300]])
dst_pts = np.float32([[0,0], [450,0], [0,300], [450,300]])

# Compute homography and warp

H, _ = cv2.findHomography(src_pts, dst_pts)
warped = cv2.warpPerspective(img_bgr, H, (450,300))

plt.imshow(cv2.cvtColor(warped, cv2.COLOR_BGR2RGB))
plt.title('Perspective-corrected')
plt.show()

Motion Detection Techniques

OpenCV processes video streams through cv2.VideoCapture, enabling two primary motion analysis approaches. Frame differencing provides lightweight motion detection by comparing consecutive grayscale frames, while dense optical flow calculates motion vectors for every pixel using cv2.calcOpticalFlowFarneback.

For simple motion detection:

cap = cv2.VideoCapture('data/motionvideo.mp4')
ret, prev = cap.read()
prev_gray = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)

while True:
    ret, cur = cap.read()
    if not ret:
        break
    cur_gray = cv2.cvtColor(cur, cv2.COLOR_BGR2GRAY)
    diff = cv2.absdiff(cur_gray, prev_gray)
    _, motion = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY)
    cv2.imshow('Motion', motion)
    if cv2.waitKey(30) & 0xFF == ord('q'):
        break
    prev_gray = cur_gray

cap.release()
cv2.destroyAllWindows()

For dense optical flow visualization:

cap = cv2.VideoCapture('data/motionvideo.mp4')
ret, f1 = cap.read()
prev = cv2.cvtColor(f1, cv2.COLOR_BGR2GRAY)

while True:
    ret, f2 = cap.read()
    if not ret:
        break
    cur = cv2.cvtColor(f2, cv2.COLOR_BGR2GRAY)
    flow = cv2.calcOpticalFlowFarneback(prev, cur, None, 0.5, 3, 15, 3, 5, 1.2, 0)
    
    # Convert flow to HSV color coding

    mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
    hsv = np.zeros((flow.shape[0], flow.shape[1], 3), dtype=np.uint8)
    hsv[...,0] = ang * 180 / np.pi / 2
    hsv[...,1] = 255
    hsv[...,2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
    bgr_flow = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

    cv2.imshow('Optical Flow', bgr_flow)
    if cv2.waitKey(30) & 0xFF == ord('q'):
        break
    prev = cur

cap.release()
cv2.destroyAllWindows()

Summary

  • Color space handling: Always convert BGR to RGB using cv2.cvtColor when displaying with Matplotlib to avoid color distortion.
  • Image binarization: Use cv2.adaptiveThreshold instead of global thresholding for documents with uneven illumination.
  • Feature extraction: cv2.ORB_create provides a fast, patent-free alternative to SIFT for detecting keypoints and descriptors.
  • Geometric correction: Compute transformation matrices with cv2.findHomography and apply them with cv2.warpPerspective to align document perspectives.
  • Motion analysis: Choose frame differencing for lightweight detection or cv2.calcOpticalFlowFarneBack for dense motion vector fields.

Frequently Asked Questions

Why does OpenCV use BGR instead of RGB?

OpenCV uses BGR color ordering for historical reasons related to early camera standards and the Windows color format. When displaying images with Matplotlib or saving for web use, you must convert using cv2.cvtColor(image, cv2.COLOR_BGR2RGB) to prevent color channel reversal.

What is the difference between ORB and SIFT feature detection?

ORB (Oriented FAST and Rotated BRIEF) is a faster, open-source alternative to SIFT (Scale-Invariant Feature Transform) that offers similar rotation invariance without patent restrictions. According to the microsoft/AI-For-Beginners source code, cv2.ORB_create is the recommended choice for beginner projects requiring robust feature matching.

How does dense optical flow differ from frame differencing?

Frame differencing computes absolute pixel differences between consecutive frames to detect motion regions, while dense optical flow (cv2.calcOpticalFlowFarneBack) calculates motion vectors for every pixel, encoding both direction and magnitude. Optical flow provides richer motion information but requires more computation.

Where can I find the complete OpenCV tutorial notebook?

The full interactive tutorial is located at lessons/4-ComputerVision/06-IntroCV/OpenCV.ipynb in the microsoft/AI-For-Beginners repository. The accompanying README.md in the same directory provides additional context and links to external resources like the official OpenCV documentation.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →