K-Means Algorithm to Recognize Grayscale

Geetha Rangaswamaiah
Analytics Vidhya
Published in
3 min readSep 20, 2019

K-means is the most popular and widely used Unsupervised Learning Algorithm. It classifies the given unlabeled data into K clusters. When we pass the unlabeled training set and the number of clusters required to K-Means algorithm, it finds some structure or coherence in data points and groups them accordingly.

Let’s apply this algorithm to recognize the shades of gray in a grayscale image.

  • Step 1: Read an image (say RGB), convert it to grayscale, convert the pixel matrix into a column vector. This is our unlabeled data set. Let’s recognize 5 shades — White (W), Light Gray (LG), Gray (G), Dark Gray (DG), Black (B), so number of clusters, K = 5.
  • Step 2: Initialize cluster centroids by randomly selecting K data points from data set.
  • Step 3: Assign each data point to a cluster centroid which is closer to that data point.
MATLAB function to assign the data points to their closest centroids
  • Step 4: Compute the mean of the location of all data points that belong to each cluster and update the centroids with computed mean.
MATLAB function to compute mean and update centroids
  • Step 5: Repeat step 3 and step 4 for say 10 iterations for the algorithm to classify the data points. Once the data points are classified, label them based on their pixel values (255: White, 0: Black and so on).
Example 1
Example 2

Reference:

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Geetha Rangaswamaiah
Geetha Rangaswamaiah