【发布时间】:2022-01-10 04:51:34
【问题描述】:
我是计算机视觉方面的新手,我正在尝试绘制一个图来总结图像上的颜色识别。我使用 KMeans 来查找需要检测的像素值。但我想通过调色板或饼图的可视化来改进代码。我想我应该计算落在每个集群上的像素值。但是,我只是卡住了,我不知道该怎么做。
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np
from collections import Counter
from skimage import io, color
def kmeans_segementation(image, n_clusters, random_state=0):
rows, cols, channels = image.shape #get the image shape
X = image.reshape(rows*cols, channels)
kmeans = KMeans(n_clusters=n_clusters, random_state=0).fit(X)
labels = kmeans.labels_.reshape(rows, cols)
labels = kmeans.cluster_centers_ # get group of colors
ordered_colors = [labels[key] for key,value in enumerate(labels)]
print(ordered_colors)
img2_rgb = io.imread('frame41.png')
n_clusters = 15
kmeans_segementation(img2_rgb, n_clusters, random_state=0)
【问题讨论】:
标签: python matplotlib scikit-learn k-means