This is an implementation of the k-means clustering algorithm. Image pixels are represented as 4-dimensional vectors (r,g,b,a) and each pixel is ultimately assigned to one of k clusters. Because the cluster centroids are initialized on random pixels, the final image is (usually) different on each run.
After cloning the repo, npm install
. You may need to install ImageMagick, which is easiest done with Homebrew. Drop a .jpg
image into the directory, and configure the variables jpeg_name
, NUM_ITERATIONS
, and k
in k-means.js
accordingly. Then run node k-means.js
to transform your image!
An interesting pattern emerged while generating k-means'd versions of my profile picture. In 10 runs of the algorithm, with 10 iterations each and K=3, each run appeared to converge at one of only two local optima: .png "Title") .png "Title")
(See the images/10_3/ directory for all 10 runs).
The code and this README are both works-in-progress. Feel free to contact me with suggestions!