import numpy as np
from PIL import Image as image
def loadData(filePath):
f = open(filePath,'rb')
data= []
img =image.open(f)
m,n =img.size
for i in range(m):
for j in range(n):
x,y,z =img.getpixel((i,j))
data.append([x/256.0,y/256.0,z/256.0])
f.close()
return np.mat(data),m,n
imgData,row,col =loadData('apple.jpg')
from sklearn.cluster import KMeans
km=KMeans(n_clusters=3)
label =km.fit_predict(imgData)
label=label.reshape([row,col])
pic_new = image.new("L",(row,col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i,j),int(256/(label[i][j]+1)))
pic_new.save("KM_Apple.jpg","JPEG")
调整参数 K,(为km=KMeans(n_clusters=3)
中参数n_clusters
)得到如下结果:
K=2 |
K=3 |
K=6 |
from skfuzzy.cluster import cmeans
imgData = imgData.T
center, u, u0, d, jm, p, fpc = cmeans(imgData, m=2, c=6, error=0.0001, maxiter=1000)
for i in u:
label = np.argmax(u, axis=0)
label = label.reshape([row, col])
print(label)
pic_new = image.new('L', (row, col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i, j), int(256 / (label[i][j] + 1)))
pic_new.save('FCM_apple.jpg','JPEG')
调整参数 C C C,(为center, u, u0, d, jm, p, fpc = cmeans(imgData, m=2, c=6, error=0.0001, maxiter=1000)
中参数c
)得到如下结果:
C=3 |
C=6 |
C=9 |
import sklearn.cluster as sc
bw = sc.estimate_bandwidth(imgData, n_samples=500, quantile=0.2)
model = sc.MeanShift(bandwidth=bw, bin_seeding=True)
label = model.fit_predict(imgData)
label = label.reshape([row, col])
pic_new = image.new('L', (row, col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i, j), int(256 / (label[i][j] + 1)))
pic_new.save('MS_Apple.jpg','JPEG')
bw=0.01 |
bw=0.17 |
bw=0.5 |
from sklearn.cluster import SpectralClustering
label = SpectralClustering(n_clusters=3).fit_predict(imgData)
label=label.reshape([row,col])
pic_new = image.new("L",(row,col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i,j),int(256/(label[i][j]+1)))
pic_new.save("SP_apple.jpg","JPEG")
from sklearn.cluster import AffinityPropagation
label = AffinityPropagation(damping=0.9, max_iter=20, convergence_iter=15, copy=True, preference=None, affinity='euclidean', verbose=False).fit_predict(imgData)
for i in range(row):
for j in range(col):
pic_new.putpixel((i,j),int(256/(label[i][j]+1)))
pic_new.save("AP_apple.jpg","JPEG")
max_iter=20 |
max_iter=50 |
max_iter=100 |
from sklearn.cluster import Birch
label = Birch(n_clusters = 2).fit_predict(imgData)
label=label.reshape([row,col])
pic_new = image.new("L",(row,col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i,j),int(256/(label[i][j]+1)))
pic_new.save("Birch_apple.jpg","JPEG")
from sklearn.cluster import DBSCAN
label = DBSCAN(eps=0.005,min_samples=1).fit_predict(imgData)
label=label.reshape([row,col])
pic_new = image.new("L",(row,col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i,j),int(256/(label[i][j]+1)))
pic_new.save("DBSCAN_apple.jpg","JPEG")
eps=0.005 |
eps=0.07 |
eps=0.072 |
from sklearn import mixture
label=mixture.GaussianMixture(n_components=4,covariance_type='full')
.fit_predict(imgData)
label=label.reshape([row,col])
pic_new = image.new("L",(row,col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i,j),int(256/(label[i][j]+1)))
pic_new.save("GMM_apple.jpg","JPEG")
ncomponents=2 |
ncomponents=3 |
ncomponents=5 |
from sklearn.cluster import OPTICS
label = OPTICS(min_samples=0.1, xi=0.005, min_cluster_size=0.05).fit_predict(imgData)
label=label.reshape([row,col])
pic_new = image.new("L",(row,col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i,j),int(256/(label[i][j]+1)))
pic_new.save("OPTICS.jpg","JPEG")
from sklearn.cluster import AgglomerativeClustering
label = AgglomerativeClustering(n_clusters=3).fit_predict(imgData)
label=label.reshape([row,col])
print(label)
pic_new = image.new("L",(row,col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i,j),int(256/(label[i][j]+1)))
pic_new.save("AC_apple.jpg","JPEG")
博主: 于2020年毕业于安庆师范大学数学与计算科学学院信息与计算科学专业,QQ(1755826272)
说明: 本博客系毕业论文原文代码,距毕业近两年之久,今得兴致,整而发之!秉着开源精神将此代码进行公开,供大家学习使用。 转载请联系本人!!!
关于毕业论文::毕业论文的撰写过程涉及查阅文献资料、展开实验、撰写论文、查重一系列操作,其中滋味唯有经历者可意会。作者在实验过程中花去两月余时间,疫情在家,白天睡觉,晚上点灯调试代码,其中百般滋味。借撰写毕业论文之机会作者代码能力有所提升,查阅资料能力也随之提升。
关于博客内容: 博客写作仅供分享学习之用,如有理论方面困惑请移步他处自行查找,稍后博主可能补充,也可能不会补充,随性而为。如遇理论方面之困顿可联系博主讨论,欢迎来扰!