#coding:utf-8 __author__ = 'jmh081701' # This paper mainly studies a classical clustering method:k-means
# We use this algorithm for aRGB Image clustering, What effect can be seen #k-means Principle: '''
input:x[1],x[2],x[3],...x[n], Each of themx[i] All arem Dimension vector, Number of given clustersk
1. Random generationk Representative yuan:z[1],z[2],...z[k]; eachz[i] All are the first.i The central element of a class 2.repeat: To update
xi Categories describedci, bring：|x[i]-z[ci]| Minimum To update z[j],z[j] Equal to categoryG[j] Average of all samples for until:z No change '''
import numpy as np import math import random from PIL import Image cnt=0 def
calculate_zi(Gi,X): # givenGi, It contains elements belonging to this category, Then calculate the center points of these elements # In this example,Gi It contains Subscripts global
cnt sumi=np.zeros(len(X[0])) for each in Gi: cnt+=1 sumi+=X[each]
sumi/=(len(Gi)+0.000000001) zi=sumi return zi def find_ci(xi,Z):
# Search forxi Nearest central elementci, bringZ[ci] Andxi The inner product of the vector difference between global cnt dis_= np.inf len_=len(Z)
rst_index =None for i in range(len_): cnt+=1
tmp_dist=np.dot(xi-Z[i],np.transpose(xi-Z[i]))if tmp_dist<dis_: rst_index=i
dis_=tmp_distreturn rst_index def k_mean(X,k): G=[]
#G[i]={1,2,3...} Indicates belonging to thei The sample of class is inX Index in, Wash label Z=[] #Z[i] The firsti Center point of class N=len(X) c=[]
#c[i]=1,2,...k; Express the firsti Samples belong toc[i] class tmpr=set() while len(Z)<k: r=random.randint(0
,len(X)-1) if r not in tmpr: tmpr.add(r) Z.append(X[r]) G.append(set()) for i in
range(N): c.append(0) # Random generationK Central elements while True: group_flag=np.zeros(k) for i in
range(N): new_ci = find_ci(X[i],Z)if c[i] != new_ci:
# Found a better one, holdxi From the originalc[i] Transferred tonew_ci go, So there are two groups that need to be updated：new_ci,c[i] if i in G[c[i]]:
G[c[i]].remove(i) group_flag[c[i]]=1 # holdi Remove from the original group G[new_ci].add(i)
group_flag[new_ci]=1 # holdi Join the new group c[i]=new_ci # All elements have been updated. if
np.sum(group_flag)==0: # No group was modified break for i in range(k): if group_flag[i]==0:
# not changed, No recalculation required continue else: Z[i]=calculate_zi(list(G[i]),X) return Z,c,k def
test_rgb_img(): filename=r"1.jpg" im = Image.open(filename) img = im.load()
im.close() height = im.size[0] width= im.size[1] print(im.size) X=[] for i in
range(0,height): for j in range(0,width): X.append(np.array(img[i,j]))
Z,c,k=k_mean(X,8) #print(Z) new_im = Image.new("RGB",(height,width)) for i in
range(0,height): for j in range(0,width): index = i * width + j pix =
list(Z[c[index]])for k in range(len(pix)): pix[k]=int(pix[k])
new_im.putpixel((i,j),tuple(pix)) new_im.show()if __name__ == '__main__':
test_rgb_img() print(cnt)
Original graph:

k=8 Clustering results of：

k=4 Clustering results of：

k=2： Clustering results

<https://github.com/jmhIcoding/ml.git>

30天阅读排行