DEV Community

Discussion on: K-Means Clustering for Magic: The Gathering - Card Recommendation

Collapse
 
kenbellows profile image
Ken Bellows

I'm currently taking an AI course for grad school, and one of our projects was to use k-means to implement simple image compression, clustering using Euclidean distance with R, G, and B as the axes. Results were surprisingly good! I can't release the Python code for that implementation because of the plagiarism policy in the course, but maybe I'll try implementing the same thing in JavaScript using the canvas and see how it goes... 🤔

Collapse
 
strikingloo profile image
Luciano Strika

That's awesome! I guess you were finding clusters for the most common colors, and then storing the differences? Maybe even turning similar colors to the centroid one, to get a lossy compression?

I don't really know enough about compression yet, to be honest.
Any good books on Information Theory or Compression you could recommend me?

Collapse
 
kenbellows profile image
Ken Bellows

As for books, tbh I'm not that well read on information theory or image compression either, I basically just learned what I needed to for this project haha. The textbook we're using for this class is the famous Artificial Intelligence: A Modern Approach (warning: large PDF) by Stuart Russel and Peter Norvig, which is an awesome introductory AI textbook, though it sounds like you're already well into AI anyway

Thread Thread
 
strikingloo profile image
Luciano Strika

Thank you! That's a clever algorithm, I'll try it out sometime.
In class, I remember we tried denoising images with PCA, and it worked wonders.

Collapse
 
kenbellows profile image
Ken Bellows

Yeah, basically. We looked for centroids based on the three color channels, so the average Red, Green, and Blue values within each cluster. Then once the centroids converged and stopped shifting around, we rendered a new version of the image with each pixel replaced by the centroid value of its cluster. So essentially you compress the image by reducing it down to only k unique colors. So yes, it's definitely a lossy compression.

I was very surprised at how recognizable the pictures were even with k=5. I mean, it's very clearly compressed, but it still looks surprisingly good. Bump it up to k=16 or k=24 and it'll definitely take a lot longer to run, but the output is very nice.