Spaces:
Build error
Build error
File size: 847 Bytes
6250360 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import logging
import cv2
import numpy as np
import util.dec
import util.np
@util.dec.print_calling
def kmeans(samples, k, criteria = None, attempts = 3, flags = cv2.KMEANS_RANDOM_CENTERS):
if criteria == None:
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
samples = np.asarray(samples, dtype = np.float32)
_,labels,centers = cv2.kmeans(samples, k, criteria, attempts, flags)
labels = util.np.flatten(labels)
clusters = [None]*k
for idx, label in enumerate(labels):
if clusters[label] is None:
clusters[label] = []
clusters[label].append(idx)
for idx, cluster in enumerate(clusters):
if cluster == None:
logging.warn('Empty cluster appeared.')
clusters[idx] = []
return labels, clusters, centers
|