-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCM.py
89 lines (66 loc) · 1.91 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
import numpy as np
import cv2
import matplotlib.pyplot as plt
def init_mem_mat(nb_pixels, nb_clusters):
mem_mat = np.zeros((nb_pixels, nb_clusters))
x = np.arange(nb_pixels)
for j in range(nb_clusters):
xj = x % nb_clusters == j
mem_mat[xj, j] = 1
return mem_mat
def compute_centers(img_mat, mem_mat, fuzzy):
num = np.dot(img_mat, mem_mat ** fuzzy)
dem = np.sum(mem_mat ** fuzzy, axis=0)
return num / dem
def update_mem_mat(ctr_mat, img_mat, fuzzy):
ctr_mat_mesh, img_mat_mesh = np.meshgrid(ctr_mat, img_mat)
power = 2. / (fuzzy - 1)
p1 = abs(img_mat_mesh - ctr_mat_mesh) ** power
p2 = np.sum((1. / abs(img_mat_mesh - ctr_mat_mesh)) ** power, axis=1)
return 1. / (p1 * p2[:, None])
# Image to segment
#src = cv2.imread("./img/sky3.jpeg")
src = cv2.imread("./img/peppers.png")
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
plt.show()
# Number of clusters
K = 2
# Number of data/pixels
N = gray.size
# Fuzzyness coefficient
m = 2
# Threshold
eps = 0.3
# Maximum number of iterations
max_i = 100
# Initialization
X = gray.flatten().astype('float')
U = init_mem_mat(N, K)
# Repeat until convergence
i = 0
while True:
# Compute centroid for each cluster
C = compute_centers(X, U, m)
# Save initial membership matrix
old_U = np.copy(U)
# Update coefficients for each pixel
U = update_mem_mat(C, X, m)
# Difference between initial mem matrix and new one
d = np.sum(abs(U - old_U))
print(str(i) + " - d = " + str(d))
# Check convergence
if d < eps or i > max_i:
break
i += 1
# Segmentation
seg = np.argmax(U, axis=1)
seg = seg.reshape(gray.shape).astype('int')
# Plot
fig = plt.figure(figsize=(8, 4), dpi=100)
ax1 = fig.add_subplot(1, 2, 1)
ax1.imshow(cv2.cvtColor(src, cv2.COLOR_BGR2RGB))
ax1.set_title('original')
ax2 = fig.add_subplot(1, 2, 2)
ax2.imshow(seg, cmap="gray")
ax2.set_title('segmentation')
plt.show()