/
Asymmetry.py
309 lines (301 loc) · 11.5 KB
/
Asymmetry.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import cv2
import numpy as np
import pandas as pd
import imutils
from scipy import ndimage
from Preprocess import Preprocess
from Data import Data
from Caracteristics import Caracteristics
from Contours import Contours
import math
from matplotlib import pyplot as plt
class Asymmetry:
'''
all Asymmetry methods
'''
@staticmethod
def asymmetryByBestFitEllipse(img, contour):
'''
get asymmetry by best fitted ellipse
'''
# remove artifact
img = Preprocess.removeArtifactYUV(img)
# convert img to gray
imgray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# get best fitted ellipse
ellipse = cv2.fitEllipse(contour)
# blank img
blankImg = np.zeros(np.shape(imgray))
# draw ellipse on blank img
cv2.ellipse(blankImg, ellipse, (255, 255, 255), -1)
# get ellipse area
ellipseArea = np.sum(blankImg != 0)
# lesion area
lesionArea = cv2.contourArea(contour)
# diff between lesion and ellipse area
delta = abs(ellipseArea - lesionArea)
asymmetry = (delta / ellipseArea) * 100
asymmetry = round(asymmetry, 2)
return asymmetry
@staticmethod
def asymmetryByDistanceByCircle(img, contour):
'''
distance Between the center of gravity of contour and center of circle around the contour
'''
# get moment of contour
M = cv2.moments(contour)
# get center of gravity of contour
xe = int(M["m10"] / M["m00"])
ye = int(M["m01"] / M["m00"])
# get center of circle around the contour
cv2.circle(img, (xe, ye), radius=2, color=(0, 255, 255), thickness=1)
(xCiCe, yCiCe), radius = cv2.minEnclosingCircle(contour)
xCiCe = int(xCiCe)
yCiCe = int(yCiCe)
cv2.circle(img, (xCiCe, yCiCe), radius=2, color=(0, 0, 255), thickness=1)
asm = 100 - (Caracteristics.DistanceEuclidean(xe, ye, xCiCe, yCiCe) * 100 / radius)
asm = round(asm, 2)
return asm
@staticmethod
def asymmetryIndex(img, contour):
'''
get asymmetry index
search for homologue of each pixel
'''
# remove artifact
img = Preprocess.removeArtifactYUV(img)
# convert img to gray
imgray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
# get bounding rect
x, y, w, h = cv2.boundingRect(contour)
# crop the rect
rect = imgray[y:y + h, x:x + w]
# rotate 180°
rotated = imutils.rotate_bound(rect, 180)
# intersection between rect and rotated (search)
intersection = cv2.bitwise_and(rect, rotated)
imgray[y:y + h, x:x + w] = intersection
# get area of intersection (black means no homologues found)
intersectionArea = np.sum(intersection != 0)
noHomologueArea = np.sum(intersection == 0)
# lesion area
lesionArea = cv2.contourArea(contour)
# asymmetry
asymmetry = (noHomologueArea / lesionArea) * 100
asymmetry = round(asymmetry, 2)
return asymmetry
@staticmethod
def asymmetryBySubRegion(img, contour):
'''
get asymmetry by dividing the lesion to 4 subregions
'''
# remove artifact
img = Preprocess.removeArtifactYUV(img)
# convert img to gray
img = Caracteristics.extractLesion(img, contour)
imgray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
# binarize the img
# imgray[imgray > 0] = 255
# find best fit ellipse
(_, _), (_, _), angle = cv2.fitEllipse(contour)
# get bounding rect
x, y, w, h = cv2.boundingRect(contour)
padding = 0
# crop the rect
rect = imgray[y - padding:y + h + padding, x - padding:x + w + padding]
# rotate the lesion according to its best fit ellipse
rect = ndimage.rotate(rect, angle, reshape=True)
# flip H, flip V, flip VH
rectH = cv2.flip(rect, 0)
rectV = cv2.flip(rect, 1)
rectVH = cv2.flip(rect, -1)
# lesion area
lesionArea = cv2.contourArea(contour)
# intersect rect and rectH
intersection1 = cv2.bitwise_and(rect, rectH)
intersectionArea1 = np.sum(intersection1 != 0)
result1 = (intersectionArea1 / lesionArea) * 100
# intersect rect and rectV
intersection2 = cv2.bitwise_and(rect, rectV)
intersectionArea2 = np.sum(intersection2 != 0)
result2 = (intersectionArea2 / lesionArea) * 100
# intersect rect and rectVH
intersection3 = cv2.bitwise_and(rect, rectVH)
intersectionArea3 = np.sum(intersection3 != 0)
result3 = (intersectionArea3 / lesionArea) * 100
res = [result1, result2, result3]
asymmetry = max(res)
asymmetry = 100 - asymmetry
asymmetry = round(asymmetry, 2)
return asymmetry
@staticmethod
def asymmetryBySubRegionCentered(img, contour):
'''
get asymmetry by dividing the lesion to 4 subregions
but the lesion is placed in the center of img
'''
# remove artifact
img = Preprocess.removeArtifactYUV(img)
# convert img to gray
img = Caracteristics.extractLesion(img, contour)
imgray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
# binarize the img
# imgray[imgray > 0] = 255
# find best fit ellipse
(_, _), (_, _), angle = cv2.fitEllipse(contour)
# get bounding rect
x, y, w, h = cv2.boundingRect(contour)
# get moments of contour
M = cv2.moments(contour)
# center of gravity of the lesion
xe = int(M["m10"] / M["m00"])
ye = int(M["m01"] / M["m00"])
# get the centered rect
cx = x + w//2
cy = y + h//2
deltaX1 = abs(int(xe - cx))
deltaY1 = abs(int(ye - cy))
x1 = x + deltaX1
w1 = w + deltaX1
y1 = y + deltaY1
h1 = h + deltaY1
padding = 0
# crop the rect
rect = imgray[y1 - padding:y1 + h1 + padding, x1 - padding:x1 + w1 + padding]
# rotate the lesion according to its best fit ellipse
rect = ndimage.rotate(rect, angle, reshape=False)
# flip H, flip V, flip VH
rectH = cv2.flip(rect, 0)
rectV = cv2.flip(rect, 1)
rectVH = cv2.flip(rect, -1)
# lesion area
lesionArea = cv2.contourArea(contour)
# intersect rect and rectH
intersection1 = cv2.bitwise_and(rect, rectH)
intersectionArea1 = np.sum(intersection1 != 0)
result1 = (intersectionArea1 / lesionArea) * 100
# intersect rect and rectV
intersection2 = cv2.bitwise_and(rect, rectV)
intersectionArea2 = np.sum(intersection2 != 0)
result2 = (intersectionArea2 / lesionArea) * 100
# intersect rect and rectVH
intersection3 = cv2.bitwise_and(rect, rectVH)
intersectionArea3 = np.sum(intersection3 != 0)
result3 = (intersectionArea3 / lesionArea) * 100
res = [result1, result2, result3]
asymmetry = max(res)
asymmetry = 100 - asymmetry
asymmetry = round(asymmetry, 2)
return asymmetry
@staticmethod
def asymmetryBySubRegionCentered2(img, contour):
'''
get asymmetry by dividing the lesion to 4 subregions
but the lesion is placed in the center of img
'''
# remove artifact
img = Preprocess.removeArtifactYUV(img)
# convert img to gray
img = Caracteristics.extractLesion(img, contour)
imgray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
# binarize the img
# imgray[imgray > 0] = 255
# find best fit ellipse
(_, _), (_, _), angle = cv2.fitEllipse(contour)
# get bounding rect
x, y, w, h = cv2.boundingRect(contour)
# get moments of contour
M = cv2.moments(contour)
# center of gravity of the lesion
xe = int(M["m10"] / M["m00"])
ye = int(M["m01"] / M["m00"])
# get the centered rect
deltaX1 = abs(int(xe - x))
deltaX2 = abs(int(xe - (x + w)))
deltaY1 = abs(int(ye - y))
deltaY2 = abs(int(ye - (y + h)))
if deltaX1 < deltaX2:
x1 = int(xe - deltaX2)
w1 = int(deltaX2 * 2)
else:
x1 = int(xe - deltaX1)
w1 = int(deltaX1 * 2)
if deltaY1 < deltaY2:
y1 = int(ye - deltaY2)
h1 = int(deltaY2 * 2)
else:
y1 = int(ye - deltaY1)
h1 = int(deltaY1 * 2)
padding = 0
# crop the rect
rect = imgray[y1 - padding:y1 + h1 + padding, x1 - padding:x1 + w1 + padding]
# rotate the lesion according to its best fit ellipse
rect = ndimage.rotate(rect, angle, reshape=True)
# flip H, flip V, flip VH
rectH = cv2.flip(rect, 0)
rectV = cv2.flip(rect, 1)
rectVH = cv2.flip(rect, -1)
# lesion area
lesionArea = cv2.contourArea(contour)
# intersect rect and rectH
intersection1 = cv2.bitwise_and(rect, rectH)
intersectionArea1 = np.sum(intersection1 != 0)
result1 = (intersectionArea1 / lesionArea) * 100
# intersect rect and rectV
intersection2 = cv2.bitwise_and(rect, rectV)
intersectionArea2 = np.sum(intersection2 != 0)
result2 = (intersectionArea2 / lesionArea) * 100
# intersect rect and rectVH
intersection3 = cv2.bitwise_and(rect, rectVH)
intersectionArea3 = np.sum(intersection3 != 0)
result3 = (intersectionArea3 / lesionArea) * 100
res = [result1, result2, result3]
asymmetry = max(res)
asymmetry = 100 - asymmetry
asymmetry = round(asymmetry, 2)
return asymmetry
if __name__ == '__main__' :
'''
test program
'''
TYPE = 'Melanoma'
# TYPE = 'Nevus'
# BDD = 'ISIC'
BDD = 'PH2'
BDD_LOCATION = 'D:/HAKIM/MIV M2/PFE/fichiers prof/MIV 96-2019/Application MIV 96-2019/Code/BDD/'
DATA = BDD_LOCATION+BDD+'/'+TYPE+'/'
files = Data.loadFilesAsArray(DATA)
t = []
i = 0
for file in files:
sImg = DATA+file
img = cv2.imread(sImg,cv2.IMREAD_COLOR)
# get contours
contour = Contours.contours2(img)
# draw contours
cv2.drawContours(img, contour, -1, (0, 255, 255), 1)
# get boundings
Contours.boundingRectangle(img,contour)
# get asymmetry
# asymmetryByBestFitEllipse = Asymmetry.asymmetryByBestFitEllipse(img,contour)
# asymmetryIndex = Asymmetry.asymmetryIndex(img,contour)
# asymmetryBySubRegion = Asymmetry.asymmetryBySubRegion(img,contour)
asymmetryBySubRegionCentered2 = Asymmetry.asymmetryBySubRegionCentered2(img,contour)
print('['+str(i)+'/'+str(len(files))+']',asymmetryBySubRegionCentered2)
t.append(asymmetryBySubRegionCentered2)
i += 1
# draw text
x, y = 0, np.shape(img)[0]-3
cv2.putText(img,'asymmetry : '+str(asymmetryBySubRegionCentered2), (x,y), cv2.FONT_HERSHEY_SIMPLEX, fontScale = 0.5, color = (0, 0, 0), lineType = 1)
cv2.imshow('img',img)
if cv2.waitKey() == ord('c'):
break
# t = pd.DataFrame(t)
# print('---------------------------------------------------------------------------------')
# print('TYPE = ',TYPE)
# print('BDD = ',BDD)
# print('max = ',t.max(), ', min = ', t.min(), ', mean = ', t.mean(), ', median = ', t.median())
# print(t)
# # t = t.values
# print('---------------------------------------------------------------------------------')
cv2.destroyAllWindows()