Prerequisite: K-means clustering
The internet is filled with huge amounts of data in the form of images. People upload millions of pictures every day on social media sites such as Instagram, Facebook and cloud storage platforms such as google drive, etc. With such large amounts of data, image compression techniques become important to compress the images and reduce storage space.
In this article, we will look at image compression using K-means clustering algorithm which is an unsupervised learning algorithm.
An image is made up of several intensity values known as Pixels. 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.
K-means clustering will group similar colors together into ‘k’ clusters (say k=64) of different colors (RGB values). Therefore, each cluster centroid is the representative of the color vector in RGB color space of its respective cluster. Now, these ‘k’ cluster centroids will replace all the color vectors in their respective clusters. Thus, we need to only store the label for each pixel which tells the cluster to which this pixel belongs. Additionally, we keep the record of color vectors of each cluster center.
Libraries needed –
-> Numpy library:
sudo pip3 install numpy.
-> Matplotlib library:
sudo pip3 install matplotlib.
-> scipy library:
sudo pip3 install scipy
Below is the Python implementation :
- Image Compression using Huffman Coding
- Project Idea | (Model based Image Compression of Medical Images)
- Elbow Method for optimal value of k in KMeans
- DBSCAN Clustering in ML | Density based clustering
- ML | Hierarchical clustering (Agglomerative and Divisive clustering)
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
- MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
- Image Processing in Java | Set 7 (Creating a random pixel image)
- Getting started with Scikit-image: image processing in Python
- Image Processing in Java | Set 11 (Changing orientation of image)
- Image Processing in Java | Set 8 (Creating mirror image)
- ML | Mean-Shift Clustering
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.