-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
246 lines (193 loc) · 7.23 KB
/
main.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
240
241
242
243
244
from SkinDetector import SkinDetector
import matplotlib.pyplot as plt
from skimage import color
import numpy as np
from PIL import Image
# This function segments the validation dataset.
def segmentation_experiments():
sd = SkinDetector(hist_range = (3,97), dilate = 2)
sd.train()
segmented = sd.segment_dataset(sd.VD_DATA)
# Uncomment this to visualize the segmented images.
"""
for idx in range(0, len(segmented)):
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(sd.VD_DATA[idx])
ax[1].imshow(segmented[idx], cmap = plt.get_cmap('gray'))
plt.show()
"""
sd.validate(segmented, sd.VD_MASK)
# Uncomment this to obtain the median of the segmentation.
"""
print('\nMedian')
print(f'{np.round(np.median(sd.accuracy), 3) * 100}%')
print(f'{np.round(np.median(sd.precision), 3) * 100}%')
print(f'{np.round(np.median(sd.recall), 3) * 100}%')
print(f'{np.round(np.median(sd.F1score), 3) * 100}%')
"""
# Uncomment this to obtain the mean of the segmentation.
print('\nMean')
print(f'{np.round(np.mean(sd.accuracy) * 100, 3)}%')
print(f'{np.round(np.mean(sd.precision) * 100, 3)}%')
print(f'{np.round(np.mean(sd.recall) * 100, 3)}%')
print(f'{np.round(np.mean(sd.F1score) * 100, 3)}%')
print(f'{np.std(sd.accuracy)}%')
print(f'{np.std(sd.precision)}%')
print(f'{np.std(sd.recall)}%')
print(f'{np.std(sd.F1score)}%')
print('\n')
# This function segments an image you want.
# Note: change the path of the image.
def segment_your_image():
sd = SkinDetector(hist_range=(3, 97), dilate=2)
sd.train()
basewidth = 300
img = Image.open(f'../../../../Desktop/mano5.jpg')
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
segmented = sd.segment(np.asarray(img), plot=True)
# This function makes all the assessment to find the optimal range
# yields the plot from the paper
# and stores the data in a dictionary in file data.txt.
def assessment_paper():
TRmedianAcc = []
TRmedianPre = []
TRmedianRec = []
TRmedianF1 = []
TRmeanAcc = []
TRmeanPre = []
TRmeanRec = []
TRmeanF1 = []
TRstdAcc = []
TRstdPre = []
TRstdRec = []
TRstdF1 = []
VDmedianAcc = []
VDmedianPre = []
VDmedianRec = []
VDmedianF1 = []
VDmeanAcc = []
VDmeanPre = []
VDmeanRec = []
VDmeanF1 = []
VDstdAcc = []
VDstdPre = []
VDstdRec = []
VDstdF1 = []
for hist_range in zip(np.arange(0, 10, 0.1), 100 -np.arange(0, 10, 0.1)):
sd = SkinDetector(hist_range = hist_range, dilate = 0)
sd.train()
print(f"Trained {sd}!")
segmentedTrain = sd.segment_dataset(sd.TR_DATA)
sd.validate(segmentedTrain, sd.TR_MASK)
TRmedianAcc.append(np.median(sd.accuracy))
TRmedianPre.append(np.median(sd.precision))
TRmedianRec.append(np.median(sd.recall))
TRmedianF1.append(np.median(sd.F1score))
TRmeanAcc.append(np.mean(sd.accuracy))
TRmeanPre.append(np.mean(sd.precision))
TRmeanRec.append(np.mean(sd.recall))
TRmeanF1.append(np.mean(sd.F1score))
TRstdAcc.append(np.std(sd.accuracy))
TRstdPre.append(np.std(sd.precision))
TRstdRec.append(np.std(sd.recall))
TRstdF1.append(np.std(sd.F1score))
segmentedValid = sd.segment_dataset(sd.VD_DATA)
sd.validate(segmentedValid, sd.VD_MASK)
VDmedianAcc.append(np.median(sd.accuracy))
VDmedianPre.append(np.median(sd.precision))
VDmedianRec.append(np.median(sd.recall))
VDmedianF1.append(np.median(sd.F1score))
VDmeanAcc.append(np.mean(sd.accuracy))
VDmeanPre.append(np.mean(sd.precision))
VDmeanRec.append(np.mean(sd.recall))
VDmeanF1.append(np.mean(sd.F1score))
VDstdAcc.append(np.std(sd.accuracy))
VDstdPre.append(np.std(sd.precision))
VDstdRec.append(np.std(sd.recall))
VDstdF1.append(np.std(sd.F1score))
data = {
'TR': {
'median': {
'accuracy': TRmedianAcc,
'precision': TRmedianPre,
'recall': TRmedianRec,
'F1': TRmedianF1
},
'mean': {
'accuracy': TRmeanAcc,
'precision': TRmeanPre,
'recall': TRmeanRec,
'F1': TRmeanF1
},
'std': {
'accuracy': TRstdAcc,
'precision': TRstdPre,
'recall': TRstdRec,
'F1': TRstdF1
}
},
'VD': {
'median': {
'accuracy': VDmedianAcc,
'precision': VDmedianPre,
'recall': VDmedianRec,
'F1': VDmedianF1
},
'mean': {
'accuracy': VDmeanAcc,
'precision': VDmeanPre,
'recall': VDmeanRec,
'F1': VDmeanF1
},
'std': {
'accuracy': VDstdAcc,
'precision': VDstdPre,
'recall': VDstdRec,
'F1': VDstdF1
}
}
}
with open('data.txt', 'w') as f:
f.write(str(data))
# Experiments with reconstruction operators
def reconstruction_experiments():
from skimage.morphology import reconstruction
from skimage import exposure
sd = SkinDetector(hist_range=(3, 97), dilate=0)
sd.train()
basewidth = 300
img = Image.open(f'../../../../Desktop/caras.jpg')
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = np.asarray(img.resize((basewidth,hsize), Image.ANTIALIAS))
from skimage import data
img = data.astronaut()
segmented = sd.segment(np.asarray(img), plot=True)
better_contrast = exposure.equalize_adapthist(color.rgb2gray(img), clip_limit=0.001)
fig, ax = plt.subplots(1, 2, figsize=(10,5))
ax[0].imshow(color.rgb2gray(img), cmap = plt.get_cmap('gray'))
ax[1].imshow(better_contrast, cmap = plt.get_cmap('gray'))
plt.show()
maxval = np.max(color.rgb2gray(better_contrast))
minval = np.min(color.rgb2gray(better_contrast))
#print("Max=",maxval,", Min=",minval)
marker = np.copy(better_contrast)
# Intensity of seed image must be greater than that of the mask image for reconstruction by erosion.
print("Before ", len(np.where(marker <= color.rgb2gray(better_contrast)[0])))
marker[marker <= minval] = maxval
print("After ", len(np.where(marker <= color.rgb2gray(better_contrast)[0])))
fig, ax = plt.subplots(1, 2, figsize=(10,5))
ax[0].imshow(segmented, cmap = plt.get_cmap('gray'))
ax[1].imshow(marker, cmap = plt.get_cmap('gray'))
plt.show()
print(len(np.where(marker > color.rgb2gray(np.asarray(img)))[0]))
rec = reconstruction(marker, better_contrast, method='erosion')
fig, ax = plt.subplots(1, 3, figsize=(10,5))
ax[0].imshow(segmented, cmap = plt.get_cmap('gray'))
ax[1].imshow(rec, cmap = plt.get_cmap('gray'))
ax[2].imshow(better_contrast, cmap = plt.get_cmap('gray'))
plt.show()
if __name__ == "__main__":
segmentation_experiments()