Image Compression with K-Means Clustering

An image comprises of several very small intensity values (dots) known as Pixels. The colored image is the image that includes color information for each pixel. In a colored image, each pixel is of 3 bytes containing RGB (Red-Blue-Green) values having Red intensity value, then Blue and then Green intensity value for each pixel. As you may have observed, the size of a colored digital image can be huge as each pixel requires 3 bytes or (3X8) bits intensity values. So, these images are often stored as the compressed images with lesser number of bits for intensity values and hence, lesser memory for storage.


Tasks involved in this Project

This project on Image Compression with K-Means Clustering is divided into the following tasks:

     Task 1: Import essential modules

  • Import essential modules and helper functions from NumPy, Matplotlib, scikit-learn, and Jupyter Widgets.

    Task 2: Data Pre-processing

  •  Import images from a local directory and store them as numpy arrays.
  •  Explore the image attributes.
  •  Normalize the pixel values and unroll the arrays into vectors.

    Task 3: Visualizing the Color Space using Point Clouds

  •  Visualize the set of pixels from the original image as a two 2-D point clouds in color space.

    Task 4: Visualizing the K-Means Reduced Color Space

  •  Perform k-means clustering with scikit-learn's MiniBatchKMeans to reduce the number of possible colors in the image from over 16 million to 16.
  • Compare and contrast the color space of the original image with that of the k-means compressed image.

    Task 5: Creating Interactive Controls with Jupyter Widgets

  • Use the interact function to automatically create UI controls for function arguments.
  • Define an argument to control the value of k using a slider.
  • Define an argument to pick any image from a specified directory.

Writing code in Python/Jupyter Notebook

1. Importing Essential Modules

from __future__ import print_function
%matplotlib inline
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as image"ggplot")
from skimage import io
from sklearn.cluster import KMeans
from ipywidgets import interact, interactive, fixed, interact_manual, IntSlider
import ipywidgets as widgets
plt.rcParams['figure.figsize'] = (20, 12)

2. Data Pre-processing

img = io.imread('images/1-Saint-Basils-Cathedral.jpg')
ax = plt.axes(xticks=[], yticks=[])

3. Visualizing the Color Space using Point Clouds

from plot_utils import plot_utils
x = plot_utils(img_data, title='Input color space: 16 million possible colors')

4. Visualizing the K-means Reduced Color Space

from sklearn.cluster import MiniBatchKMeans
kmeans = MiniBatchKMeans(16).fit(img_data)
k_colors = kmeans.cluster_centers_[kmeans.predict(img_data)]
y = plot_utils(img_data, colors=k_colors, title="Reduced color space: 16 colors")

5. K-means Image Compression with Interactive Controls

img_dir = 'images/'
def color_compression(image=os.listdir(img_dir), k=IntSlider(min=1,max=256,step=1,value=16,
input_img = io.imread(img_dir + image)
img_data = (input_img / 255.0).reshape(-1, 3)
kmeans = MiniBatchKMeans(k).fit(img_data)
k_colors = kmeans.cluster_centers_[kmeans.predict(img_data)]
#After K-means has converged, load the large image into your program and
#replace each of its pixels with the nearest of the centroid colors you found
#from the small image.
k_img = np.reshape(k_colors, (input_img.shape))
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('K-means Image Compression', fontsize=20)
ax2.set_title('Original (16,777,216 colors)')

Screenshots From My Jupyter Notebook

Post a Comment

Post a Comment (0)

Previous Post Next Post