最近看到某同学在串讲k-means,
而且说它是很简单的一个聚簇算法,于是就抽空google了一下,发现还真的很简单,而且有点意思。
于是参考了一些文章和代码,自己用php写了一个demo。
参考:
http://scikit-learn.org/stable/auto_examples/cluster/plot_color_quantization.html
http://www.medphysics.wisc.edu/~ethan/kmeans/kmeans.zip
http://en.wikipedia.org/wiki/Vector_quantization
<?PHP /** 1) load image,W*H*C 2) turn into (W*H, C) 3) sample 1000 pixels 4) kmeans sample data and get 64 centers. 5) divide real data into 64 groups 6) output */ class A { private $is_debug = true; private $file_name; private $output_file_name; private $im; private $width; private $height; private $d = 3; //RGB private $data ; private $sample_count = … |