How Okay-Means can be utilized to considerably scale back the file dimension of a picture.
On this information, I describe and implement the k-means algorithm from scratch and apply it to picture compression. I take advantage of completely different visualizations to assist the reader develop a stronger understanding of the k-means algorithm and the way it may be used for picture compression. I additionally focus on numerous benefits and limitations of this method in the direction of the top.
All pictures except in any other case famous are by the creator, accessible right here.
The k-means algorithm is an unsupervised algorithm that partitions a dataset into ok distinct clusters. It’s unsupervised, that means there are not any labels for the info factors. In different phrases, we don’t have prior data of how the dataset must be clustered. We merely present the dataset as is, and use k-means to partition it into ok clusters.
Huge Image Thought
Okay-means seeks to divide a dataset into ok clusters the place members of every cluster share traits and are completely different from different clusters. Due to this fact, the aim is for k-means to divide the dataset meaningfully into ok completely different clusters.
Cluster evaluation teams related knowledge collectively by abstracting the underlying construction of a dataset, which might present significant perception. “Clustering has been successfully utilized in a wide range of engineering and scientific disciplines comparable to psychology, biology, medication, pc imaginative and prescient, communications, and distant sensing” .
The Okay-means algorithm is damaged into a number of steps:
- Initializing a set of cluster centroids
- Assigning observations to clusters
- Updating the clusters
Steps 2 and three are repeated for both a set variety of iterations or till convergence, which happens when the cluster centroids not change.
Allow us to dive deeper into every of those steps.
1. Initializing the set of cluster centroids
Step one to initializing the set of cluster centroids is selecting what number of centroids we need to use, which we seek advice from as ok.
As soon as, now we have chosen the variety of clusters, we select ok samples randomly from the coaching examples, and set the cluster centroids to be equal to the values of the chosen ok examples. Alternatively, we are able to randomly pattern ok completely different factors within the resolution house to initialize the cluster centroids.
We seek advice from the j-th centroid as μⱼ, as a result of it represents the imply of the values assigned to cluster j. That is the place the identify k-means arises from. Within the determine under, we set ok=3 and randomly pattern 3 factors within the pattern house (represented by inexperienced, blue, and pink ‘x’) to initialize the cluster centroids.
2. Assigning Observations to Clusters
Now that now we have our ok centroids, we assign every remark (knowledge level) to the centroid closest to it. Sometimes, we calculate “closeness” utilizing the euclidean distance. Within the determine under, we illustrate the method of assigning the observations to the three centroids above.
3. Updating the Centroids
As soon as all the observations have been assigned to a cluster, we shift the centroid of every cluster to the imply of its assigned observations. We illustrate this course of under.
Repeat until convergence or for a sure variety of iterations
Every iteration of the k-means algorithm consists of two elements: Step 2 (Assigning Observations to Clusters) and Step 3 (Updating the Clusters). This course of is repeated for both a set variety of iterations or till convergence. Convergence happens when the cluster centroids not change. That is equal to saying that the assignments of the observations don’t change anymore.
The ok means algorithm will at all times converge inside a finite variety of iterations  however it’s prone to native minima .
Within the instance under, the ok means algorithm converges at iteration 4. It’s because the cluster centroids not change after iteration 4.
The aim of picture compression is to cut back the file dimension of a picture. We are able to use Okay-means to pick out ok colours to characterize a complete picture. This enables us to characterize a picture utilizing solely ok colours, as a substitute of the whole RGB house. This course of can also be known as picture quantization.
Why Okay-means is helpful for picture compression
The aim of utilizing k-means for picture compression is to pick out ok colours to characterize a goal picture with the least approximation error. In different phrases, we might be utilizing k-means to seek out the finest ok colours to characterize a goal picture with.
How Okay-means supplies compression
The colour pixels in a picture are represented by their RGB values, which every vary from 0 to 255. Since every colour band has 256=2⁸ settings, there are a complete of 256 ⋅ 256 ⋅ 256 = 256³ = 2²⁴ ~ 17 million colours. To characterize every of those colours for any pixel, computer systems want log₂(2²⁴) = 24 bits of cupboard space. If we use Okay-means to pick out 16 colours that we characterize a complete picture with, we solely want log₂(16) = 4 bits. Due to this fact, by utilizing Okay-means with ok=16, we are able to compress the picture dimension by an element of 6!
Now that we perceive the idea, allow us to dive into some code and visualizations.
Studying within the Picture and Initializing the Centroids
Within the context of picture compression, the centroids are the colours we’re utilizing to characterize the compressed picture. Due to this fact, our first step is to learn within the picture and choose ok random colours from the picture to initialize our centroids.
In line 7, we learn within the picture utilizing numpy. This produces a 2 dimensional array the place every component is a listing of size 3 that represents the RGB values of that pixel. Keep in mind to change
image_path to your personal.
Beginning on line 9, we outline the perform to initialize our centroids. We choose a random pixel from the picture and add its corresponding RGB worth to the
centroids_init array. We do that ok =
num_clusters instances. Thus,
centroids_init is an array of ok colours sampled randomly from our picture.
Assigning and Updating Centroids
To iteratively replace our centroids, we repeat the method of assigning observations to cluster centroids and updating the centroids to be the imply of the assigned observations.
Within the context of picture compression, this implies assigning every pixel of the goal picture to the centroid colour that’s nearest to it.
In strains 11–17, we’re creating the dictionary
centroid_rgbs . Every key corresponds to an index of the centroids and the values are a single numpy array that comprise all the colours assigned to the corresponding centroid.
The task of every pixel to a centroid is completed on line 13 utilizing
linalg.normto calculate the euclidean distance to every centroid after which utilizing
argminto seek out the index of the closest centroid.
Creating the Compressed Picture
Now that now we have the finalized centroids, we are able to create the compressed picture. We merely iterate by every pixel and alter its colour to the closest centroid.
Placing Every little thing Collectively
With the next snippet and the perform definitions above, all of the items to operating k-means for picture compression are full.
To generate the GIF’s, I used
plt.savefig at numerous levels of the algorithm. My github repository comprises the code for that course of, and tips on how to convert these frames to a GIF .
Within the GIF above, we see how the centroids, that are the colours we select to characterize the picture, change over time because the k-means algorithm iterates.
Now, we analyze some particulars relating to using k-means for picture compression.
Usually, pictures will comprise outlier colours relative to the primary colour palette of the picture. For instance, the goal picture under comprises two small clown-fish which can be vibrant orange. Their colour contrasts strongly from the darkish background and sea anemone, which attracts the viewers consideration to them (hopefully in an aesthetically pleasing approach).
The GIF under illustrates what occurs once we apply k-means to this picture for ok=16. Though the clown-fish’s vibrant orange is chosen as an preliminary cluster, it’s ultimately ‘washed out’ by the darker colours because the algorithm iterates.
Though the general high quality of the compressed picture will increase because the variety of iterations will increase, the accuracy of the outlier colour decreases.
Some literature suggests creating clusters particularly for outliers (calculated utilizing a distance metric) to enhance general clustering accuracy . The authors’ use of numerical experiments on each artificial knowledge and actual knowledge are offered to exhibit the effectiveness and effectivity of their proposed algorithm. I think that implementing this algorithm might assist with picture compression utilizing k-means, particularly with pictures that comprise outlier colours.
The selection of ok determines the quantity of compression, and is as much as the person to set. The next worth of ok will present for a extra devoted illustration of the goal picture, however comes at the price of decrease compression. Within the graphic under, we illustrate compressed pictures with growing values of ok. The compression components for ok=16, ok=32, ok=64, and ok=128 are 6, 4.8, 4, and 3.4 respectively.
Within the instance above, we see that selecting a ok worth larger than 32 is essential in mitigating the outlier subject talked about within the earlier part. Since ok is giant sufficient, at the very least one centroid is ready to be assigned to the brilliant orange colour. Within the determine under, we plot the centroid colours after 30 iterations for each ok=64 and ok=256.
After 30 iterations, ok=64 has one centroid that’s assigned to orange. For ok=256, there are roughly 4 shades of orange.
This visualization additionally portrays the compression quantity vs. element trade-off for various k-values. Clearly for bigger values of ok, now we have extra colours and retention of element, nevertheless we require extra knowledge to characterize these colours.
It’s doubtless value experimenting with completely different values of ok relying on the goal picture and use case.
Utilizing the k-means algorithm to compress a picture is a type of lossy compresion. Lossy compression is a category of information compression that makes use of approximations and partial knowledge lack of a goal picture . Once we use k-means for picture compression, we’re approximating every pixel’s colour utilizing the closest centroid. Since we’re dropping data on this course of, we can’t revert the compressed picture again to the unique. That is why lossy compression can also be refered to as irreversible compression.
Alternatively, lossless knowledge compression doesn’t lose data. As an alternative, it makes use of methods to characterize the unique data utilizing much less knowledge . Nonetheless, the quantity of compression that’s doable utilizing lossless knowledge compression is far decrease than that of lossy compression.
Though k-means is a type of lossy compression, the lack of element might be virtually in-perceivable to the human eye for sure values of ok.
Are you able to discover many variations between the 2 pictures above? Utilizing ok=256, the compressed picture on the suitable requires only one/3 the quantity of information in comparison with the total picture on the suitable!
Randomness in Centroid Initialization
Holding every part with reference to k-means fixed, every run will differ barely because of the randomness inherent within the centroid initialization course of.
Because of this the compressed pictures given the identical parameters will output barely completely different variations. Nonetheless, for bigger values of ok, this impact just isn’t as obvious to the human eye.
Now that now we have accomplished an intensive evaluation of the k-means algorithm with reference to picture compression, we’ll explicitly focus on its benefits and downsides.
- Effectivity: The k-means algorithm is computationally environment friendly (linear time complexity), making it appropriate for real-time picture compression purposes . This additionally means it might probably deal with giant pictures.
- Simplicity: The k-means algorithm is comparatively easy and straightforward to grasp.
- Nice for sure forms of pictures: k-means performs properly on pictures with distinct clusters of comparable colours.
- Lossy Compression Algorithm: Okay-means is a type of lossy compression that represents a complete picture primarily based on clusters of pixels, due to this fact it loses some colour data and should not protect high quality particulars in a picture.
- Sensitivity to initialization: The efficiency of the k-means algorithm might be delicate to the preliminary positions of the centroids, which might result in sub-optimal or inconsistent outcomes. That is much less of an issue with bigger values of ok.
- Not appropriate for sure forms of pictures: k-means algorithm carry out poorly on pictures with clean colour gradients and pictures with excessive noise.
Total, k-means could be a good selection for lossy picture compression, particularly for pictures with distinct clusters of comparable colours. Nonetheless, it will not be the only option for all sorts of pictures and different methods comparable to vector quantization or fractal compression might produce higher outcomes.
The person has a essential choice to make when deciding on the worth of ok, and should take into accout the ‘compression quantity vs. element trade-off’ mentioned within the “Choosing ‘ok’” part. The optimum ok worth will doubtless fluctuate in line with the person’s wants.
Hopefully the completely different visualizations had been capable of assist develop a stronger understanding of the k-means algorithm, and the way it can carry out picture compression.
 Krishna, Okay., and M. Narasimha Murty. “Genetic Okay-Means Algorithm.” IEEE Transactions on Techniques, Man and Cybernetics, Half B (Cybernetics), vol. 29, no. 3, 1999, pp. 433–439., https://doi.org/10.1109/3477.764879.
 Ng, Andrew. “CS229 Lecture Notes.” https://cs229.stanford.edu/notes2022fall/main_notes.pdf
 My Github Repository with the code for this mission. https://github.com/SebastianCharmot/kmeans_image_compression
 Gan, Guojun, and Michael Kwok-Po Ng. “Okay -Means Clustering with Outlier Elimination.” Sample Recognition Letters, vol. 90, 2017, pp. 8–14., https://doi.org/10.1016/j.patrec.2017.03.008.
 “Lossy Compression (Article).” Khan Academy, Khan Academy, https://www.khanacademy.org/computing/computers-and-internet/xcae6f4a7ff015e7d:digital-information/xcae6f4a7ff015e7d:data-compression/a/lossy-compression.
 Ming Yang, and N. Bourbakis. “An Overview of Lossless Digital Picture Compression Strategies.” forty eighth Midwest Symposium on Circuits and Techniques, 2005., 2005, https://doi.org/10.1109/mwscas.2005.1594297.
 Chiou, Paul T., et al. “A Complexity Evaluation of the JPEG Picture Compression Algorithm.” 2017 ninth Laptop Science and Digital Engineering (CEEC), 2017, https://doi.org/10.1109/ceec.2017.8101601.