ML Notes
Essential OpenCV tutorial covering image I/O, drawing, transformations, video capture, contour detection, and building real-time computer vision applications with Python.
OpenCV (Open Source Computer Vision Library) is the most widely used computer vision library in the world, with over 2500 optimized algorithms for image and video processing. Whether you are building a face detection system, processing satellite imagery, or creating augmented reality applications, OpenCV provides the foundational tools. This lesson covers essential OpenCV operations that you will use in virtually every computer vision project.
Installing and Importing OpenCV
OpenCV for Python is installed via pip and imported as cv2. The library provides both high-level functions for common tasks and low-level access to pixel data through NumPy array integration. Every image in OpenCV is a NumPy array, meaning you can use all NumPy operations directly on images.
import cv2
import numpy as np
# Check OpenCV version
print(f"OpenCV version: {cv2.__version__}")Reading, Displaying, and Saving Images
The most basic operations are loading images from disk, displaying them, and saving processed results. OpenCV uses BGR color order (not RGB like most other libraries), which is important to remember when converting between libraries or displaying with matplotlib.
# Read image (returns NumPy array)
img = cv2.imread('photo.jpg') # Color (BGR)
img_gray = cv2.imread('photo.jpg', 0) # Grayscale
img_unchanged = cv2.imread('photo.jpg', -1) # With alpha channel
# Check properties
print(f"Shape: {img.shape}") # (height, width, channels)
print(f"Size: {img.size}") # total pixel values
print(f"Dtype: {img.dtype}") # uint8 (0-255)
# Display image (opens a window)
cv2.imshow('Window Title', img)
cv2.waitKey(0) # Wait for any key press
cv2.destroyAllWindows()
# Save processed image
cv2.imwrite('output.jpg', img)
cv2.imwrite('output.png', img) # PNG for lossless savingDrawing on Images
OpenCV provides functions to draw shapes, lines, and text on images — essential for visualizing detection results, annotations, and debugging.
# Create a blank canvas or draw on existing image
canvas = np.zeros((500, 500, 3), dtype=np.uint8)
# Draw shapes (note: operations modify image in-place)
cv2.line(canvas, (0, 0), (500, 500), (0, 255, 0), 2) # Green line
cv2.rectangle(canvas, (100, 100), (400, 400), (255, 0, 0), 3) # Blue rectangle
cv2.circle(canvas, (250, 250), 100, (0, 0, 255), -1) # Red filled circle
cv2.putText(canvas, 'OpenCV!', (150, 470), cv2.FONT_HERSHEY_SIMPLEX,
1.5, (255, 255, 255), 2) # White text
# Draw bounding box with label (common in object detection)
def draw_bbox(img, x1, y1, x2, y2, label, color=(0, 255, 0)):
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.6, color, 2)Geometric Transformations
Transforming image geometry is essential for data augmentation, alignment, and perspective correction.
Video Capture and Processing
OpenCV handles video through the VideoCapture class, processing one frame at a time in a loop. This enables real-time applications like face detection, motion tracking, and augmented reality.
# Capture from webcam
cap = cv2.VideoCapture(0) # 0 = default camera
# Set camera properties
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Process each frame (e.g., convert to grayscale, detect edges)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
cv2.imshow('Live Feed', frame)
cv2.imshow('Edges', edges)
# Press 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# Process video file
cap = cv2.VideoCapture('video.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"Video: {frame_count} frames at {fps} FPS")Contour Detection
Contours are curves joining continuous points along a boundary with the same intensity. They are essential for shape detection, object counting, and extracting object boundaries from binary images.
# Convert to binary image first
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Find contours
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(f"Found {len(contours)} objects")
# Draw all contours
result = img.copy()
cv2.drawContours(result, contours, -1, (0, 255, 0), 2)
# Analyze each contour
for i, contour in enumerate(contours):
area = cv2.contourArea(contour)
perimeter = cv2.arcLength(contour, closed=True)
x, y, w, h = cv2.boundingRect(contour)
# Filter by area (ignore small noise)
if area > 500:
cv2.rectangle(result, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.putText(result, f"Area: {area}", (x, y-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)Region of Interest (ROI) Operations
Working with specific regions of an image is a fundamental skill. ROIs let you focus processing on relevant areas, reducing computation and improving accuracy for targeted tasks like license plate reading or face analysis.
Integration with Deep Learning Frameworks
OpenCV seamlessly integrates with TensorFlow and PyTorch for preprocessing images before feeding them to neural networks, and for post-processing network outputs for visualization.
Practical Implementation Considerations
When deploying OpenCV solutions in production environments, several practical considerations become important. First, preprocessing must be consistent between training and inference — if you normalized images to a specific range during training, the same normalization must be applied at inference time. Second, consider the computational budget available: edge devices may require model compression techniques like quantization (reducing weight precision from 32-bit to 8-bit) or pruning (removing unimportant connections). Third, monitor model performance over time since real-world data distributions shift — a model trained on summer images may degrade in winter conditions without periodic retraining on fresh data.
Debugging and Troubleshooting
When OpenCV models produce unexpected results, systematic debugging is essential. Start by visualizing predictions on individual examples to understand failure modes. Check that input preprocessing matches training exactly — even subtle differences in normalization or resizing can cause significant accuracy drops. Verify that your evaluation metrics match the competition or paper you are comparing against, as different IoU thresholds or averaging methods can give very different numbers. When training diverges or produces poor results, reduce learning rate, verify label correctness on a random sample, and ensure data augmentation is not too aggressive for your dataset size.
Key Takeaways
OpenCV is your essential toolkit for all computer vision work in Python. Master the basics — reading and writing images, geometric transformations, drawing annotations, video capture, and contour detection — and you can build any CV application. Remember that OpenCV uses BGR color order, images are NumPy arrays (enabling all NumPy operations), and most functions modify images in-place. Start with these fundamentals and progressively add more advanced operations as your projects require them.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for OpenCV Basics.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, computer, vision, opencv, basics
Related Machine Learning Topics