-
Notifications
You must be signed in to change notification settings - Fork 2
/
kmeans.py
89 lines (65 loc) · 1.71 KB
/
kmeans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from PIL import Image
import numpy as np
import pylab
#from PIL import Image
import random
import tifwork as tw
import scipy.misc
def initCentroids(Dataset,K,z):
init_centroid = np.zeros((K,z))
for i in range(0,K):
j = random.choice(Dataset)
init_centroid[i,:] = j
return init_centroid
def kmeans(Dataset,K,initial_centroid):
(nos,z) = Dataset.shape
idx = np.zeros((row*col),float)
dis = np.zeros(z,float)
for i in range(0,nos):
for j in range(0,K):
dis = (initial_centroid[j,:] - Dataset[i,:])**2
dist = 0
for k in range(0,z):
dist += dis[k]
if( j == 0):
minimum = dist
idx[i] = j
elif (dist < minimum):
minimum = dist
idx[i] = j
centroid = newclus(K,nos,Dataset,idx)
return (idx,centroid)
def newclus(K,nos,Dataset,idx):
centroid = np.zeros((K,z))
for i in range(0,K):
mean = [0,0,0]
count = 0
for j in range(0,nos):
if(idx[j] == i):
mean = mean+Dataset[j]
count+=1
mean = mean/count
centroid[i,:] = mean
return centroid
Dataset = np.array(Image.open('good.jpg'),float)
(row,col,z) = Dataset.shape
arr2 = np.zeros(Dataset.shape,float)
Dataset = Dataset/255
# no of clusters
K = 4
#no of iters
iters = 3
Data_orig = Dataset
Dataset=Dataset.reshape(((row*col),z))
#print Dataset.shape
initial_centroid = initCentroids(Dataset,K,z)
#print initial_centroid
for q in range(0,iters):
(idx,centroid)=kmeans(Dataset,K,initial_centroid)
print idx.max()
color = [(10,0,0),(0,25,0),(0,0,55),(10,10,10),(12,0,45),(34,65,0),(0,45,87),(100,100,100),(50,50,50),(12,54,77),(43,65,99)]
for i in range(0,(row*col)):
Dataset[i] = color[int(idx[i])]
Dataset=Dataset.reshape(row,col,z)
print Dataset.shape
scipy.misc.imsave('kmeans.jpeg',Dataset)