-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans_hw3.py
239 lines (197 loc) · 7.45 KB
/
kmeans_hw3.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from __future__ import division
import warnings
warnings.simplefilter(action = "ignore", category = FutureWarning)
import numpy as np
import scipy as sp
from matplotlib import image
import matplotlib.pyplot as plt
import matplotlib.cm as cm
"""Helper image-processing code."""
def image_to_matrix(image_file, grays=False):
"""
Convert .png image to matrix
of values.
params:
image_file = str
grays = Boolean
returns:
img = (color) np.ndarray[np.ndarray[np.ndarray[float]]]
or (grayscale) np.ndarray[np.ndarray[float]]
"""
img = image.imread(image_file)
# in case of transparency values
if(len(img.shape) == 3 and img.shape[2] > 3):
height, width, depth = img.shape
new_img = np.zeros([height, width, 3])
for r in range(height):
for c in range(width):
new_img[r,c,:] = img[r,c,0:3]
img = np.copy(new_img)
if(grays and len(img.shape) == 3):
height, width = img.shape[0:2]
new_img = np.zeros([height, width])
for r in range(height):
for c in range(width):
new_img[r,c] = img[r,c,0]
img = new_img
# clean up zeros
if(len(img.shape) == 2):
zeros = np.where(img == 0)[0]
img[zeros] += 1e-7
return img
def matrix_to_image(image_matrix, image_file):
"""
Convert matrix of color/grayscale
values to .png image
and save to file.
params:
image_matrix = (color) numpy.ndarray[numpy.ndarray[numpy.ndarray[float]]] or (grayscale) numpy.ndarray[numpy.ndarray[float]]
image_file = str
"""
# provide cmap to grayscale images
cMap = None
if(len(image_matrix.shape) < 3):
cMap = cm.Greys_r
image.imsave(image_file, image_matrix, cmap=cMap)
#image.imsave(image_file, image_matrix, cmap=cm.Greys_r)
def image_width(image_matrix):
if(len(image_matrix.shape) == 3):
height, width, depth = image_matrix.shape
else:
height, width = image_matrix.shape
return width
def flatten_image_matrix(image_matrix):
"""
Flatten image matrix from
Height by Width by Depth
to (Height*Width) by Depth
matrix.
params:
image_matrix = (color) numpy.ndarray[numpy.ndarray[numpy.ndarray[float]]] or (grayscale) numpy.ndarray[numpy.ndarray[float]]
returns:
flattened_values = (color) numpy.ndarray[numpy.ndarray[float]] or (grayscale) numpy.ndarray[float]
"""
if(len(image_matrix.shape) == 3):
height, width, depth = image_matrix.shape
else:
height, width = image_matrix.shape
depth = 1
flattened_values = np.zeros([height*width,depth])
for i, r in enumerate(image_matrix):
for j, c in enumerate(r):
flattened_values[i*width+j,:] = c
return flattened_values
def unflatten_image_matrix(image_matrix, width):
"""
Unflatten image matrix from
(Height*Width) by Depth to
Height by Width by Depth matrix.
params:
image_matrix = (color) numpy.ndarray[numpy.ndarray[float]] or (grayscale) numpy.ndarray[float]
width = int
returns:
unflattened_values = (color) numpy.ndarray[numpy.ndarray[numpy.ndarray[float]]] or (grayscale) numpy.ndarray[numpy.ndarray[float]]
"""
heightWidth = image_matrix.shape[0]
height = int(heightWidth / width)
if(len(image_matrix.shape) > 1):
depth = image_matrix.shape[-1]
unflattened_values = np.zeros([height, width, depth])
for i in range(height):
for j in range(width):
unflattened_values[i,j,:] = image_matrix[i*width+j,:]
else:
depth = 1
unflattened_values = np.zeros([height, width])
for i in range(height):
for j in range(width):
unflattened_values[i,j] = image_matrix[i*width+j]
return unflattened_values
def image_difference(image_values_1, image_values_2):
"""
Calculate the total difference
in values between two images.
Assumes that both images have same
shape.
params:
image_values_1 = (color) numpy.ndarray[numpy.ndarray[numpy.ndarray[float]]] or (grayscale) numpy.ndarray[numpy.ndarray[float]]
image_values_2 = (color) numpy.ndarray[numpy.ndarray[numpy.ndarray[float]]] or (grayscale) numpy.ndarray[numpy.ndarray[float]]
returns:
dist = int
"""
flat_vals_1 = flatten_image_matrix(image_values_1)
flat_vals_2 = flatten_image_matrix(image_values_2)
N, depth = flat_vals_1.shape
dist = 0.
point_thresh = 0.005
for i in range(N):
if(depth > 1):
new_dist = sum(abs(flat_vals_1[i] - flat_vals_2[i]))
if(new_dist > depth * point_thresh):
dist += new_dist
else:
new_dist = abs(flat_vals_1[i] - flat_vals_2[i])
if(new_dist > point_thresh):
dist += new_dist
return dist
from random import randint
from math import sqrt
from functools import reduce
def k_means_cluster(image_values, k=3):
"""
Separate the provided RGB values into
k separate clusters using the k-means algorithm,
then return an updated version of the image
with the original values replaced with
the corresponding cluster values.
params:
image_values = numpy.ndarray[numpy.ndarray[numpy.ndarray[float]]]
k = int
initial_means = numpy.ndarray[numpy.ndarray[float]] or None
returns:
updated_image_values = numpy.ndarray[numpy.ndarray[numpy.ndarray[float]]]
"""
def distance_point_mean(pix,clustMean):
d = sqrt( (pix[0]-clustMean[0])*(pix[0]-clustMean[0]) + (pix[1]-clustMean[1])*(pix[1]-clustMean[1]) + (pix[2]-clustMean[2])*(pix[2]-clustMean[2]) )
return d
# Flatten the image matrix
imHeight,imWidth,imDep = np.shape(image_values)
flat_image = flatten_image_matrix(image_values)
numPix, rgbdim = np.shape(flat_image)
updated_image_values = np.copy(flat_image)
#####################################################
# Select initial cluster centers
# Note 1: you can modify the function to be given
# the initial means as an input.
# Note 2: I've included a line of code that sets all
# of the initial means to zero. I don't
# suggest you keep it that way.
#####################################################
initial_means = np.zeros((k,3))
#####################################################
# Implement k-means
#####################################################
# Unflatten the image matrix with the updated values. Return the unflattened matrix.
updated_image_values_sqr = unflatten_image_matrix(updated_image_values,imWidth)
return updated_image_values_sqr
def k_means_test():
"""
Testing your implementation
of k-means on the segmented
reference images.
"""
# Identify the location of the input image.
# Load the image in as a matrix.
image_dir = 'images/'
image_name = 'lily.png'
image_values = image_to_matrix(image_dir + image_name)
# Define how many (k) clusters you want.
# For debugging, I suggest you use k == 2.
#k = 2
k = 3
# Call k-means.
updated_values = k_means_cluster(image_values, k)
# Convert the output pixels from matrix to image. Save the output image.
ref_image = image_dir + 'k%d_%s'%(k, image_name)
matrix_to_image(updated_values, ref_image)
k_means_test()