-
Notifications
You must be signed in to change notification settings - Fork 0
/
vq.py
80 lines (71 loc) · 1.93 KB
/
vq.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
import numpy as np
from kmeans import Kmeans as km
import matplotlib.pyplot as plt
def loadimages():
import matplotlib.pyplot as plt
import os
import fnmatch
"""
Load all the gray scale images in all the subdirectories with suffix `png`.
The images are flattened and each image is represented as an (d,) array.
Return
------
images : (d,n,i) ndarray
returns n, d-dimensional images. with i dimensional color data
"""
matches = []
for root, dirs, files in os.walk("./data"):
for filename in fnmatch.filter(files, '*.png'):
matches.append(os.path.join(root, filename))
m = np.shape(matches)
data = []
for m in matches:
data.append(plt.imread(m))
return data
def clustercolors(X, labels, means):
"""
Put colors into the means given
Parameters
----------
X : (n,d) ndarray
labels : (d,) ndarray
means : (n,k) ndarray
Return
------
clustereddata (d,n) ndarray with colored by means data
"""
n,d = np.shape(X)
nm,k = np.shape(means)
clustereddata = np.zeros((n,d))
# print clustereddata[:,labels == 0].shape
print means[:,0]
for temp in range(k):
# clustereddata[:,labels == temp] = means[:,temp].flatten()
print np.where(labels == temp)
ind = np.where(labels == temp)
for temp2 in range(n):
np.put(clustereddata[temp2,:],ind,means[temp2,temp])
return clustereddata
# import picture
X = loadimages()
# picture size: 400 x 267 pixels rgb colours
#print np.shape(X)
plt.imshow(X[0])
rgb = 3
# plt.show()
#print np.shape(X[0].flatten())
# put image through kmeans classification
height, width, three = np.shape(X[0])
data = X[0].reshape(width*height,rgb).T
np.shape(data)
kmobj = []
plt.show()
for k in range(2,12):
kmobj.append(km(data,k))
for k in range(10):
clusters = kmobj[k].get_means
labels = kmobj[k].label
newdat = clustercolors(data, labels, clusters)
plt.figure()
plt.imshow(newdat.T.reshape(height,width,rgb))
plt.show()