Python 3.x KMeans是指在一组图像中找到每个图像的主色

Python 3.x KMeans是指在一组图像中找到每个图像的主色,python-3.x,k-means,image-recognition,Python 3.x,K Means,Image Recognition,我试图使用K-Means在一组图像中找到每幅图像的主色。下面的示例使用python的sklearn.cluster导入中的KMeans 例如,我有一个100x100像素的图像,我想在100x100图像中找到5x5块的主色 我当前的实现(如下)是使用K-Means一次一个地分析每个5x5块,这对于较大的图像尺寸来说非常缓慢。我想将一个图像数组输入到K-Means,并让它返回一个主色数组,其中返回数组中的每个索引对应于图像数组中的索引 目前的执行情况: def get_dominant_color(

我试图使用K-Means在一组图像中找到每幅图像的主色。下面的示例使用python的sklearn.cluster导入中的KMeans

例如,我有一个100x100像素的图像,我想在100x100图像中找到5x5块的主色

我当前的实现(如下)是使用K-Means一次一个地分析每个5x5块,这对于较大的图像尺寸来说非常缓慢。我想将一个图像数组输入到K-Means,并让它返回一个主色数组,其中返回数组中的每个索引对应于图像数组中的索引

目前的执行情况:

def get_dominant_color(image):
    image = image.reshape((image.shape[0] * image.shape[1], 3))

    clt = KMeans(n_clusters = 4)
    labels = clt.fit_predict(image)
    label_counts = Counter(labels)
    dominant_color = clt.cluster_centers_[label_counts.most_common(1)[0][0]]

    divisor = np.sum(dominant_color)
    if divisor != 0:
        # normalize the rgb values
        dominant_color = dominant_color / np.sum(dominant_color)

    return dominant_color
我已经尝试过修改它来调用
clt.fit\u predict(images)
,其中images是一个5x5块的数组,但我相信这将混合所有图像的所有颜色以生成单个输出。如果可能的话,我如何操作它来独立分析每个单独的图像