Now Reading
Beginners Guide to Image Compression using K-Means Clustering

Beginners Guide to Image Compression using K-Means Clustering

Himanshu Sharma
Image Compression using K-Means Clustering

Image compression is reducing the size that an image takes while storing or transmitting.  Images of high quality take a lot of memory while storing, whereas the low image of low quality takes less memory. There are many ways by which we can compress images, one of which is K-Means Clustering. Here, we will discuss image compression and demonstrate how image compression can be done using K-Means clustering.

In this article we will cover:

  • What is K-Means Clustering?
  • Image Compression using K-Means clustering
  • Creating Interactive controls to compress image
  • Visualize the compressed image

What is K-Means Clustering?



Clustering is a grouping of different data points which are similar to each other and form different groups which contain similar data points. For example, if we are having a dataset that contains the location of people from all over the world, then we can create different clusters according to different states, such that each cluster contains people of a particular state only. K-Means clustering is a Machine Learning algorithm which works on partitioning data points into predefined distinct clusters in which each data point belongs to only one cluster. If we go around the statistics behind it then we can see that K-Means Clustering as the name suggests takes in account the arithmetic mean of the data point and form clusters containing the homogenous data i.e. data points having similar mean in one cluster. 

As we know that an image consists of different colours, so while compressing the image using K-Means Clustering we will create clusters of major colours and group all the similar colors in one cluster, forming different clusters for only major colors.

Implementation of Image Compression using K-Means Clustering

K-Means Clustering is defined under the SK-Learn library of python, before using it let us install it by pip install sklearn

a. Importing required libraries

Here we require libraries for Visualization, Compression and creating interactive widgets.

import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as image
%matplotlib inline
plt.style.use("ggplot")
from skimage import io
from sklearn.cluster import KMeans
from ipywidgets import interact, interactive, fixed, interact_manual, IntSlider
import ipywidgets as widgets

b. Loading Images Dataset and analyzing the properties of images

In order to access images, you can have different images stored in a folder and parse the images one at a time. We will set a standard view size i.e. (20,12) for all the images for maintaining consistency. We will also set the axes title as blank.



plt.rcParams['figure.figsize'] = (20, 12)
image = io.imread('image_dataset/Bird.jpg')
labels = plt.axes(xticks=[], yticks=[])
labels.imshow(img);
Image Compression using K-Means Clustering



























image.shape



image.size

The image shape contains the rows, columns and channels in the image. Here we can see that it contains 3 channels because it is a colored picture, similarly, if we check the shape of grayscale images it will have only 1 channel. Image size displayed here shows the total no. of pixels.

We will reshape this image so that it contains only 2 parameters: product of rows and columns i.e. no. of pixels and no. of channels. We will divide the image size by 255 because that is the maximum intensity value for RGB individually.

image_data = (image / 255.0).reshape(749 * 500, 3)
image_data.shape


c. Analyzing the Color Space 

Colorspace is the specific organization of colours in physical appearance where any 2 images having the same colour model can have entirely different colorspace. Here we will analyze the colorspace of our image.

from plot_utils import plot_utils
color = plot_utils(image_data, title='Original possible colors')
color.colorSpace()
Image Compression using K-Means Clustering

As our image is based on the RGB model we can see the colorspace according to Red, Green and Blue colors. We will reduce these possible colors from 16 Million to only 160 colors and visualize the colorspace again.

As we are going to reduce this colorspace to 160 it means we have to make 160 clusters and pass it to K Means clustering.

from sklearn.cluster import MiniBatchKMeans
kmeans = MiniBatchKMeans(16).fit(image_data)
k_colors = kmeans.cluster_centers_[kmeans.predict(image_data)]
reduced = plot_utils(image_data, colors=k_colors, title="Reduced color space only 160 colors")
reduced.colorSpace()






Here we can clearly see that the color scheme is changed as compared to the original colorspace to only 160 Colors. Now we will work on creating an interactive workspace where we can select different images and select different sizes of clusters from 1 to 256.

d. Image Compression using Interactive widgets

Now we will create an image compression module, but let’s make it a little interactive. So what we will do is, we will create widgets using the ipywidgets library of python. It is mainly used for creating widgets like range slider, dropdown menu etc. 

We will create a range slider which will allow us to choose the value of the K i.e the number of clusters, also we will create a dropdown menu from which we can select different images from our image dataset.

See Also
semi-supervised learning
Is Semi-Supervised Learning The Future Of Image & Video Classification?

We will wrap this up in a single user-defined function and display the comparison of the image with the original one.

We will use @interact from ipywidgets which automatically creates the user interface. 

#location of image dataset
img_lib = 'image_dataset/'  
@interact
#defining compression function
def compression(image=os.listdir(img_lib), k=IntSlider(min=1, max=256, 
                             step=1,value=160,  continuous_update=False, 
                                         layout=dict(width='100%'))):

                        

   #loading the image and reshaping it as done above
    input_img = io.imread(img_dir + image)
    img_data = (input_img / 255.0).reshape(-1, 3)
  
    #Using K value to create clusters
    kmeans = MiniBatchKMeans(k).fit(img_data)
    k_colors = kmeans.cluster_centers_[kmeans.predict(img_data)]
 
    # Reshaping the image according to the clusters
    k_img = np.reshape(k_colors, (input_img.shape))

    #Plotting the compressed and original image
    fig, (ax1, ax2) = plt.subplots(1, 2)
    fig.suptitle('K-means Image Compressor', fontsize=20)

    ax1.set_title('Compressed Image')
    ax1.set_xticks([])
    ax1.set_yticks([])
    ax1.imshow(k_img)

    ax2.set_title('Original (16Million Colors)')
    ax2.set_xticks([])
    ax2.set_yticks([])
    ax2.imshow(input_img)

    plt.subplots_adjust(top=0.85)
    plt.show()
    import matplotlib
    matplotlib.image.imsave('Compressed.jpg', k_img)

Image Compression using K-Means Clustering

As we can see here the image is compressed to 160 colors only and comparing it to the original image which is of around 16 Million colors. 

In the above code we can see that while defining the function we pass the parameters for:

  1. Drop Down for Image Selection
  2. Range Slider: In this we passed the min, max value of slider, value to slider will set by default, step defines the number of increment when slides and the layout which is set to 100%

Other than this we can see in function how we reshaped the original image to the compressed image with the range slider value as given by user or by default. We can also select different images and see the results for them.

We have also downloaded the compressed image, so you can check in your directory that the file is now compressed.






Here, we can see in the above picture that the compressed image is having a lesser size as compared to the original image.

The function is clearly well defined and segregated in 3 sections:

  1. Using K value to create the clusters.
  2. Reshaping the original image according to K value
  3. Visualizing and comparing the original and compressed images.   
  4. Downloading the compressed image.

Conclusion

In this article, we saw what are clusters and how we can use the K-Means clustering algorithm to compress images. We also learned about the statistics behind the K Means Clustering and how it works. Simultaneously we learned how we can use ipywidget to create widgets and created a User Interactive image compression module. 

Provide your comments below

comments


If you loved this story, do join our Telegram Community.


Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top