Classical Image Segmentation
How traditional computer vision techniques separate foreground from background using pixel intensity, edges, and regions without deep learning.
Why Does This Exist?
Before convolutional neural networks dominated computer vision, algorithms needed a way to separate objects of interest from their background. Classical image segmentation relies on mathematical and heuristic approaches—like pixel intensity thresholding, edge detection, and region growing—to group pixels into meaningful structures. These techniques remain crucial for domains like medical imaging and industrial inspection where annotated training data is scarce or strict deterministic behavior is required.
Think of It Like This
Sorting laundry by color
Imagine you have a giant pile of laundry and you need to separate the white clothes from the colored ones. You don't need to know what a "shirt" or a "sock" is; you just look at the brightness of each item. If it's pure white, it goes in one pile; if it's dark, it goes in another. This is exactly how global thresholding works—it separates pixels based purely on their intensity value, completely ignoring their semantic meaning.
How It Actually Works
Classical segmentation typically groups pixels using one of three fundamental properties: intensity, edges, or regions.
1. Thresholding (Intensity-based)
The simplest approach converts a grayscale image into a binary mask. A threshold value is chosen. If a pixel's intensity is greater than , it is classified as foreground (1); otherwise, it becomes background (0). Otsu's method automatically calculates the optimal by minimizing the intra-class variance between the foreground and background pixels.
2. Edge Detection
Objects are often bounded by sharp changes in brightness. Edge-based segmentation computes the image gradient (using operators like Sobel or Canny) to find these boundaries. The gradient magnitude represents the strength of the edge:
Once edges are found, morphological operations can connect them to form closed contours around objects.
3. Region Growing
Starting from a set of "seed" pixels, this algorithm iteratively adds neighboring pixels to a region if they are similar enough (e.g., their intensity difference is below a certain tolerance). It continues growing until no more similar neighbors can be found, effectively grouping connected, homogenous areas.
Code
import cv2import numpy as np
# -> Load a grayscale imageimage = cv2.imread("coins.jpg", cv2.IMREAD_GRAYSCALE)
# -> Apply Otsu's thresholding to separate foreground and background# -> Returns the calculated threshold and the binary maskthresh_val, binary_mask = cv2.threshold( image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# -> binary_mask is now an array of 0s (background) and 255s (foreground)Watch Out For
Sensitivity to lighting changes
Global thresholding fails completely if the image has uneven illumination. A shadow falling across half the image will cause dark foreground objects in the lit area and bright background pixels in the shadowed area to be grouped together incorrectly. Adaptive thresholding (calculating locally) is required to fix this.
No semantic understanding
Classical methods group pixels by raw visual similarity, not meaning. If a dog and a rug have the exact same texture and color, classical region growing will merge them into a single segment. It cannot distinguish objects based on context.
The Quick Version
- Thresholding: Separates pixels into foreground/background based on a cutoff intensity value.
- Edge detection: Finds object boundaries by looking for sharp gradients in pixel brightness.
- Region growing: Starts at a seed pixel and expands outward to neighbors with similar colors.
- Watershed: Treats intensity as a topographic map to separate touching objects.
- No deep learning: These methods require zero training data but rely heavily on clean, well-lit input images.