我正在Coursera上学习Machine Learnirg,并在Octave上实现它之后,我正在Python上学习。我正在进行一个练习,该练习采用RGB颜色的128x128图像,将具有16个簇的K均值应用于其中(以找到最重要的16种颜色),然后再次打印图像。问题是,当我在Octave上运行图像时,图像看起来与原始图像相似,但是在Python上,图像看起来却不太相似(images)。
我试图更改聚类的参数,但是没有任何解决方案。我正在执行的代码如下:
import imageio
from sklearn.cluster import KMeans
image = imageio.imread('bird_small.png')
extended_image = image.reshape(128*128, 3)
extended_image = extended_image/255
image_km = KMeans(n_clusters=16, n_init=100)
image_km.fit(extended_image)
clusters = image_km.predict(extended_image)
centroids = image_km.cluster_centers_
reduced_extended_image = extended_image
# Here I substitue each data color point for the centroid
for i in range(0, len(extended_image)):
color = clusters[i]
reduced_extended_image[i, :] = centroids[color, :]
compressed_image = reduced_extended_image.reshape(128, 128, 3)
plt.imshow(compressed_image)
如果您尝试运行代码,则“ bird_small.png”图片为here
您知道问题可能在哪里吗?谢谢