Image Segmentation By Clustering
Last Updated :
18 Jul, 2021
Segmentation By clustering
It is a method to perform Image Segmentation of pixel-wise segmentation. In this type of segmentation, we try to cluster the pixels that are together. There are two approaches for performing the Segmentation by clustering.
- Clustering by Merging
- Clustering by Divisive
Clustering by merging or Agglomerative Clustering:
In this approach, we follow the bottom-up approach, which means we assign the pixel closest to the cluster. The algorithm for performing the agglomerative clustering as follows:
- Take each point as a separate cluster.
- For a given number of epochs or until clustering is satisfactory.
- Merge two clusters with the smallest inter-cluster distance (WCSS).
- Repeat the above step
The agglomerative clustering is represented by Dendrogram. It can be performed in 3 methods: by selecting the closest pair for merging, by selecting the farthest pair for merging, or by selecting the pair which is at an average distance (neither closest nor farthest). The dendrogram representing these types of clustering is below:
Nearest clustering
Average Clustering
Farthest Clustering
Clustering by division or Divisive splitting
In this approach, we follow the top-down approach, which means we assign the pixel closest to the cluster. The algorithm for performing the agglomerative clustering as follows:
- Construct a single cluster containing all points.
- For a given number of epochs or until clustering is satisfactory.
- Split the cluster into two clusters with the largest inter-cluster distance.
- Repeat the above steps.
In this article, we will be discussing how to perform the K-Means Clustering.
K-Means Clustering
K-means clustering is a very popular clustering algorithm which applied when we have a dataset with labels unknown. The goal is to find certain groups based on some kind of similarity in the data with the number of groups represented by K. This algorithm is generally used in areas like market segmentation, customer segmentation, etc. But, it can also be used to segment different objects in the images on the basis of the pixel values.
The algorithm for image segmentation works as follows:
- First, we need to select the value of K in K-means clustering.
- Select a feature vector for every pixel (color values such as RGB value, texture etc.).
- Define a similarity measure b/w feature vectors such as Euclidean distance to measure the similarity b/w any two points/pixel.
- Apply K-means algorithm to the cluster centers
- Apply connected component's algorithm.
- Combine any component of size less than the threshold to an adjacent component that is similar to it until you can't combine more.
Following are the steps for applying the K-means clustering algorithm:
- Select K points and assign them one cluster center each.
- Until the cluster center won't change, perform the following steps:
- Allocate each point to the nearest cluster center and ensure that each cluster center has one point.
- Replace the cluster center with the mean of the points assigned to it.
- End
The optimal value of K?
For a certain class of clustering algorithms, there is a parameter commonly referred to as K that specifies the number of clusters to detect. We may have the predefined value of K, if we have domain knowledge about data that how many categories it contains. But, before calculating the optimal value of K, we first need to define the objective function for the above algorithm. The objective function can be given by:
J = \sum_{j=1}^{K}\sum_{i=1}^{N} |x_i^{j}-c_j |^{2}
Where j is the number of clusters, and i will be the points belong to the jth cluster. The above objective function is called within-cluster sum of square (WCSS) distance.
A good way to find the optimal value of K is to brute force a smaller range of values (1-10) and plot the graph of WCSS distance vs K. The point where the graph is sharply bent downward can be considered the optimal value of K. This method is called Elbow method.
For image segmentation, we plot the histogram of the image and try to find peaks, valleys in it. Then, we will perform the peakiness test on that histogram.
Implementation
- In this implementation, we will be performing Image Segmentation using K-Means clustering. We will be using OpenCV k-Means API to perform this clustering.
Python3
# imports
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (12,50)
# load image
img = cv.imread('road.jpg')
Z = img.reshape((-1,3))
# convert to np.float32
Z = np.float32(Z)
# define stopping criteria, number of clusters(K) and apply kmeans()
# TERM_CRITERIA_EPS : stop when the epsilon value is reached
# TERM_CRITERIA_MAX_ITER: stop when Max iteration is reached
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0)
fig, ax = plt.subplots(10,2, sharey=True)
for i in range(10):
K = i+3
# apply K-means algorithm
ret,label,center=cv.kmeans(Z,K,None,criteria,attempts = 10,
cv.KMEANS_RANDOM_CENTERS)
# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
# plot the original image and K-means image
ax[i, 1].imshow(res2)
ax[i,1].set_title('K = %s Image'%K)
ax[i, 0].imshow(img)
ax[i,0].set_title('Original Image')
Output:
Image Segmentation for K=3,4,5
Image Segmentation for K=6,7,8
References:
Similar Reads
Image Segmentation using K Means Clustering Image segmentation is a technique in computer vision that divides an image into different segments. This can help identify specific objects, boundaries or patterns in the image. Image is basically a set of given pixels and in image segmentation pixels with similar intensity are grouped together. Im
2 min read
Image Segmentation Using Fuzzy C-Means Clustering This article delves into the process of image segmentation using Fuzzy C-Means (FCM) clustering, a powerful technique for partitioning images into meaningful regions. We'll explore the fundamentals of FCM, its advantages over traditional methods, and provide a step-by-step guide to implementing FCM
7 min read
Thresholding-Based Image Segmentation Image segmentation is the technique of subdividing an image into constituent sub-regions or distinct objects. The level of detail to which subdivision is carried out depends on the problem being solved. That is, segmentation should stop when the objects or the regions of interest in an application h
7 min read
Image Segmentation Models Image segmentation involves dividing an image into distinct regions or segments to simplify its representation and make it more meaningful and easier to analyze. Each segment typically represents a different object or part of an object, allowing for more precise and detailed analysis. Image segmenta
10 min read
Spectral Clustering using R Spectral clustering is a technique used in machine learning and data analysis for grouping data points based on their similarity. The method involves transforming the data into a representation where the clusters become apparent and then using a clustering algorithm on this transformed data. In R Pr
9 min read
Clustering in Machine Learning In real world, not every data we work upon has a target variable. Have you ever wondered how Netflix groups similar movies together or how Amazon organizes its vast product catalog? These are real-world applications of clustering. This kind of data cannot be analyzed using supervised learning algori
9 min read
Clustering Distance Measures Clustering is a fundamental concept in data analysis and machine learning, where the goal is to group similar data points into clusters based on their characteristics. One of the most critical aspects of clustering is the choice of distance measure, which determines how similar or dissimilar two dat
7 min read
Image compression using K-means clustering Prerequisite: K-means clustering The internet is filled with huge amounts of data in the form of images. People upload millions of pictures every day on social media sites such as Instagram, and Facebook and cloud storage platforms such as google drive, etc. With such large amounts of data, image c
6 min read
K means clustering using Weka In this article, we are going to see how to use Weka explorer to do simple k-mean clustering. Here we will use sample data set which is based on iris data that is available in ARFF format. There are 150 iris instances in this dataset. Before starting let's have a small intro about clustering and sim
3 min read
Criterion Function Of Clustering Cluster examination isolates information into bunches (clusters) that are important, valuable, or both. In case significant bunches are the objective, at that point, the clusters ought to capture the common structure of the information. In a few cases, be that as it may, cluster investigation is as
2 min read