Skip to content

This is a program which compresses images by reducing the number of colors and mapping pixel vectors to a single rgb value by making centroids.

Notifications You must be signed in to change notification settings

sharozhaseeb/K-means-Image-Compression

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

K-means-Image-Compression

I have implemented an unsupervised image compression program using K-means clustering. The program is to be given an array of clusters, depending on the values of the clusters respective K-means algorithm is ran and k centroids are calculated on which colors are mapped.
Reducing the number of different colors reduce the size of the image a lot as the pixels can be mapped as a vector corresponding to a single RGB value rather than all pixels having unique values.
I have used skimage for image input output & sklearn.cluster for kmeans algorithm.

To run the program following dependencies have to be installed;

pip install sklearn
pip install skimage

Make sure the correct path is added where you are placing your image files.

I used cluster values 2,3,5,10,15 and 20 and got the following results.

for k = 2;

face k=2

for k = 3;

face k=3

for k = 5;

k=5

for k = 10;

k=10

for k = 15;

k=15

for k = 20;

k=20

Original image;

face

Observation;

What we can observe is that with k = 2 we have very little information of features; we can only observe eyes and the shape of the face as we move to k= 3; we observe all the features and can predict the gender of the person accurately; then on k=5 we get a cartoony image of the person which is a very popular filter nowadays. At k =10 we get the image which will get as close to original with the least size with all features and smooth edges which gets better at k = 15 where we can observe easily the wave of different centroid on the skin of the person. At k=20 we have a compressed image close to the original with a fraction of the size.

About

This is a program which compresses images by reducing the number of colors and mapping pixel vectors to a single rgb value by making centroids.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages