-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathFCM.py
163 lines (130 loc) · 5.57 KB
/
FCM.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
# =============================================================================
# Standard Fuzzy C-means algorithm
# (https://en.wikipedia.org/wiki/Fuzzy_clustering.)
# =============================================================================
import os
from os import listdir
from os.path import isfile, join
import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import convolve2d
from utils import makedirs
class FCM():
def __init__(self, image, image_bit, n_clusters, m, epsilon, max_iter):
'''Modified Fuzzy C-means clustering
<image>: 2D array, grey scale image.
<n_clusters>: int, number of clusters/segments to create.
<m>: float > 1, fuzziness parameter. A large <m> results in smaller
membership values and fuzzier clusters. Commonly set to 2.
<max_iter>: int, max number of iterations.
'''
#-------------------Check inputs-------------------
if np.ndim(image) != 2:
raise Exception("<image> needs to be 2D (gray scale image).")
if n_clusters <= 0 or n_clusters != int(n_clusters):
raise Exception("<n_clusters> needs to be positive integer.")
if m < 1:
raise Exception("<m> needs to be >= 1.")
if epsilon <= 0:
raise Exception("<epsilon> needs to be > 0")
self.image = image
self.image_bit = image_bit
self.n_clusters = n_clusters
self.m = m
self.epsilon = epsilon
self.max_iter = max_iter
self.shape = image.shape # image shape
self.X = image.flatten().astype('float') # flatted image shape: (number of pixels,1)
self.numPixels = image.size
#---------------------------------------------
def initial_U(self):
U=np.zeros((self.numPixels, self.n_clusters))
idx = np.arange(self.numPixels)
for ii in range(self.n_clusters):
idxii = idx%self.n_clusters==ii
U[idxii,ii] = 1
return U
def update_U(self):
'''Compute weights'''
c_mesh,idx_mesh = np.meshgrid(self.C,self.X)
power = 2./(self.m-1)
p1 = abs(idx_mesh-c_mesh)**power
p2 = np.sum((1./abs(idx_mesh-c_mesh))**power,axis=1)
return 1./(p1*p2[:,None])
def update_C(self):
'''Compute centroid of clusters'''
numerator = np.dot(self.X,self.U**self.m)
denominator = np.sum(self.U**self.m,axis=0)
return numerator/denominator
def form_clusters(self):
'''Iterative training'''
d = 100
self.U = self.initial_U()
if self.max_iter != -1:
i = 0
while True:
self.C = self.update_C()
old_u = np.copy(self.U)
self.U = self.update_U()
d = np.sum(abs(self.U - old_u))
print("Iteration %d : cost = %f" %(i, d))
if d < self.epsilon or i > self.max_iter:
break
i+=1
else:
i = 0
while d > self.epsilon:
self.C = self.update_C()
old_u = np.copy(self.U)
self.U = self.update_U()
d = np.sum(abs(self.U - old_u))
print("Iteration %d : cost = %f" %(i, d))
if d < self.epsilon or i > self.max_iter:
break
i+=1
self.segmentImage()
def deFuzzify(self):
return np.argmax(self.U, axis = 1)
def segmentImage(self):
'''Segment image based on max weights'''
result = self.deFuzzify()
self.result = result.reshape(self.shape).astype('int')
return self.result
def main(DIRECTORY, args):
IMG_PATH = DIRECTORY['IMG_PATH']
OUTPUT_PATH = DIRECTORY['OUTPUT_PATH']
OUTPUT_PLOT_PATH = os.path.join(OUTPUT_PATH,'segmentation') # path for output (plot directory)
IS_PLOT = args.plot_show
IS_SAVE = args.plot_save
files = [f for f in listdir(IMG_PATH) if isfile(join(IMG_PATH, f))] # read all files in IMG_PATH
for file in files:
target_img_path = os.path.join(IMG_PATH,file)
try:
#--------------Lord image file--------------
img= cv2.imread(target_img_path, cv2.IMREAD_GRAYSCALE) # cf. 8bit image-> 0~255
#--------------Clustering--------------
cluster = FCM(img, image_bit=args.num_bit, n_clusters=args.num_cluster, m=args.fuzziness, epsilon=args.epsilon, max_iter=args.max_iteration)
cluster.form_clusters()
result=cluster.result
#-------------------Plot and save result------------------------
if IS_PLOT:
fig=plt.figure(figsize=(12,8),dpi=100)
ax1=fig.add_subplot(1,2,1)
ax1.imshow(img,cmap='gray')
ax1.set_title('image')
ax2=fig.add_subplot(1,2,2)
ax2.imshow(result)
ax2.set_title('segmentation')
plt.show(block=False)
plt.close()
if IS_SAVE:
makedirs(OUTPUT_PLOT_PATH)
seg_result_path = os.path.join(OUTPUT_PLOT_PATH,"%s.png"%(os.path.splitext(file)[0]))
plt.imshow(result)
plt.savefig(seg_result_path, dpi=300)
plt.close()
except IOError:
print("Error")
if __name__ == '__main__':
main()