Back to Course
CMSC 178IP

Module 08: Segmentation and Morphology

1 / --

Segmentation and Morphology

CMSC 178IP - Module 08

Noel Jeffrey Pinton
Department of Computer Science
University of the Philippines Cebu

Segmentation and Morphology

CMSC 178IP - Module 08

Noel Jeffrey Pinton
Department of Computer Science
University of the Philippines Cebu

Learning Objectives

By the end of this module, you will be able to:

  1. Apply thresholding techniques (global, Otsu, adaptive)
  2. Implement region-based segmentation methods
  3. Use the watershed algorithm for complex segmentation
  4. Apply morphological operations (erosion, dilation, opening, closing)
  5. Use morphology for noise removal and object extraction

Thresholding

Converting grayscale to binary

Global Thresholding

Global Thresholding

Binary Thresholding:

$$g(x,y) = \begin{cases} 1 & \text{if } f(x,y) > T \\ 0 & \text{otherwise} \end{cases}$$

Simple but requires manual threshold selection.

Otsu's Thresholding

Otsu Thresholding

Otsu's Method: Automatically find optimal threshold by maximizing between-class variance.

$$\sigma^2_B = \omega_0 \omega_1 (\mu_0 - \mu_1)^2$$

Works well for bimodal histograms.

Knowledge Check

Think About It

When does Otsu's method work well, and when might it fail?

Click the blurred area to reveal the answer

Adaptive Thresholding

Adaptive Thresholding
Adaptive (Local) Thresholding:
Compute threshold for each pixel based on local neighborhood. Handles uneven illumination.

Region-Based Segmentation

Grouping similar pixels

Region Growing

Region Growing

Region Growing Algorithm:

  1. Start from seed point(s)
  2. Examine neighboring pixels
  3. Add if similar (within threshold)
  4. Repeat until no more pixels can be added

Watershed Segmentation

Watershed
Watershed: Treat gradient image as topography. "Flood" from markers - watershed lines form where waters meet. Excellent for separating touching objects.

Knowledge Check

Think About It

What is over-segmentation in watershed, and how can it be addressed?

Click the blurred area to reveal the answer

Morphological Operations

Shape-based image processing

Structuring Elements

Structuring Elements

Structuring Element: Small shape used as a probe. Common shapes: square, disk, cross, diamond.

Erosion and Dilation

Erosion Dilation
Erosion

$$A \ominus B$$

Shrinks objects, removes small protrusions

Dilation

$$A \oplus B$$

Expands objects, fills small holes

Opening and Closing

Opening Closing
Opening

$$A \circ B = (A \ominus B) \oplus B$$

Removes small objects, smooths contours

Closing

$$A \bullet B = (A \oplus B) \ominus B$$

Fills small holes, connects nearby objects

Knowledge Check

Think About It

What is the difference between erosion and opening?

Click the blurred area to reveal the answer

Morphological Gradient

Morphological Gradient

Morphological Gradient:

$$G = (A \oplus B) - (A \ominus B)$$

Dilation minus erosion = edge detection using morphology

Grayscale Morphology

Grayscale Morphology
Grayscale Morphology: Same operations applied to grayscale images. Useful for top-hat transforms and contrast enhancement.

Applications

Practical uses of segmentation and morphology

Noise Removal Pipeline

Noise Removal
Morphological Filtering: Opening removes salt noise (white specks), closing removes pepper noise (black specks).

Edge Detection with Morphology

Morphological Edges

Comparing morphological gradient with other edge detection methods

Object Extraction

Object Extraction
Connected Component Analysis: Label distinct objects after segmentation. Extract properties: area, centroid, bounding box.

Texture Enhancement

Texture Enhancement

Top-hat and bottom-hat transforms for texture analysis

Multi-Scale Segmentation

Multi-Scale

Different structuring element sizes capture features at different scales

Implementation

import cv2
import numpy as np

img = cv2.imread('image.jpg', 0)

# Otsu's thresholding
_, binary = cv2.threshold(img, 0, 255, 
    cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Adaptive thresholding
adaptive = cv2.adaptiveThreshold(img, 255,
    cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

# Morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
opened = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
closed = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)

Summary

Key Takeaways

Key Takeaways

  1. Thresholding: Global, Otsu (automatic), adaptive (local)
  2. Region growing: Expand from seeds based on similarity
  3. Watershed: Separate touching objects using markers
  4. Erosion/Dilation: Basic morphological operations
  5. Opening/Closing: Remove noise, fill holes
  6. Applications: Noise removal, edge detection, object extraction

Questions?

Thank you for your attention!


Next: Module 09 - Computer Vision & Deep Learning I

End of Module 08

Segmentation and Morphology

Questions?