Color Quantization Using K-Means Clustering

Aditya Mohanty
The Startup
Published in
3 min readAug 25, 2020
Picture By Pawel Czerneski On Unsplash

Color Quantization is the process of reducing the number of colors in an image while keeping the visual appearance of the image intact. This is an useful image compression technique which is quite useful for the devices that can show limited number of colors due to memory restriction.

Every image can be represented by three features which is the B,G,R value for each pixel. Considering that our image has pixel values from 0 to 255 we can say that for each image we have 256 * 256 * 256 colors. Now our aim is to reduce this number of colors to a desired amount.

Here we shall apply quantization to the below image.

Steps In Image Quantization:

In the first step we shall try to choose the number of clusters for our image. Here number of cluster basically represents the number of colors we want to represent our image with. In our problem we would see how to represent the image with 3 and 50 colors.

After choosing the number of colors now it is the time to choose the cluster centroids which would be the color representative of the clusters. For example for 3 colors let C1= (120,140,180) ,C2= (125,180,170) ,C3= (122,145,182) be the three cluster centers.

In the next step we shall compute the distance of each point from the cluster centroids. And based on the distance we shall assign each point to the center with shortest distance. To compute the distance we shall consider the euclidean distance. We would continue this process till the maximum number of iteration is reached or the center doesn’t change at all.

The above code snippet shows how to do image quantization using kmeans clustering. Initially we would read the image and transform it into 3 features which is the B,G,R value of the image. Next we have declared the kmeans clustering algorithm using opencv. To do so we have initialized the stopping criterion where it says if the number of iteration is 20 or if all cluster centroid moves within a euclidean of one we shall stop the running.

In the 10th line of the code we are storing the label array and the center matrix. The label array stores the label value for each point whereas the center matrix stores the center of each cluster. In the next line we have replaced the pixel values with their center values.

Output for 5 cluster is as below for the given image

Similarly output for 50 colors is as below

Here we can see as the value of K increases the image becomes more realistic.

This was an overview of image quantization using kmeans clustering.

Add me to your network https://www.linkedin.com/in/aditya-mohanty-7982451a9/

--

--