-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaskOverlapCorrectionFinal.py
More file actions
475 lines (322 loc) · 15.7 KB
/
Copy pathMaskOverlapCorrectionFinal.py
File metadata and controls
475 lines (322 loc) · 15.7 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# -*- coding: utf-8 -*-
"""
Created on Mon May 13 19:20:35 2024
@author: Elijah Gardi
"""
from matplotlib import pyplot as plt
import numpy as np
from scipy import ndimage
from numpy import ndarray
from scipy.ndimage import gaussian_filter
from skimage import measure
def contour_mask(mask, ax, color):
'''
Input:
mask: Binary mask 2D numpy array
ax: pyplot axes
'''
contours = measure.find_contours(mask, 0.5)
for n, contour in enumerate(contours):
ax.plot(contour[:, 1], contour[:, 0], linewidth=1.5, color = color, linestyle='dashed')
def create_circular_mask(h, w, center=None, radius=None):
if center is None: # use the middle of the image
center = (int(w/2), int(h/2))
if radius is None: # use the smallest distance between the center and image walls
radius = min(center[0], center[1], w-center[0], h-center[1])
Y, X = np.ogrid[:h, :w]
dist_from_center = np.sqrt((X - center[0])**2 + (Y-center[1])**2)
mask = dist_from_center <= radius
return mask
def spect_mask(organ_mask, expanded_mask, seconday_mask):
''' Mask that recovers the actual activity '''
return expanded_mask > 0.5
''' Gamma Camera blur '''
SIGMA = 15
''' Change distance and size '''
small_dist = 200
small_height = 60
small_radius = 30
'''L1 and L2 are lesion 1 and lesion 2. The G is to indicate filtering '''
L1 = create_circular_mask(500, 500, center=(200,200), radius=100)
L1_G = gaussian_filter(L1.astype('float'), sigma=SIGMA)
L2 = create_circular_mask(500, 500, center=(small_dist,small_height), radius=small_radius)
L2_G = gaussian_filter(L2.astype('float'), sigma=SIGMA)
mask_L1 = create_circular_mask(500, 500, center=(200,200), radius=110*1.25)
mask_L2 = create_circular_mask(500, 500, center=(small_dist,small_height), radius=small_radius*1.35)
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.set_figheight(8)
fig.set_figwidth(16)
"-------------------------------------------------------------------------------------"
"---------------------------------"
# Finding path between two cell in matrix
# Method for finding and printing
# whether the path exists or not
def isPath(matrix, n):
# Defining visited array to keep
# track of already visited indexes
visited = [[False for x in range(n)]
for y in range(n)]
# Flag to indicate whether the
# path exists or not
flag = False
for i in range(n):
for j in range(n):
# If matrix[i][j] is source
# and it is not visited
if (matrix[i][j] == 1 and not
visited[i][j]):
# Starting from i, j and
# then finding the path
if (checkPath(matrix, i,
j, visited)):
# If path exists
flag = True
break
if (flag):
return True
else:
return False
# Method for checking boundaries
def isSafe(i, j, matrix):
if (i >= 0 and i < len(matrix) and
j >= 0 and j < len(matrix[0])):
return True
return False
# Returns true if there is a
# path from a source(a
# cell with value 1) to a
# destination(a cell with
# value 2)
def checkPath(matrix, i, j,
visited):
# Checking the boundaries, walls and
# whether the cell is unvisited
if (isSafe(i, j, matrix) and
matrix[i][j] != 0 and not
visited[i][j]):
# Make the cell visited
visited[i][j] = True
# If the cell is the required
# destination then return true
if (matrix[i][j] == 2):
return True
# traverse up
up = checkPath(matrix, i - 1,
j, visited)
# If path is found in up
# direction return true
if (up):
return True
# Traverse left
left = checkPath(matrix, i,
j - 1, visited)
# If path is found in left
# direction return true
if (left):
return True
# Traverse down
down = checkPath(matrix, i + 1,
j, visited)
# If path is found in down
# direction return true
if (down):
return True
# Traverse right
right = checkPath(matrix, i,
j + 1, visited)
# If path is found in right
# direction return true
if (right):
return True
# No path has been found
return False
# This code is contributed by Chitranayal
"---------------------------------"
"Variable declerations"
Intersection = mask_L1 ^ mask_L2
Intersection = Intersection ^ (mask_L1 + mask_L2)
"Obtains coordinates of masks"
rows, cols = np.where(mask_L1)
maskL1coordinates = np.column_stack((cols, rows))
rows, cols = np.where(mask_L2)
maskL2coordinates = np.column_stack((cols, rows))
rows, cols = np.where(Intersection)
IntersectionCoords = np.column_stack((cols, rows))
"Intersection boundries for speed"
IntersectionCoordsXMax = max(IntersectionCoords[:,0])
IntersectionCoordsXMin = min(IntersectionCoords[:,0])
IntersectionCoordsYMax = max(IntersectionCoords[:,1])
IntersectionCoordsYMin = min(IntersectionCoords[:,1])
"Define image"
PET_Image = L1_G + L2_G
"Empty path matrix"
pathMask = np.zeros(PET_Image.shape, bool)
L2Intersection = np.zeros(PET_Image.shape, bool)
"Functions"
def StepMinus(matrix, stepsize):
for a in range (matrix.shape[0]):
for b in range (matrix.shape[1]): # loop through matrix
if matrix[a,b] > stepsize and not matrix[a,b] == 3 and not matrix[a,b] == 2 and not matrix[a,b] == 1:
matrix[a,b] = matrix[a,b] - stepsize
if matrix[a,b] <= stepsize:
matrix[a,b] = 0
return matrix
def FindMinima(image, stepsize):
imageMatrix = image * Intersection
PathMatrix = imageMatrix
"Start and end points"
x_start, y_start = min(IntersectionCoords, key=lambda x: (x[0], -x[1]))
x_end, y_end = max(IntersectionCoords, key=lambda x: (x[0], -x[1]))
while not isPath(PathMatrix, 500):
imageMatrix = StepMinus(imageMatrix, stepsize) # Subtract 'stepsize' from all pixels until there is a path
PathMatrix = ndarray.copy(imageMatrix) # Matrix for checking path
for a in range(PathMatrix.shape[0]):
for b in range(PathMatrix.shape[1]): # Loop through all pixels
if PathMatrix[a,b] == 0:
PathMatrix[a,b] = 3 # Path cell
else:
PathMatrix[a,b] = 0 # Blocked cell
"Start and end pixels"
PathMatrix[y_start, x_start] = 1 # 'Starting cell'
PathMatrix[y_end, x_end] = 2 # 'Destination cell'
PathMatrix = PathMatrix*Intersection # Reset out of intersection to blocked cells (0)
if isPath(PathMatrix, 500):
for a in range(PET_Image.shape[0]):
for b in range(PET_Image.shape[1]):
if imageMatrix[a,b] == 0:
pathMask[a,b] = True
else: pathMask[a,b] = False
return pathMask*Intersection
return "Error"
def GetCentroid(mask):
"Origin is the average of all points from mask"
x = [p[0] for p in mask]
y = [p[1] for p in mask]
centroid = (sum(x) / len(mask), sum(y) / len(mask))
return centroid
def Check_Direction(Centroid, c, d):
# Check for the LocalMinima points "Use local minima coordinate with the direction from the origin of the mask to determine left and right/top and down filling direction"
Direction_Vector = np.subtract(Centroid, np.array([c,d]))
Magnitude = np.linalg.norm(Direction_Vector)
Direction_Vector = Direction_Vector/Magnitude
return Direction_Vector
pathMask = FindMinima(PET_Image, 0.05)
# copyMask = ndarray.copy(pathMask)
# pathMask = ndarray.copy(copyMask)
plt.imshow(pathMask)
plt.imshow(L2Intersection)
"Calculate centroid of secondary mask and intersection for direction"
centroid = GetCentroid(maskL2coordinates)
intersecCentroid = GetCentroid(IntersectionCoords)
"Create Direction (unit) Vector"
Direction_Vector = Check_Direction(intersecCentroid, centroid[0], centroid[1])
"LEFT FILLING DIRECTION"
if Direction_Vector[0] < 0 and abs(Direction_Vector[1]) < 0.5: # Check unit vector is to left and between 0.5 vertically
"Average True values along x axis"
for d in range(IntersectionCoordsYMin, IntersectionCoordsYMax):
for k in range(IntersectionCoordsXMin, IntersectionCoordsXMax):
Sum = 0
if pathMask[d,k]:
for a in range(IntersectionCoordsXMin, IntersectionCoordsXMax):
if pathMask[d,a]:
Sum +=1
pathMask[d,a] = False
pathMask[d,k+int(Sum/2)] = True
for c in range(IntersectionCoordsXMin, IntersectionCoordsXMax):
for d in range(IntersectionCoordsYMin, IntersectionCoordsYMax):# Loop through intersection coordinates region
if pathMask[d,c]: # Check for the LocalMinima points
for a in range(IntersectionCoords.shape[0]): # Loop through every intersection coordinate
if IntersectionCoords[a,1] == d and IntersectionCoords[a,0] < c: # Check that the intersection coordinate is not greater than the LocalMinima coordinate
L2Intersection[IntersectionCoords[a,1], IntersectionCoords[a,0]] = True
# Set all points (Limited to intersection points and less than local minima) to the PET image.
# IntersectionCoords[a,1] == d leftIntersection matrix is updated only when the y coord is the same as the Localminia y coord
"RIGHT FILLING DIRECTION"
if Direction_Vector[0] > 0 and abs(Direction_Vector[1]) < 0.5: # Check unit vector is to right and between 0.5 vertically
"Average True values along x axis"
for d in range(IntersectionCoordsYMin, IntersectionCoordsYMax):
for k in range(IntersectionCoordsXMin, IntersectionCoordsXMax):
Sum = 0
if pathMask[d,k]:
for a in range(IntersectionCoordsXMin, IntersectionCoordsXMax):
if pathMask[d,a]:
Sum +=1
pathMask[d,a] = False
pathMask[d,k+int(Sum/2)] = True
for c in range(IntersectionCoordsXMin, IntersectionCoordsXMax):
for d in range(IntersectionCoordsYMin, IntersectionCoordsYMax): # Loop through intersection coordinates region
if pathMask[d,c]: # Check for the LocalMinima points
for a in range(IntersectionCoords.shape[0]): # Loop through every intersection coordinate
if IntersectionCoords[a,1] == d and IntersectionCoords[a,0] > c: # Check that the intersection coordinate is not greater than the LocalMinima coordinate
L2Intersection[IntersectionCoords[a,1], IntersectionCoords[a,0]] = True
# Set all points (Limited to intersection points and less than local minima) to the PET image.
# IntersectionCoords[a,1] == d leftIntersection matrix is updated only when the y coord is the same as the Localminia y coord
"TOP FILLING DIRECTION"
if (Direction_Vector[1] < 0 and abs(Direction_Vector[0]) < 0.5): # Check unit vector is pointing up and between 0.5 horizontally
"Average True values along y axis"
for k in range(IntersectionCoordsXMin, IntersectionCoordsXMax):
for d in range(IntersectionCoordsYMin, IntersectionCoordsYMax):
Sum = 0
if pathMask[d,k]:
for a in range(IntersectionCoordsYMin, IntersectionCoordsYMax):
if pathMask[a,k]:
Sum +=1
pathMask[a,k] = False
pathMask[d+int(Sum/2),k] = True
"Uses vertical local minima matrix to seperate the intersection"
for c in range(IntersectionCoordsXMin, IntersectionCoordsXMax):
for d in range(IntersectionCoordsYMin, IntersectionCoordsYMax):
# Loop through intersection coordinates region
if pathMask[d,c]:
# Check for the LocalMinima points
for a in range(IntersectionCoords.shape[0]):
# Loop through every intersection coordinate
if IntersectionCoords[a,0] == c and IntersectionCoords[a,1] < d: #check it is the first local minima with that x coord
L2Intersection[IntersectionCoords[a,1], IntersectionCoords[a,0]] = True
# Set all points (Limited to intersection points and less than local minima) to the PET image.
# IntersectionCoords[a,0] == c leftIntersection matrix is updated only when the x coord is the same as the Localminia x coord
"BOTTOM FILLING DIRECTION"
if Direction_Vector[1] > 0 and abs(Direction_Vector[0]) < 0.5: # Check unit vector is pointing up and between 0.5 horizontally
"Average True values along y axis"
for k in range(IntersectionCoordsXMin, IntersectionCoordsXMax):
for d in range(IntersectionCoordsYMin, IntersectionCoordsYMax):
Sum = 0
if pathMask[d,k]:
for a in range(IntersectionCoordsYMin, IntersectionCoordsYMax):
if pathMask[a,k]:
Sum +=1
pathMask[a,k] = False
pathMask[d+int(Sum/2),k] = True
"Uses vertical local minima matrix to seperate the intersection"
for c in range(IntersectionCoordsXMin, IntersectionCoordsXMax):
for d in range(IntersectionCoordsYMin, IntersectionCoordsYMax):
# Loop through intersection coordinates region
if pathMask[d,c]:
# Check for the LocalMinima points
for a in range(IntersectionCoords.shape[0]):
# Loop through every intersection coordinate
if IntersectionCoords[a,0] == c and IntersectionCoords[a,1] > d: #check it is the first local minima with that x coord
L2Intersection[IntersectionCoords[a,1], IntersectionCoords[a,0]] = True
# Set all points (Limited to intersection points and less than local minima) to the PET image.
# IntersectionCoords[a,0] == c leftIntersection matrix is updated only when the x coord is the same as the Localminia x coord
plt.imshow(mask_L1 + mask_L2)
plt.imshow(mask_L2)
mask_L2 = mask_L2 ^ L2Intersection
L1Intersection = Intersection ^ L2Intersection
mask_L1 = mask_L1 ^ L1Intersection
mask = mask_L1
mask_spect2 = mask_L2
"-------------------------------------------------------------------------------------"
ax1.imshow(L1+L2)
ax1.axis('off')
ax2.imshow(L1_G+L2_G, vmin=0,vmax=1)
ax2.axis('off')
contour_mask(mask, ax2, 'red')
contour_mask(mask, ax1, 'red')
contour_mask(mask_spect2, ax2, 'black')
masked_sum = np.sum(mask*(L1_G+L2_G))
actual_sum = np.sum(L1)
print('Large ROI masked vs actual counts: ', round(masked_sum/actual_sum, 2))
plt.title(round(masked_sum/actual_sum, 3))
masked_sum = np.sum(mask_spect2*(L1_G+L2_G))
actual_sum = np.sum(L2)
print('Small ROI masked vs actual counts: ', round(masked_sum/actual_sum, 2))