Image ProcessingUnit 512 min read
Image Segmentation: Techniques, Algorithms & Real-World Applications
Unit 5 of Image Processing covers pixel/region-based segmentation methods, thresholding, edge detection, clustering, and morphological operations—explained with visuals, real-world examples (e.g., medical imaging, autonomous vehicles), and step-by-step calculations for TU/PU exams.
TAKEAWAYS:
- Segmentation splits images into meaningful regions (objects/background) using pixel similarity, edges, or clustering—critical for object recognition in apps like Pathao’s traffic analysis or NTC’s license plate detection.
- Thresholding (global/local) and edge detection (Sobel, Canny) are fast but fail on uneven lighting; region-growing and clustering (K-means) handle complex scenes better.
- Morphological operations (erosion/dilation) clean up noise but distort shapes—use them after segmentation (e.g., Daraz’s product bounding boxes).
- Worked examples show how to compute thresholds, trace edge-detection steps, and compare segmentation accuracy using real images (e.g., a blurry traffic sign).
- Exam focus: Define methods, draw block diagrams (e.g., Canny edge detector pipeline), and calculate segmentation metrics (e.g., IoU for a 5×5 grid).
- Real-world tie: WhatsApp’s photo filters use segmentation to isolate faces; Nepal Police’s drone surveillance segments suspicious vehicles from backgrounds.
1. What Is Image Segmentation?
Segmentation divides an image into discrete, meaningful regions (e.g., foreground objects, textures). Unlike enhancement (which improves pixel values), segmentation groups pixels into logical units for analysis.
graph TD
A["Input Image"] --> B["Preprocessing\n(Noise Reduction)"]
B --> C["Segmentation\n(Pixel/Region/Edge-based)"]
C --> D["Post-processing\n(Morphology, Merging)"]
D --> E["Output:\nLabeled Regions"]Why it matters:
- Object detection: Pathao’s app segments riders from traffic to optimize routes.
- Medical imaging: Doctors segment tumors in X-rays for surgery planning.
- Autonomous vehicles: Ncell’s 5G-enabled cars segment pedestrians from roads.
2. Key Segmentation Methods
A. Pixel-Based: Thresholding
Idea: Split pixels into foreground/background using a global or local threshold. How it works:
- Choose a threshold . Pixels = object; = background.
- Global thresholding (e.g., Otsu’s method) picks for the whole image.
- Local thresholding adapts per region (e.g., Bernsen’s method for uneven lighting).
Worked Example: Otsu’s Thresholding Given a histogram:
| Gray Level | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Pixels | 300 | 500 | 1000 | 800 | 600 | 400 | 200 | 100 |
Steps:
- Compute class probabilities . , , etc.
- Calculate weighted mean . , , , etc.
- Find that maximizes inter-class variance: , where , .
Result: (highest ). Output:
graph TD
A["Input Image"] -->|"T=3"| B["Segmented\nObject (gray ≥3)"]
B --> C["Background\n(gray <3)"]Limitations:
- Fails on multi-modal images (e.g., blood cells in microscopy).
- Real fix: Use adaptive thresholding (e.g., for NTC’s license plate scans with shadows).
B. Edge-Based: Detecting Boundaries
Idea: Find edges (sudden intensity changes) to outline objects. Operators:
| Operator | Method | Output | Best For |
|---|---|---|---|
| Sobel | Gradient magnitude | Thick edges | Noise-free images |
| Prewitt | Similar to Sobel but simpler | Blurry edges | Low-contrast scenes |
| Canny | 5-step pipeline (noise → edges) | Thin, accurate edges | Medical imaging |
Worked Example: Sobel Edge Detection For a 3×3 pixel patch:
[ 100 120 100 ]
[ 120 150 120 ]
[ 100 120 100 ]
Steps:
- Apply Sobel kernels: , .
- Compute gradients: , .
- Magnitude: .
- Threshold: If , mark as edge.
Canny Edge Detector Pipeline (5 steps):
flowchart LR
A["1. Noise Reduction\n(Gaussian blur)"] --> B["2. Gradient Calc\n(Sobel)"]
B --> C["3. Non-Max Suppression\n(Keep local maxima)"]
C --> D["4. Double Thresholding\n(Hysteresis)"]
D --> E["5. Edge Tracking\n(Connect regions)"]Why Canny?
- Used in Google Maps’ street view segmentation to detect lanes.
- Exam tip: Draw this pipeline and label each step!
C. Region-Based: Grouping Pixels
Idea: Merge adjacent pixels with similar properties (intensity, texture). Methods:
| Method | Approach | Pros | Cons |
|---|---|---|---|
| Region Growing | Start from seed, add similar pixels | Simple, fast | Sensitive to seed choice |
| Split and Merge | Recursively divide/merge regions | Handles complex shapes | Computationally heavy |
| Clustering (K-means) | Group pixels into clusters | Works on color/texture | Needs as input |
Worked Example: Region Growing
[ 50 52 60 ]
[ 55 58 70 ]
[ 60 65 80 ]
Steps:
- Pick seed at (1,1) = 58, threshold .
- Compare neighbors:
- (1,2) = 52: → reject.
- (2,1) = 55: → accept.
- Repeat until no new pixels join.
Output:
[ X X X ]
[ A A X ]
[ A A X ]
(: segmented region, : background)
Real Use: Daraz’s product images use region growing to isolate items from white backgrounds.
D. Clustering: K-Means Segmentation
Idea: Group pixels into clusters (e.g., : sky, road, car). Steps:
- Randomly pick centroids.
- Assign each pixel to the nearest centroid.
- Recompute centroids as the mean of assigned pixels.
- Repeat until convergence.
Worked Example: Segment a 2×2 image:
[ (50,100) (150,50) ]
[ (200,200) (50,150) ]
For :
- Initial centroids: , .
- Assign pixels:
- (50,100) → ,
- (150,50) → (closer to ),
- (200,200) → ,
- (50,150) → .
- Recompute centroids: , .
- Repeat until centroids stabilize.
Exam Tip: Always show initialization → assignment → update steps!
3. Morphological Operations for Cleanup
After segmentation, use erosion/dilation to remove noise or fill gaps. Definitions:
- Erosion: Shrinks objects (removes small regions). .
- Dilation: Expands objects (fills holes). .
Worked Example: Clean up a segmented license plate. Input:
[ 1 1 0 0 ]
[ 1 1 1 0 ]
[ 0 1 1 1 ]
Step 1: Erode with structuring element :
- Center pixel (2,2) = 1: fits → keep 1.
- Edge pixels (e.g., (1,1)): doesn’t fit → set to 0. Output:
[ 0 0 0 0 ]
[ 0 1 0 0 ]
[ 0 0 1 0 ]
Step 2: Dilate to recover shape:
- Use same , output becomes:
[ 0 1 0 0 ]
[ 1 1 1 0 ]
[ 0 1 1 1 ]
Real Use: NTC’s automated number plate reader uses erosion to remove dust spots before OCR.
4. Evaluation Metrics
Quantify segmentation quality with:
| Metric | Formula | Interpretation |
|---|---|---|
| Accuracy | % of correctly labeled pixels | |
| IoU (Jaccard Index) | Overlap between predicted and ground truth | |
| Dice Coefficient | Similar to IoU, penalizes FP/FN equally |
Worked Example: Compare two segmentations of a 2×2 grid. Ground Truth:
[ 1 0 ]
[ 0 1 ]
Prediction A:
[ 1 1 ]
[ 0 1 ]
Prediction B:
[ 1 0 ]
[ 1 0 ]
Calculations:
| Metric | Prediction A | Prediction B |
|---|---|---|
| TP | 2 | 1 |
| FP | 1 | 0 |
| FN | 0 | 1 |
| IoU | ||
| Conclusion: Prediction A is better. |
## In the Real World
Pathao’s Ride-Hailing App
- Idea Used: Edge detection (Canny) + region growing
- How: Segments riders from traffic in real-time camera feeds to avoid collisions. Uses Gaussian blur → Canny edges → region merging to track moving objects.
Nepal Police’s Drone Surveillance
- Idea Used: K-means clustering for color segmentation
- How: Drones capture crowd images, then cluster pixels into groups (people, vehicles, background) to detect illegal gatherings. Exam tie-in: If asked about clustering, mention this!
WhatsApp’s Photo Filters
- Idea Used: Skin tone segmentation (thresholding + morphological ops)
- How: Filters like "Dog Face" use Otsu’s thresholding to isolate faces, then apply effects only to segmented regions. Real numbers: For a 100×100 face image, threshold (skin tone ~100–150 gray).
NTC’s License Plate Reader
- Idea Used: Adaptive thresholding + erosion
- How: Scans plates under varying lighting. Step 1: Bernsen’s thresholding handles shadows. Step 2: Erosion removes dust spots. Exam tip: Draw a block diagram of this pipeline!
## Exam Tip
- Definitions: Always define methods with maths (e.g., for erosion).
- Diagrams: Draw block diagrams for:
- Canny edge detector (5 steps).
- Region-growing pipeline.
- Calculations: Practice:
- Otsu’s thresholding on histograms.
- Sobel/Canny edge detection on 3×3 patches.
- IoU/Dice scores for 2×2 grids.
- Comparisons: Memorize this table for short-answer questions:
Method Speed Accuracy Best For Thresholding Fast Low Uniform lighting Canny Medium High Medical/automotive K-means Slow Medium Color/texture scenes - Real-world links: If asked about applications, name Pathao, NTC, or WhatsApp and tie to the method (e.g., "Pathao uses Canny edges for collision avoidance").
## Past Exam Questions Solved
Q: "From the given gray-level histogram, enhance the image using histogram equalization." Solution:
- Compute CDF (cumulative distribution function): . For : .
- Scale to 0–255: . For : .
- Map original gray levels to new values:
- Original 0 → 0,
- Original 1 → 64 (since ),
- Original 2 → 115, etc.
Q: "Explain region-based segmentation with an example." Answer:
Region-based segmentation groups pixels into homogeneous regions using criteria like intensity or texture. For example, in a 3×3 image with center pixel 50 and neighbors [48, 52, 55], starting from the center and using a threshold of 5, we merge all pixels within of 50. This is used in Daraz’s product segmentation to isolate items from backgrounds. Visual: Draw a 3×3 grid with merged regions shaded.
## Summary Checklist
Before the exam, ensure you can:
- Draw the Canny edge detector pipeline.
- Calculate Otsu’s threshold for a given histogram.
- Compare thresholding vs. clustering methods in a table.
- Apply erosion/dilation to a 3×3 binary image.
- Name 2 real-world apps using segmentation (e.g., Pathao, NTC).
- Compute IoU for a 2×2 segmentation example.
Based on the TU BCA syllabus for Image Processing, unit 5.
Discussion
Loading…