Theory
Image segmentation partitions an image into regions that belong together, such as an object and its background. Semantic segmentation labels every pixel with a class; instance segmentation also separates individual objects of the same class. This lab covers the classical methods that come before deep learning.
Thresholding
The simplest segmentation compares each pixel with a threshold T: g(x, y) = 255 if f(x, y) > T, otherwise 0.
Otsu's method (1979) picks T automatically. For every candidate T it splits the histogram into two classes with weights ω₀, ω₁ and means μ₀, μ₁, and chooses the T that maximizes the between-class variance
σ_B²(T) = ω₀ ω₁ (μ₀ − μ₁)²
which is equivalent to minimizing the variance within each class.
A single global threshold fails under uneven lighting. Adaptive thresholding computes a separate threshold for each pixel from its neighbourhood: the mean or Gaussian-weighted mean of a block, minus a constant C.
K-means clustering
K-means groups pixels by color. It chooses k cluster centers, assigns each pixel to the nearest center, moves each center to the mean of its pixels, and repeats until assignments stop changing. It minimizes the sum of squared distances between pixels and their centers. The result depends on k and on the starting centers.
Watershed
The watershed algorithm treats the gradient magnitude as a landscape and floods it from markers. Where floods from different markers meet, it builds a dam: the watershed line, which becomes the boundary. Markers usually come from a distance transform of a thresholded image (sure foreground) and a dilation of it (sure background). Watershed is good at separating touching objects.
Contours
Contours are the boundaries of connected regions in a binary image, found by border following (Suzuki and Abe, 1985). They give each region's area, perimeter and bounding box, and can be overlaid on the original image.
Workbench
Segment an image with each method and overlay the mask on the original.
Input
Input preview
Parameters
Readout
- n/a
- n/a
Export
Procedure
- Load the Shapes sample and apply global thresholds of 64, 128 and 192.
- Apply Otsu's method and record the threshold it chooses.
- Load Underexposed and compare global, Otsu and adaptive thresholding (block sizes 11 and 31, C = 2).
- Segment Street at dusk with k-means for k = 2, 3, 5 and 8 and describe what each cluster captures.
- Separate touching objects with marker-based watershed and show the boundaries.
- Extract contours from the best binary mask and list the area of each region.
- Repeat steps 2 to 6 with your own photograph or a webcam frame.
- Save mask overlays for the report.
Results and Discussion
To be completed
Add your figures and observations: the threshold Otsu chose, where adaptive thresholding helped, how k changed the k-means segmentation, and how watershed handled touching objects.
Conclusion
To be completed
Summarize the strengths and limits of each segmentation method, in your own words.