Image Compression using K-Means Clustering
By: Satyam Kumar
Tags: Machine Learning, Artificial Intelligence, Towards Data Science, Image Processing, Data Science

What is K-Means Clustering?

K-Means algorithm is a centroid based clustering technique. This technique cluster the dataset into k different clusters. Each cluster in the k-means clustering algorithm is represented by its centroid point.

Left Image: Plot of the dataset, Right Image: Plot of the result of 3-means clustering, (Image 1)

The above image (image 1) describes how 3 clusters are formed for a given dataset using the k-Means clustering algorithm with the value of k=3.

Further, read this article to know more about the k-Means Clustering algorithm.

How does the K-Means Clustering technique compress the image?

In a colored image, each pixel is of size 3 bytes (RGB), where each color can have intensity values from 0 to 255. Following combinatorics, the total number of colors which can be represented is 256*256*256 ( equal to 16,777,216). Practically, we can visualize only a few colors in an image very less than the above number. So the k-Means Clustering algorithm takes advantage of the visual perception of the human eye and uses few colors to represent the image. Colors having different values of intensity that are RGB values seem the same to the human eye. The K-Means algorithm takes this advantage and clubs similar looking colors (which are close together in a cluster). Here’s an illustration of how this works:

Choosing some pixels from the input image, (Image 2)

In the above image (Image 2), few pixels are picked and expanded in the further images to continue the illustration.

Left: A maximized image of above-picked pixels, Right: Two nearby pixels x and y, (Image 3)

The pixels picked in (Image 2) from an input image is expanded in the left part of (Image 3). In the right part of (Image 3) two nearby pixels are picked (names as ‘x’ and ‘y’).

If the RGB value of ‘x’ and ‘y’ pixel is (130, 131, 140) and (127, 132, 137) respectively, then below is the illustration of how these two-pixel colors are visible to a human eye. The below illustration of RGB intensity to color is prepared from w3schools.

Above Image: Color for RGB(130, 131, 140), Below Image: Color for RGB(127, 132, 137), (Image 4)

In the above image (Image 4), it is observed that for some amount of changes in the RGB values the color resembles the same to a human eye. So k-Means clustering can club these two colors together and can be represented by a centroid point that has almost the same resemblance to a human eye.

The initial dimension of an image is 750*1000 pixels. For each pixel, the image has 3-dimension representing RGB intensity values. The RGB intensity values range from 0 to 255. Since intensity value has 256 values (2**8), so the storage required to store each pixel value is 3*8 bits.

Finally, the initial size of the image is (750*1000*3*8) bits.

Total number of color combination equals (256*256*256) ( equal to 16,777,216). As the human eye is not able to perceive so many numbers of colors at once, so the idea is to club similar colors together and use less number of colors to represent the image.

We will be using k-Means clustering to find k number of colors which will be representative of its similar colors. These k-colors will be centroid points from the algorithm. Then we will replace each pixel values with its centroid points. The color combination formed using only k values will be very less compared to the total color combination. We will try different values of k and observe the output image.

If k=64 then the final size of the output image will be (750*1000*6 + 64*3*8) bits, as intensity value ranges to 2**6.

If k=128 then the final size of the output image will be (750*1000*7 + 128*3*8) bits, as intensity value ranges to 2**7.

Hence it is observed that the final size of the image is reduced to a great extent from the original image.