K-Means Clustering for Image Segmentation: An Introduction

An unsupervised learning algorithm

Ujang Riswanto
5 min readFeb 6, 2023
Photo by Alexander Park on Unsplash

Introduction

Image segmentation is a process of dividing an image into multiple segments or regions, each of which corresponds to a different object or part of the image. It is an important step in image analysis, which is used in various fields such as computer vision, medical imaging, remote sensing, and more. Image segmentation is used to identify and locate objects and boundaries in an image, which can then be used for various tasks such as object detection, image compression, and image editing.

K-means clustering is a popular algorithm used for image segmentation. The algorithm is based on the idea of partitioning an image into k clusters, where each cluster represents a different segment or region of the image. The algorithm is simple to understand and implement, and it can be applied to images of various sizes and types.

K-Means Clustering Algorithm

The K-Means Clustering algorithm is a method of partitioning a dataset into k clusters, where k is a user-defined parameter. The algorithm works by iteratively assigning each data point to the cluster with the nearest mean, and then updating the mean of each cluster. The process is repeated until the assignment of data points to clusters no longer changes.

The steps involved in the K-Means Clustering algorithm are as follows:

  1. Initialize k cluster centroids.
  2. Assign each data point to the cluster with the nearest centroid.
  3. Update the cluster centroids by calculating the mean of all data points assigned to the cluster.
  4. Repeat steps 2 and 3 until the assignment of data points to clusters no longer changes.

The advantages of K-Means Clustering include its simplicity, speed, and scalability. It can handle large datasets and is able to find clusters of different shapes and sizes. However, it also has some disadvantages, such as the need to specify the number of clusters in advance and the sensitivity to the initial configuration of cluster centroids.

K-Means Clustering for Image Segmentation

K-Means Clustering can be applied to image segmentation by treating each pixel in the image as a data point and using its color values as features. The algorithm can then be used to partition the image into k clusters, where each cluster represents a different segment or region of the image.

Examples of using K-Means Clustering for image segmentation include segmenting images of natural scenes into different regions, such as sky, water, and land. Another example is segmenting medical images into different tissue types, such as the brain, blood vessels, and tumors.

Compared to other methods of image segmentation, K-Means Clustering has the advantage of being simple and fast. However, it also has some limitations, such as the need to specify the number of clusters in advance and the sensitivity to the initial configuration of cluster centroids.

Implementing K-Means Clustering for Image Segmentation

There are several libraries and tools available for implementing K-Means Clustering for image segmentation in Python, such as OpenCV, Scikit-learn, and Numpy. These libraries provide a range of functions and methods that can be used to read, write, and process images, as well as perform the K-Means Clustering algorithm.

A sample implementation of K-Means Clustering for image segmentation in Python might involve the following steps:

  • import some library
import numpy as np
import cv2
from sklearn.cluster import KMeans
  • Read an image using OpenCV or another library.
img = cv2.imread('image.jpg')
  • Convert the image to a 2D array of pixels, where each pixel is represented by a 3 -dimensional vector of color values (R, G, B). 3. Apply the K-Means Clustering algorithm to the pixel array, using a library such as Scikit-learn.
pixels = img.reshape(-1, 3)
kmeans = KMeans(n_clusters=3)
kmeans.fit(pixels)
  • Assign each pixel to the cluster it was assigned to by the algorithm.
clusters = kmeans.predict(pixels)
  • Replace each pixel’s color values with the mean color values of the cluster it belongs to.
segmented_img = kmeans.cluster_centers_[clusters].reshape(img.shape)
  • Save the resulting segmented image.
cv2.imwrite('segmented_image.jpg', segmented_img)

The code above reads an image using the OpenCV library and converts it to a 2D array of pixels. Each pixel is represented by a 3-dimensional vector of color values (R, G, B).

Next, we use the Scikit-learn library’s KMeans class to perform the K-Means Clustering algorithm on the pixel array. We set the number of clusters to 3, but this can be adjusted as needed.

The KMeans class has a predict method, which we use to assign each pixel to the cluster it was assigned to by the algorithm. We can then replace each pixel’s color values with the mean color values of the cluster it belongs to, and reshape it to the original image shape. This will give us the segmented image which we save using OpenCV’s imwrite method.

In order to optimize the performance of the implementation, it can be useful to preprocess the image and perform some data cleaning prior to the K-Means Clustering algorithm. Additionally, one can try different values for the number of clusters and different initialization methods for the cluster centroids to see which yields the best results.

It’s worth noting that this is a very basic implementation, and there are many other preprocessing steps and parameter tuning that can be done to improve the performance of the segmentation. Also, the number of clusters can be set according to the desired number of segments in the image.

Conclusion

In this article, we have provided an introduction to the topic of image segmentation and the use of the K-Means Clustering algorithm for this purpose. We have explained the steps and advantages of K-Means clustering, how it can be applied to image segmentation, and its limitations. We also provided some hints on how to implement the algorithm using Python libraries and how to optimize the performance. Image segmentation is an important step in image analysis, with various applications in fields such as computer vision, medical imaging, and remote sensing. The k-Means Clustering algorithm is a simple and efficient approach for image segmentation, but it has also some limitations that one should be aware of. Future research in this field could focus on developing new methods for image segmentation that can overcome these limitations.

References

  • Jain, A.K., Murty, M.N., and Flynn, P.J. (1999). Data clustering: A review. ACM Computing Surveys, 31(3), 264–323.
  • R.C. Gonzalez, R.E. Woods, Digital Image Processing, Pearson, 2008.
  • S. Theodoridis, K. Koutroumbas, Pattern Recognition, Academic Press, 2008.
  • OpenCV documentation, available at https://opencv.org/
  • Scikit-learn documentation, available at https://scikit-learn.org/
  • Numpy documentation, available at https://numpy.org/

--

--

Ujang Riswanto
Ujang Riswanto

Written by Ujang Riswanto

web developer, uiux enthusiast and currently learning about artificial intelligence

No responses yet