MVS_Expt3 Color Based Segmentation Using K-Means Clustering
MVS_Expt3 Color Based Segmentation Using K-Means Clustering
Theory:
Image Segmentation
In computer vision, image segmentation is the process of partitioning an image into
multiple segments. The goal of segmenting an image is to change the representation of
an image into something that is more meaningful and easier to analyze. It is usually
used for locating objects and creating boundaries.
It is not a great idea to process an entire image because many parts in an image may not
contain any useful information. Therefore, by segmenting the image, we can make use
of only the important segments for processing.
An image is basically a set of given pixels. In image segmentation, pixels which have
similar attributes are grouped together. Image segmentation creates a pixel-wise mask
for objects in an image which gives us a more comprehensive and granular
understanding of the object.
Uses:
1. Used in self-driving cars. Autonomous driving is not possible without object detection
which involves segmentation.
2. Used in the healthcare industry. Helpful in segmenting cancer cells and tumours using
which their severity can be gauged.
4. Application of Post-Processing:
Techniques such as morphological operations (dilation, erosion) can refine the
segmented regions, removing noise or filling in small gaps for smoother results.
Examples of Use Cases:
1. Medical Imaging: Detecting specific tissues or structures in color-coded
images (e.g., histological images).
2. Object Detection: Isolating objects like fruits in agricultural images by
their color.
3. Scene Segmentation: Differentiating between sky, trees, and water in
landscape photography
JSPM’s
RAJARSHI SHAHU COLLEGE OF ENGINEERING
TATHAWADE, PUNE-33
(An Autonomous Institute Affiliated to Savitribai Phule Pune
University, Pune)
DEPARTMENT OF AUTOMATION AND ROBOTICS
K-means clustering is one of the most commonly used clustering algorithms. Here, k represents
the number of clusters.
4. Calculate the distance of the data points from the centers of each of the clusters.
5. Depending on the distance of each data point from the cluster, reassign the data points
to the nearest clusters.
7. Repeat steps 4,5 and 6 till data points don’t change the clusters, or till we reach the
assigned number of iterations.
JSPM’s
RAJARSHI SHAHU COLLEGE OF ENGINEERING
TATHAWADE, PUNE-33
(An Autonomous Institute Affiliated to Savitribai Phule Pune
University, Pune)
DEPARTMENT OF AUTOMATION AND ROBOTICS
3. Image Reshaping:
Reshape the image data from a 3D array (height width color channels) into a 2D array
where each row represents the RGB values of a single pixel.
Convert the data type of pixel values to float32 for compatibility with clustering.
Reshape the mask to the original image dimensions, setting other cluster pixels to 0.
Display the
Conclusion:
JSPM’s
RAJARSHI SHAHU COLLEGE OF ENGINEERING
TATHAWADE, PUNE-33
(An Autonomous Institute Affiliated to Savitribai Phule Pune
University, Pune)
DEPARTMENT OF AUTOMATION AND ROBOTICS
Code:
import cv2
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
segmented_image = dominant_colors[labels]
segmented_image = segmented_image.reshape(image.shape)
def get_basic_color_name(rgb):
basic_colors = {
"Red": np.array([255, 0, 0]),
"Green": np.array([0, 255, 0]),
"Blue": np.array([0, 0, 255]),
"Yellow": np.array([255, 255, 0]),
"Cyan": np.array([0, 255, 255]),
"Magenta": np.array([255, 0, 255]),
"Black": np.array([0, 0, 0]),
"White": np.array([255, 255, 255]),
"Gray": np.array([128, 128, 128]),
"Orange": np.array([255, 165, 0]),
"Pink": np.array([255, 192, 203]),
"Purple": np.array([128, 0, 128]),
"Brown": np.array([165, 42, 42]),
"Beige": np.array([245, 245, 220]),
"Olive": np.array([128, 128, 0]),
"Maroon": np.array([128, 0, 0]),
"Navy": np.array([0, 0, 128]),
"Teal": np.array([0, 128, 128]),
"Turquoise": np.array([64, 224, 208]),
"Gold": np.array([255, 215, 0]),
"Silver": np.array([192, 192, 192]),
"Lavender": np.array([230, 230, 250]),
"Peach": np.array([255, 218, 185]),
JSPM’s
RAJARSHI SHAHU COLLEGE OF ENGINEERING
TATHAWADE, PUNE-33
(An Autonomous Institute Affiliated to Savitribai Phule Pune
University, Pune)
DEPARTMENT OF AUTOMATION AND ROBOTICS
color_name = get_basic_color_name(color)
print(f"Coordinates: ({x}, {y}), Color: {color_name} (RGB: {color})")
# Original Image
axes[0].imshow(image)
axes[0].set_title("Original Image")
axes[0].axis("off")
# Segmented Image
axes[1].imshow(segmented_image)
axes[1].set_title("Segmented Image")
axes[1].axis("off")
Output: