Image segmentation is a critical step in computer vision, enabling the division of an image into meaningful regions. K-means clustering, a simple and effective unsupervised machine learning algorithm, is widely used for this task. By determining an optimal number of clusters (K
) using the Elbow Method, we can achieve better segmentation results.
To ensure accurate segmentation, the image is denoised using Bilateral Filtering, which preserves edges while smoothing noise.
import cv2 as cv import numpy as np import matplotlib.pyplot as plt # Load the image img = cv.imread('road.jpg') img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB) # Convert for plotting # Denoising using Bilateral Filter denoised_img = cv.bilateralFilter(img, d=9, sigmaColor=75, sigmaSpace=75)
The image is reshaped into a 2D array of pixels to prepare for clustering. Each pixel is treated as a data point in a 3-dimensional color space (RGB).
Z = denoised_img.reshape((-1, 3)) # Reshape into 2D Z = np.float32(Z) # Convert to float32 for k-means
The Elbow Method is used to determine the optimal number of clusters (K) by plotting the Within-Cluster Sum of Squares (WCSS) against various values of K. The "elbow point" indicates the best K.
from sklearn.cluster import KMeans wcss = [] # Store WCSS for each K K_range = range(1, 11) # Testing K values from 1 to 10 for k in K_range: kmeans = KMeans(n_clusters=k, random_state=42) kmeans.fit(Z) wcss.append(kmeans.inertia_) # Plot the Elbow Curve plt.figure(figsize=(8, 6)) plt.plot(K_range, wcss, marker='o') plt.title('Elbow Method for Optimal K') plt.xlabel('Number of Clusters (K)') plt.ylabel('WCSS') plt.show()
Using the optimal K (identified from the Elbow Curve), K-means clustering is applied to segment the image.
optimal_k = 4 # Replace this with the chosen K ret, label, center = cv.kmeans( Z, optimal_k, None, (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0), 10, cv.KMEANS_RANDOM_CENTERS ) # Map pixels to their nearest cluster centers center = np.uint8(center) clustered_img = center[label.flatten()].reshape(denoised_img.shape)
The original, denoised, and segmented images are displayed for comparison.
Copy code plt.figure(figsize=(12, 8)) plt.subplot(1, 3, 1) plt.imshow(img_rgb) plt.title('Original Image') plt.subplot(1, 3, 2) plt.imshow(cv.cvtColor(denoised_img, cv.COLOR_BGR2RGB)) plt.title('Denoised Image') plt.subplot(1, 3, 3) plt.imshow(clustered_img) plt.title(f'Clustered Image (K={optimal_k})') plt.tight_layout() plt.show()
Autonomous Vehicles 🚗: Lane detection and road segmentation.
Medical Imaging 🩺: Segmenting tissues or identifying tumors.
Satellite Imaging 🛰️: Land cover classification.
E-commerce 🛒: Isolating product images for better display.
Real-Time Segmentation ⚡: Optimizing for faster processing.
Integration with Neural Networks 🤖: Hybrid approaches for enhanced accuracy.
3D Image Segmentation 📸: Applying clustering to 3D volumetric data.
K-means clustering is a robust method for image segmentation, and determining the optimal number of clusters (K) using the Elbow Method ensures effective results. Its simplicity and versatility make it invaluable in diverse applications ranging from healthcare to automotive systems.
There are no models linked
There are no models linked