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.
pip install sklearn
pip install skimage
I used cluster values 2,3,5,10,15 and 20 and got the following results.
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.