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

MVS_Expt3 Color Based Segmentation Using K-Means Clustering

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

MVS_Expt3 Color Based Segmentation Using K-Means Clustering

Same
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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

MVS Experiment No. 3 Color Based Segmentation Using K-Means Clustering

Aim: 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.

Color image segmentation

 Color image segmentation is a technique in image processing that involves dividing an


image into meaningful, distinct regions based on color. The goal is to simplify or
change the representation of an image to make it easier to analyze.
 Color segmentation is commonly used in applications like object recognition, scene
understanding, medical imaging, and industrial automation.
 In color image segmentation, the segmentation process leverages color information to
group pixels with similar characteristics together. Here's how it generally works:
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

1. Color Space Selection:


 First, the image is converted into a color space that makes segmentation easier.
 Common color spaces include RGB (Red, Green, Blue), HSV (Hue, Saturation,
Value), and LAB (Lightness, A and B channels).
 Each color space has its own advantages; for instance, HSV and LAB are often
better for segmentation because they separate color information from intensity,
making it easier to identify and isolate regions based on hue or color.
2. Color-Based Region Identification:
 Color features are extracted from the chosen color space, and pixels are grouped
based on similarity in color values. Techniques like clustering (e.g., K-means),
thresholding, or region-growing algorithms can be used to achieve this. For
instance, K-means clustering can partition pixels into clusters based on the
similarity of their colors, separating different regions or objects.
3. Segmentation Techniques:
 Different algorithms are applied depending on the requirements. Some common
techniques include:
 Thresholding: Selecting color ranges to isolate objects of interest.
 Clustering: Using algorithms like K-means or Mean Shift togroup pixels.
 Edge Detection: Identifying boundaries based on color contrast.
 Region Growing: Expanding regions from seed points based oncolor
similarity.

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 Algorithm:


K Means is a clustering algorithm. Clustering algorithms are unsupervised algorithms which
means that there is no labelled data available. It is used to identify different classes or clusters
in the given data based on how similar the data is. Data points in the same group are more
similar to other data points in that same group than those in other groups.

K-means clustering is one of the most commonly used clustering algorithms. Here, k represents
the number of clusters.

Let’s see how does K-means clustering work –

1. Choose the number of clusters you want to find which is k.

2. Randomly assign the data points to any of the k clusters.

3. Then calculate the center of the 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.

6. Again calculate the new cluster center.

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

Detailed Steps Involved:


1. Image Upload:
Upload the image using a file upload option, allowing selection directly from the local
computer.

2. Image Loading and Preprocessing:


Load the uploaded image and convert it to an RGB format if it has an alpha channel
(RGBA) or BGR format.

Display the original image to verify correct loading.

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.

4. Define Number of Clusters (k):


Define k, the number of clusters, based on the desired segmentation granularity (e.g.,
k=5 for 5 color segments).
Initialize the K-Means clustering model with the chosen number of clusters.

5. Define Number of Clusters (k):


Define k, the number of clusters, based on the desired segmentation granularity (e.g.,
k5 for 5 color segments). IInitialize the K-Means clustering model with the chosen
number of clusters.

6. Cluster Assignment Using K-Means:


a. Apply the K-Means clustering algorithm to group pixels based on color similarity.
b. Obtain the cluster centers (representing the dominant colors) and assign each pixel
to the nearest cluster.

7. Recolor Segmented Image:


a. Replace each pixel’s RGB value with its corresponding cluster center’s RGB value.
b. Reshape the recolored image back to its original dimensions for display as a
segmented image.
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

8. Display Segmented Image:


a. Display the segmented image with k unique colors representing each cluster.

9. Cluster Mask Creation (Optional):


a. Create a binary mask for one specific cluster (e.g., Cluster 0) to isolate a segment in
the image.
b. Set non-cluster pixels to black, leaving only the selected cluster visible.

10. Choose the Cluster:


Specify which cluster to isolate by setting selected_cluster.

For example, selected_cluster 8 selects the first cluster.

11. Create the Binary Mask:


Generate a mask where all pixels belonging to the selected cluster are set to 1 (or 255
for visibility in an 8-bit image).

Reshape the mask to the original image dimensions, setting other cluster pixels to 0.
Display the

12. Binary Mask:


Use plt.imshow(mask, cmap=gray') to visualize the binary mask.
13. Apply the Mask to the Image:
Use the mask to retain only the pixels of the selected cluster and set all other pixels to
black.

14. Display the Masked Image:


Visualize the result, showing only the segmented area corresponding to the selected
cluster.

15. Display Masked Cluster:


a. Display the image with the isolated cluster, showing only the pixels belonging to
the chosen cluster.

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

# Load the image


image_path = r"D:\forza pics\11831209211889451008_10.jpg" # Replace with your
image path
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Resize the image for faster processing (optional)


image = cv2.resize(image, (300, 300))

# Reshape the image to a 2D array of pixels


pixels = image.reshape(-1, 3)
pixels = np.float32(pixels)

# Apply K-Means Clustering with more clusters for better accuracy


num_clusters = 10 # You can adjust this for more granularity
kmeans = KMeans(n_clusters=num_clusters, random_state=42)
kmeans.fit(pixels)

# Get dominant colors and labels


dominant_colors = np.uint8(kmeans.cluster_centers_)
labels = kmeans.labels_

# Create segmented image


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

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

"Mint": np.array([189, 252, 201]),


"Coral": np.array([255, 127, 80]),
"Indigo": np.array([75, 0, 130]),
"Crimson": np.array([220, 20, 60]),
"Chartreuse": np.array([127, 255, 0]),
"Khaki": np.array([240, 230, 140]),
"Amber": np.array([255, 191, 0]),
"Dark Green": np.array([0, 100, 0]),
"Sky Blue": np.array([135, 206, 235]),
"Lime": np.array([0, 255, 0]),
"Violet": np.array([238, 130, 238]),
"Dark Red": np.array([139, 0, 0]),
"Dark Blue": np.array([0, 0, 139]),
"Light Gray": np.array([211, 211, 211]),
}
rgb = np.array(rgb)
distances = {name: np.linalg.norm(rgb - color_rgb) for name, color_rgb in
basic_colors.items()}
closest_color = min(distances, key=distances.get)
return closest_color

# Function to display color and coordinates on click


def on_click(event):
if event.inaxes is not None:
x, y = int(event.xdata), int(event.ydata)
if event.inaxes.title.get_text() == "Original Image":
color = image[y, x]
elif event.inaxes.title.get_text() == "Segmented Image":
color = segmented_image[y, x]
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})")

# Create a side-by-side plot


fig, axes = plt.subplots(1, 2, figsize=(12, 6))

# 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")

# Connect the click event


fig.canvas.mpl_connect('button_press_event', on_click)
plt.show()

Output:

You might also like