本文出自:http://blog.csdn.net/xizhibei
自从上次介绍过c++版本的k-meansK-means之C++及OpenCV实现,感觉有些不足的地方,加上近些时间在学习python(好吧,是觉得Python比Perl好点),而且刚好有对应的OpenCVPython库,于是就写了个Python版本的
from cv import *
import numpy as np
class Cluster:
center = []
pre_center = []
pts = []
color = ()
def is_good_result(cluster):
for c in cluster:
if np.linalg.norm(c.center - c.pre_center) > 1.0:
return False
return True
def update_center(cluster):
for c in cluster:
c.pre_center = c.center
#print len(c.pts)
c.center = np.sum(c.pts,0