1、KMeans算法
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import KMeans, MiniBatchKMeans
import datetime
if __name__ == "__main__":
A = Image.open("photo.jpg", 'r')
rawimage = np.asarray(A)
h,w=A.size
plt.imshow(rawimage)
plt.show()
data = rawimage/ 255.0
data = data.reshape(-1, 3)
k=8
cluster = KMeans(n_clusters=k)
import time
train_start = datetime.datetime.now()
C_Image = cluster.fit_predict(data)
train_end = datetime.datetime.now()
Trainingtime = train_end - train_start
print("训练总耗时为:%s(s)" % (Trainingtime).seconds)
ClusterImage = C_Image.reshape(h,w, )
plt.imshow(ClusterImage)
plt.show()
plt.imsave(str(k)+'photo'+'_kMeans.png',ClusterImage)
运行结果: 原图 8种颜色(耗时26s) 16种颜色(耗时59s) 32种颜色(耗时150s) 64种颜色(耗时288s) 128种颜色(耗时678s) 从上面这几张图片颜色压缩的图可以看出,颜色越多,使用Kmeans算法进行颜色压缩的运行时长越长,时间上跳跃幅度大。并且图像的线条越来越不清晰了。 2、MiniBatchKMeans(小批量处理算法)
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import KMeans, MiniBatchKMeans
import datetime
if __name__ == "__main__":
A = Image.open("photo.jpg", 'r')
rawimage = np.asarray(A)
h,w=A.size
plt.imshow(rawimage)
plt.show()
data = rawimage/ 255.0
data = data.reshape(-1, 3)
k=8
cluster =MiniBatchKMeans(n_clusters=k, batch_size = 256, random_state=3)
import time
train_start = datetime.datetime.now()
C_Image = cluster.fit_predict(data)
train_end = datetime.datetime.now()
print("训练总耗时为:%s(s)" % (train_end - train_start).seconds)
ClusterImage = C_Image.reshape(h,w, )
plt.imshow(ClusterImage)
plt.show()
plt.imsave(str(k)+'photo'+'_MiniBatchKMeans.png',ClusterImage)
运行结果: 原图 8种颜色(耗时0s) 16种颜色(耗时1s) 32种颜色(耗时2s) 64种颜色(耗时3s) 128种颜色(耗时6s) 对比前面的k-mean算法,我们不难发现小批量处理算法的耗时减少了很多,并且图像相对来说线条更为明显,可以看出MiniBatchKMeans小批量处理算法总体优于Kmean算法。
|