【问题标题】:Image segregation by k-mean pythonk-mean python的图像分离
【发布时间】:2018-12-17 03:46:21
【问题描述】:

我是机器学习的新手,我正在学习用于图像分离的 k-mean,但我无法理解它的代码:

from matplotlib.image import imread
image = imread(os.path.join("images","unsupervised_learning","ladybug.png"))
image.shape
X = image.reshape(-1, 3)
kmeans = KMeans(n_clusters=8, random_state=42).fit(X)
segmented_img = kmeans.cluster_centers_[kmeans.labels_]
segmented_img = segmented_img.reshape(image.shape)
segmented_imgs = []
n_colors = (10, 8, 6, 4, 2)
for n_clusters in n_colors:
    kmeans = KMeans(n_clusters=n_clusters, random_state=42).fit(X)
    segmented_img = kmeans.cluster_centers_[kmeans.labels_]
    segmented_imgs.append(segmented_img.reshape(image.shape))
plt.figure(figsize=(10,5))
plt.subplots_adjust(wspace=0.05, hspace=0.1)
plt.subplot(231)
plt.imshow(image)
plt.title("Original image")
plt.axis('off')
for idx, n_clusters in enumerate(n_colors):
   plt.subplot(232 + idx)
   plt.imshow(segmented_imgs[idx])
   plt.title("{} colors".format(n_clusters))
   plt.axis('off')
plt.show()

使用的图片: 输出图

特别是这段代码的含义

segmented_img = kmeans.cluster_centers_[kmeans.labels_]

【问题讨论】:

  • 你知道 k-means 是如何工作的吗?
  • 这不是“分离”,它被称为“颜色量化”。了解正确的术语可能会帮助您找到相关的来源。例如,这里有很多关于颜色量化算法的问题,维基百科也有一篇关于它的合理文章。
  • 是的,我知道 k-mean 是如何工作的

标签: python image image-processing machine-learning k-means


【解决方案1】:

我建议您从阅读无监督学习开始,特别是使用 K-means 进行聚类及其一般应用/概念,而不仅仅是图像。

我将注释此代码的每一行以解释发生了什么。

from matplotlib.image import imread #import module
image = imread(os.path.join("images","unsupervised_learning","ladybug.png")) #Read Image
image.shape #Get shape of image, which is height, width and channel(colours)
X = image.reshape(-1, 3) #Reshaping to get color channel first
kmeans = KMeans(n_clusters=8, random_state=42).fit(X) #Applying and fitting K-means clustering
segmented_img = kmeans.cluster_centers_[kmeans.labels_] #centres of the 8 clusters made
segmented_img = segmented_img.reshape(image.shape) #reshape them using the changed image shape
segmented_imgs = []
n_colors = (10, 8, 6, 4, 2) 
for n_clusters in n_colors:
    kmeans = KMeans(n_clusters=n_clusters, random_state=42).fit(X) #Applyting kmeans for each colour, using 10,8,6.... as number of clusters
    segmented_img = kmeans.cluster_centers_[kmeans.labels_] #Repeating as mentioned above
    segmented_imgs.append(segmented_img.reshape(image.shape))
plt.figure(figsize=(10,5)) #Plotting code
plt.subplots_adjust(wspace=0.05, hspace=0.1)
plt.subplot(231)
plt.imshow(image)
plt.title("Original image")
plt.axis('off')
for idx, n_clusters in enumerate(n_colors):
   plt.subplot(232 + idx)
   plt.imshow(segmented_imgs[idx])
   plt.title("{} colors".format(n_clusters))
   plt.axis('off')
plt.show()

在这行代码中,

segmented_img = kmeans.cluster_centers_[kmeans.labels_]

顾名思义,cluster_centers_ 是一个数组属性,它返回集群中心的坐标,labels_ 是一个返回每个点的标签的属性。 因此,segmented_img 包含每个点标签的聚类中心坐标。点。

【讨论】:

    猜你喜欢
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2015-06-28
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2015-09-23
    相关资源
    最近更新 更多