ML Notes
Essential image processing fundamentals covering pixel operations, filtering, edge detection, morphological operations, and color space transformations with OpenCV.
Before you can build sophisticated computer vision models, you need to understand how to manipulate images at the pixel level. Image processing is the toolkit that prepares raw images for machine learning — cleaning noise, enhancing contrast, detecting edges, and extracting useful features. Every computer vision pipeline begins with image processing steps, making this knowledge essential regardless of whether you use classical methods or deep learning.
How Digital Images Work
A digital image is fundamentally a matrix of numbers. Each number represents the intensity of light at a specific position (pixel). Understanding this numerical representation is the foundation of all image processing.
For grayscale images, each pixel is a single number from 0 (pure black) to 255 (pure white). For color images in RGB format, each pixel has three numbers representing the intensity of Red, Green, and Blue light. When all three channels are 255, you get white. When all are 0, you get black. When Red=255 and others=0, you get pure red.
Color Space Transformations
Different color spaces represent color information differently, and choosing the right one can dramatically improve your processing results:
RGB/BGR: The standard representation. Good for display but components are correlated (changing brightness affects all three channels).
HSV (Hue, Saturation, Value): Separates color information (Hue) from intensity (Value). Perfect for color-based object detection because changing lighting only affects V, not H.
Grayscale: Single channel, used when color is irrelevant (edge detection, texture analysis).
Image Filtering: Smoothing and Sharpening
Filters modify pixel values based on their neighborhood. They are implemented as small matrices (kernels) that slide across the image, computing a weighted average at each position. This operation is called convolution — the same mathematical operation that powers CNNs.
The intuition behind filtering: a blur filter averages a pixel with its neighbors, smoothing out rapid changes (noise). A sharpening filter does the opposite — it amplifies differences between a pixel and its neighbors, making edges more prominent.
Edge Detection: Finding Boundaries
Edges are locations where pixel intensity changes rapidly — they correspond to object boundaries, texture changes, and shadows. Detecting edges is one of the most fundamental operations in computer vision.
# Canny edge detection — the gold standard
# Automatically finds optimal thresholds using gradients
edges_canny = cv2.Canny(img_gray, threshold1=50, threshold2=150)
# Sobel operator — detects edges in specific directions
sobel_x = cv2.Sobel(img_gray, cv2.CV_64F, 1, 0, ksize=3) # Horizontal edges
sobel_y = cv2.Sobel(img_gray, cv2.CV_64F, 0, 1, ksize=3) # Vertical edges
sobel_combined = cv2.magnitude(sobel_x, sobel_y)
# Laplacian — detects edges in all directions simultaneously
laplacian = cv2.Laplacian(img_gray, cv2.CV_64F)The Canny edge detector works in multiple stages: smooth the image to reduce noise, compute intensity gradients (rate of change), suppress non-maximum pixels (thin edges to single-pixel width), and apply hysteresis thresholding (connect weak edges to strong ones). This multi-stage approach produces clean, connected edge maps that are useful for subsequent processing.
Morphological Operations: Shape-Based Processing
Morphological operations process images based on shape. They use a small structuring element (kernel) to probe the image, expanding or shrinking regions. These are essential for cleaning up binary masks and extracting shape features.
# Create a structuring element (kernel)
kernel = np.ones((5, 5), np.uint8)
# Erosion — shrinks white regions, removes small noise
eroded = cv2.erode(binary_img, kernel, iterations=1)
# Dilation — expands white regions, fills small holes
dilated = cv2.dilate(binary_img, kernel, iterations=1)
# Opening (erosion then dilation) — removes small noise while preserving shape
opened = cv2.morphologyEx(binary_img, cv2.MORPH_OPEN, kernel)
# Closing (dilation then erosion) — fills small holes while preserving shape
closed = cv2.morphologyEx(binary_img, cv2.MORPH_CLOSE, kernel)Thresholding: Converting to Binary Images
Thresholding separates foreground from background by converting grayscale images to binary (black and white). Simple thresholding uses a fixed value, while adaptive thresholding adjusts based on local pixel neighborhoods — essential for images with varying illumination.
# Simple threshold
_, binary = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
# Otsu's method — automatically finds optimal threshold
_, otsu = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Adaptive threshold — handles uneven lighting
adaptive = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2)Histogram Equalization: Improving Contrast
When an image uses only a narrow range of intensities (looks washed out or too dark), histogram equalization redistributes pixel values to span the full 0-255 range, dramatically improving contrast and visibility of details.
# Standard histogram equalization
equalized = cv2.equalizeHist(img_gray)
# CLAHE — adaptive equalization that prevents over-amplification
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
clahe_result = clahe.apply(img_gray)Building an Image Processing Pipeline
In practice, you chain multiple operations into a pipeline. For example, a typical preprocessing pipeline for object detection might be: convert to grayscale, apply Gaussian blur to reduce noise, use adaptive thresholding to segment the object, apply morphological opening to clean noise, then find contours to locate the object boundary.
Understanding these building blocks gives you the vocabulary to design custom pipelines for any computer vision task. Even when using deep learning, proper image preprocessing significantly improves model performance.
Performance Optimization and Best Practices
When implementing image processing solutions in production, performance optimization becomes crucial. Start by profiling your code to identify bottlenecks — often the slowest step is not where you expect. Consider batch processing multiple images simultaneously to amortize overhead costs. Use appropriate data types (uint8 for pixel values, float32 for computations) and avoid unnecessary type conversions that waste memory and CPU cycles. Pre-allocate output arrays rather than growing them dynamically, and leverage vectorized NumPy operations over Python loops wherever possible. These optimizations can improve throughput by 10-100x for real-time applications.
Common Debugging Strategies
Debugging image processing code requires visual inspection at each pipeline stage. Display intermediate results to verify that each transformation produces expected output. Check array shapes and value ranges after every operation — a common bug is accidentally normalizing values to 0-1 when your function expects 0-255. Use assertions liberally during development to catch shape mismatches early. When results look wrong, work backwards from the output to find where the pipeline first deviates from expectations. Building this systematic debugging mindset saves hours of frustration and produces more reliable systems.
Key Takeaways
Image processing transforms raw pixel grids into useful visual information through filtering, edge detection, morphological operations, and thresholding. Master these fundamentals with OpenCV and you have the toolkit to preprocess images for any machine learning pipeline, debug computer vision systems, and build classical CV solutions that work without deep learning infrastructure.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Image Processing 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, image, processing
Related Machine Learning Topics