计算簇中的像素数(Kmeans 颜色检测)

Counting number of pixels in a cluster (Kmeans color detection)

我正在编写一个代码,用于分析图像并使用 KMeans 聚类找到三种最主要的颜色。代码工作正常,但我想计算三个簇中的像素数。

代码:

class Cluster(object):

    def __init__(self):
        self.pixels = []
        self.centroid = None


    def addPoint(self, pixel):
        self.pixels.append(pixel)


    def setNewCentroid(self):
        R = [colour[0] for colour in self.pixels]
        G = [colour[1] for colour in self.pixels]
        B = [colour[2] for colour in self.pixels]

        R = sum(R) / len(R)
        G = sum(G) / len(G)
        B = sum(B) / len(B)

        self.centroid = (R, G, B)
        self.pixels = []        
        return self.centroid


class Kmeans(object):
    def __init__(self, k=2, max_iterations=5, min_distance=2.0, size=200):
        self.k = k
        self.max_iterations = max_iterations
        self.min_distance = min_distance
        self.size = (size, size)


    def run(self, image):
        self.image = image
        self.image.thumbnail(self.size)
        self.pixels = numpy.array(image.getdata(), dtype=numpy.uint8)

        self.clusters = [None for i in range(self.k)]
        self.oldClusters = None

        randomPixels = random.sample(self.pixels, self.k)

        for idx in range(self.k):
            self.clusters[idx] = Cluster()
            self.clusters[idx].centroid = randomPixels[idx]

        iterations = 0
        while self.shouldExit(iterations) is False:

            self.oldClusters = [cluster.centroid for cluster in self.clusters]
            print iterations

            for pixel in self.pixels:
                self.assignClusters(pixel)

            for cluster in self.clusters:
                cluster.setNewCentroid()

            iterations += 1
        return [cluster.centroid for cluster in self.clusters]


    def assignClusters(self, pixel):
        shortest = float('Inf')
        for cluster in self.clusters:
            distance = self.calcDistance(cluster.centroid, pixel)
            if distance < shortest:
                shortest = distance
                nearest = cluster

        nearest.addPoint(pixel)


    def calcDistance(self, a, b):
        result = numpy.sqrt(sum((a - b) ** 2))
        return result


    def shouldExit(self, iterations):
        if self.oldClusters is None:
            return False

        for idx in range(self.k):
            dist = self.calcDistance(
                numpy.array(self.clusters[idx].centroid),
                numpy.array(self.oldClusters[idx])
            )
            if dist < self.min_distance:
                return True

        if iterations <= self.max_iterations:
            return False

        return True

有没有一种方法可以计算分配给每个群集的像素数?我不想要多少个具有相同的值,只想要总数。

你是说,你想做

for cluster in self.clusters:
    print len(cluster.pixels)

每个集群?