0% found this document useful (0 votes)
24 views

Image Segmentation in Python- Practical Hands-On (3)

Uploaded by

Chloe Tee
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Image Segmentation in Python- Practical Hands-On (3)

Uploaded by

Chloe Tee
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Image Segmentation in

Python- Practical Hands-On


Prepared by: Dr. Toh Leow Bin
Lesson Objectives
 By the end of this lesson, students will be able to:
 Understand the Methods:
 Explain the concepts of edge-based and clustering-based segmentation.
 Apply Techniques:
 Implement edge detection algorithms (e.g., Sobel, Prewitt, Canny) to identify
object boundaries in images.
 Perform clustering-based segmentation (e.g., K-Means).
 Analyze Results:
 Compare the effectiveness of both methods on different images.
 Apply to Real World Scenarios:
 Use these techniques for real-world image segmentation tasks.
What is Image Segmentation?

 Image segmentation is the process of partitioning an image into multiple


meaningful regions or objects to simplify its analysis.
 It is widely used in applications like medical imaging, object detection,
and satellite imagery.
 Type of Image Segmentation:
 Thresholding: Separates pixels based on intensity values.
 Edge-Based: Detects object boundaries using gradients.
 Region-Based: Groups connected pixels based on similarity.
 Clustering-Based: Groups similar pixels into clusters using algorithms like K-
means.
Edge-Based Method

 Principle:
 This method detects object boundaries by identifying regions with sudden
changes in pixel intensity (gradients).
 It relies on the assumption that object edges are characterized by high
contrast compared to the background.
 Steps:
 Compute the gradient of the image intensity using edge detectors
 (e.g., Sobel, Prewitt, or Laplacian operators).
 Threshold the gradient values to retain the strongest edges.
 Use edge linking to form continuous boundaries.
Edge-based Method

 Common Techniques:
 Sobel Operator: Calculates the gradient in the horizontal and vertical directions.
 Canny Edge Detector: Combines Gaussian smoothing, gradient calculation, non-
maximum suppression, and double-thresholding for robust edge detection.
 Applications:
 Object boundary detection.
 Feature extraction for object recognition.
 Road and building detection in satellite imagery.
 Challenges:
 Sensitive to noise (may require preprocessing like Gaussian smoothing).
 May fail to close gaps in edges, leading to incomplete segmentation.
 Does not provide region information, only boundaries.
Edge-Based Method- Code Example
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load grayscale image


image = cv2.imread('rose.jpg', cv2.IMREAD_GRAYSCALE)

# Apply edge detection


edges_sobel = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
edges_canny = cv2.Canny(image, 100, 200)

# Plot results
plt.figure(figsize=(10, 5))
plt.subplot(1, 3, 1), plt.title("Original Image"), plt.imshow(image, cmap='gray')
plt.subplot(1, 3, 2), plt.title("Sobel Edges"), plt.imshow(np.abs(edges_sobel),
cmap='gray')
plt.subplot(1, 3, 3), plt.title("Canny Edges"), plt.imshow(edges_canny, cmap='gray')
plt.show()
Clustering-Based Method

 Principle:
 Groups pixels with similar features (e.g., intensity, color, or texture)
into clusters.
 It uses statistical or machine learning algorithms to partition the
image into distinct segments.
 Steps:
 Extract features from each pixel (e.g., RGB values, spatial location).
 Apply clustering algorithms (e.g., K-means, Mean-shift, Gaussian
Mixture Models).
 Assign each pixel to a cluster, effectively segmenting the image.
Clustering-Based Method

 Common Techniques:
 K-means Clustering:
 Partitions pixels into k clusters by minimizing the variance within
each cluster.
 Mean-Shift Clustering:
 Identifies high-density regions in feature space to form clusters.
 Gaussian Mixture Model (GMM):
 Uses probabilistic modeling for clustering.
Clustering-Based Method

 Applications:
 Color segmentation (e.g., separating different parts of an image based on
color).
 Texture segmentation in remote sensing or biological imaging.
 Object segmentation in computer vision tasks.
 Challenges:
 Requires prior knowledge of the number of clusters (k) in methods like K-
means.
 Sensitive to initialization (e.g., cluster centroids in K-means).
 May struggle with overlapping regions or noise.
K-means clustering

 Goal: To partition the image into k clusters based on pixel similarity.


 Algorithm:
 Randomly initialize k cluster centroids.
 Assign each pixel to the nearest centroid based on similarity (e.g., color
distance in RGB space).
 Recalculate centroids as the mean of the assigned pixels.
 Repeat until convergence (minimal changes in centroids).
 Output: Segmented image with each pixel labeled according to its
cluster.
How K-means clustering works
K-means Clustering- Detailed
Walkthrough
 Step 1: Initialization
 Choose k=2 (we want two clusters).
Randomly initialize centroids:
 Centroid 1: (2, 3)
 Centroid 2: (6, 8)
 Step 2: Assignment
 Assign each point to the nearest centroid.
 Use the Euclidean distance formula:
K-means Clustering- Detailed
Walkthrough

Data Point Distance to (2, 3) Distance to (6, 8) Assigned Cluster


(2, 3) 000 Cluster
(3, 3) Cluster
(6, 8) 000 Cluster
(7, 9) Cluster
(1, 0) Cluster
(2, 1) Cluster
K-means Clustering- Detailed
Walkthrough
 Step 3: Update Centroids
 Recalculate the centroids by taking the mean of all points in each
cluster:
 Cluster 1 points: (2, 3), (3, 3), (1, 0), (2, 1)
 New Centroid 1= ?
 Cluster 2 points: (6, 8), (7, 9)
 New Centroid 2= ?
K-means Clustering- Detailed
Walkthrough

Step 4: Re-assignment
Using the new centroids, assign points again:
New Calculations:

Distance to (2.0, Distance to (6.5,


Data Point Assigned Cluster
1.75) 8.5)
(2, 3) 1.25 7.78 Cluster 1
(3, 3) 1.80 6.80 Cluster 1
(6, 8) 7.34 0.71 Cluster 2
(7, 9) 8.74 0.71 Cluster 2
(1, 0) 2.15 9.15 Cluster 1
(2, 1) 0.75 8.71 Cluster 1
K-means Clustering- Detailed
Walkthrough
 Step 5: Convergence
 Repeat the update and assignment steps until centroids no longer
change significantly. After 2-3 iterations, the centroids stabilize:
 Final Centroid 1: (2.0, 1.75)
 Final Centroid 2: (6.5, 8.5)
K-means clustering – Simple Example

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# Dataset
X = np.array([[2, 3], [3, 3], [6, 8], [7, 9], [1, 0], [2, 1]])

# KMeans Clustering
kmeans = KMeans(n_clusters=2, random_state=42)
kmeans.fit(X)

# Results
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
K-means clustering – Simple Example

# Visualization
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', marker='o',
label='Data Points')
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', marker='x',
s=200, label='Centroids')
plt.title('K-Means Clustering')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.grid()
plt.show()
K-means Clustering for Image
Segmentation – Code Example
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# Step 1: Load the image


image = cv2.imread('Acute_leukemia-ALL.jpg') # Replace with your image
path
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert from BGR to RGB

# Step 2: Reshape the image into a 2D array of pixels and 3 color values
(R, G, B)
pixel_values = image.reshape((-1, 3))
pixel_values = np.float32(pixel_values)
K-means Clustering for Image
Segmentation – Code Example
# Step 3: Define the number of clusters (k)
k = 3 # You can experiment with different values of k

# Step 4: Apply K-Means clustering


kmeans = KMeans(n_clusters=k, random_state=42)
kmeans.fit(pixel_values)

# Step 5: Replace pixel values with their cluster centers


centers = np.uint8(kmeans.cluster_centers_) # Convert centers to integers
labels = kmeans.labels_ # Get the labels for each pixel

segmented_image = centers[labels.flatten()]
segmented_image = segmented_image.reshape(image.shape)
K-means Clustering for Image
Segmentation – Code Example
# Step 6: Visualize the original and segmented image
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image)
plt.axis("off")

plt.subplot(1, 2, 2)
plt.title(f"Segmented Image (k={k})")
plt.imshow(segmented_image)
plt.axis("off")

plt.tight_layout()
plt.show()
K-means Clustering for Image Segmentation– Code Example

# Step 7: Create and display separate images for each cluster


fig, axes = plt.subplots(1, k, figsize=(15, 5)) # Creating subplots here

for i in range(k):
# Create a mask for the current cluster
cluster_mask = labels.reshape(image.shape[:2]) == i

# Apply the mask to the original image to extract the cluster


cluster_image = np.copy(image)
cluster_image[~cluster_mask] = [0, 0, 0] # Set pixels outside the cluster to black

# Display the cluster image in the subplot


axes[i].imshow(cluster_image)
axes[i].set_title(f"Cluster {i + 1}")
axes[i].axis("off")

plt.tight_layout()
plt.show()
Summary
Comparison Between Edge-Based and Clustering-Based Methods

Aspect Edge-Based Clustering-Based


Detects edges by
Groups similar pixels
Principle identifying intensity
into clusters.
changes.
Computationally fast; Handles multi-
Strengths good for high-contrast dimensional data;
images. flexible.
Needs predefined k;
Sensitive to noise and
Weaknesses struggles with irregular
poorly defined edges.
clusters.
Medical imaging,
Object detection,
Applications background
contour-based tasks.
subtraction.
Thank You

You might also like