What is Image Segmentation?
Image segmentation is the process of dividing an image into multiple regions or segments to
simplify its analysis. Each segment represents a group of pixels that share similar characteristics, such
as color, texture, or intensity.
It is used in computer vision and image processing to locate objects, detect boundaries, and analyze
scenes.
Import Required Libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
cv2 (OpenCV): Used for reading and processing images.
numpy (NumPy): Helps with numerical operations and array manipulations.
matplotlib.pyplot (Matplotlib): Used to display images.
KMeans from sklearn.cluster: Implements K-Means clustering for segmentation.
Define the Function
def segment_image_kmeans(image_path, k=3):
This function takes two inputs:
o image_path: The path of the image file.
o k: The number of clusters (segments) for K-Means (default is 3).
Load and Preprocess the Image
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert to RGB format
cv2.imread(image_path): Reads the image from the specified file path.
OpenCV loads images in BGR format by default, but matplotlib expects RGB, so we convert
it:
o cv2.cvtColor(image, cv2.COLOR_BGR2RGB): Converts from BGR to RGB.
Reshape Image for Clustering
pixels = image.reshape((-1, 3))
The image is a 3D NumPy array of shape (height, width, 3).
We reshape it into a 2D array, where each row represents a pixel's RGB values:
o (-1, 3): The -1 automatically infers the number of rows (total pixels), and 3 represents
the RGB channels.
Apply K-Means Clustering
kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)
labels = kmeans.fit_predict(pixels)
KMeans(n_clusters=k, random_state=42, n_init=10):
o n_clusters=k: Defines the number of clusters (segments).
o random_state=42: Ensures reproducibility of results.
o n_init=10: Runs the algorithm 10 times with different initializations to choose the
best.
kmeans.fit_predict(pixels):
o Performs K-Means clustering on the pixel data.
o Assigns each pixel a cluster label (which segment it belongs to).
Map Pixels to Cluster Centers
segmented_image = kmeans.cluster_centers_.astype('uint8')[labels]
segmented_image = segmented_image.reshape(image.shape)
kmeans.cluster_centers_: Stores the RGB values of the K cluster centers.
astype('uint8'): Converts the floating-point values to integers (0-255).
[labels]: Replaces each pixel with its corresponding cluster center.
reshape(image.shape): Reshapes the flattened 2D array back into the original image shape.
Display the Original and Segmented Images
fig, ax = plt.subplots(1, 2, figsize=(12, 6))
Creates a figure with two subplots (1 row, 2 columns).
figsize=(12, 6): Sets the figure size.
ax[0].imshow(image)
ax[0].set_title('Original Image')
ax[0].axis('off')
Displays the original image.
set_title('Original Image'): Sets the title.
axis('off'): Hides axis labels.
ax[1].imshow(segmented_image)
ax[1].set_title(f'Segmented Image (K={k})')
ax[1].axis('off')
Displays the segmented image.
Title shows the selected K value.
plt.show()
Displays the images.