cyrusharmon.org

Cyrus Harmon's new completely useless blog

 

k-means clustering of image pixels in opticl

posted by cyrus in Lisp

There's a new function, k-means-cluster-image-pixels, in opticl that does k-means clustering of the pixels in an image. It can be invoked thusly:

(in-package :opticl-test)  
 
(write-image-file  
 (output-image "fish-clusters.png")  
 (let ((in (read-image-file (test-image "fish.png"))))  
   (with-image-bounds (height width) in  
     (let ((img (make-8-bit-rgb-image height width)))  
       (let ((k 24))  
         (multiple-value-bind (means assignments)  
             (time (k-means-cluster-image-pixels  
                    (dilate (blur-image in)  
                            (make-8-bit-rgb-image 3 3 :initial-element 1))  
                    k))  
           (set-pixels (i j) img  
             (let ((m (aref assignments i j)))  
               (pixel means m 0)))))  
       img)))) 

That takes an image that looks like this:

fish

and yields an image such as this:

fish-clusters

k-means clustering of image pixels in opticl