-
Notifications
You must be signed in to change notification settings - Fork 0
/
contour_extraction.py
368 lines (293 loc) · 12.3 KB
/
contour_extraction.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
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
import pandas as pd
from scipy.interpolate import splprep, splev
from matplotlib import pyplot as plt
import numpy as np
import cv2 as cv
import cv2
tiff2 = []
def apply_mask(img, mask):
np.clip(img, 0, 255, out=img)
img = img.astype('uint8')
myo_mask = np.where(mask == 255, 255, 0)
np.clip(myo_mask, 0, 255, out=myo_mask)
myo_mask = myo_mask.astype('uint8')
rv_mask = np.where(mask == 120, 255, 0)
np.clip(rv_mask, 0, 255, out=rv_mask)
rv_mask = rv_mask.astype('uint8')
lv_mask = np.where(mask == 60, 255, 0)
np.clip(lv_mask, 0, 255, out=lv_mask)
lv_mask = lv_mask.astype('uint8')
MY = cv.bitwise_and(img.copy(), img.copy(), mask=myo_mask)
RV = cv.bitwise_and(img.copy(), img.copy(), mask=rv_mask)
LV = cv.bitwise_and(img.copy(), img.copy(), mask=lv_mask)
full_mask = np.where(mask != 0, 255, 0)
np.clip(full_mask, 0, 255, out=full_mask)
full_mask = full_mask.astype('uint8')
full = cv.bitwise_and(img.copy(), img.copy(), mask=full_mask)
return LV, RV, MY, full
# Take input image and area, remove any objects smaller than the defined area
def undesired_objects(binary_map, area, check):
# do connected components processing
nlabels, labels, stats, centroids = cv.connectedComponentsWithStats(cv.bitwise_not(binary_map), None, None, None, 8,
cv.CV_32S)
# get CC_STAT_AREA component as stats[label, COLUMN]
areas = stats[1:, cv.CC_STAT_AREA]
result = np.zeros((labels.shape), np.uint8)
for i in range(0, nlabels - 1):
if areas[i] >= area: # keep
result[labels == i + 1] = 255
if areas[i] >= area3 and check == 2:
result[labels == i + 1] = 0
return cv.bitwise_not(result)
def reconstruction_full(img, mask, LV_mean, MY_mean):
mask_copy = mask.copy()
mask_copy2 = mask.copy()
# Make areas less than LV holes
mask_copy2[mask == 255] = 0
mask_copy2[np.around(np.around(mask_copy2 / 10) * 10) == 60] = 255
np.clip(mask_copy2, 0, 255, out=mask_copy2)
mask_copy2 = mask_copy2.astype('uint8')
nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(mask_copy2, None, None, None, 4, cv2.CV_32S)
areas = stats[1:, cv.CC_STAT_AREA]
shape_max_label = np.argmax(areas) + 1
for i in range(0, nlabels - 1):
if i + 1 != shape_max_label:
mask[labels == i + 1] = 0
# Fill holes
mask[mask != 0] = 255
np.clip(mask, 0, 255, out=mask)
mask = mask.astype('uint8')
mask = cv2.bitwise_not(mask)
holes = undesired_objects(mask, 100)
nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(holes, None, None, None, 4, cv2.CV_32S)
for i in range(0, nlabels - 1):
hole = np.zeros((labels.shape), np.uint8)
hole[labels == i + 1] = 255
hole_mean = np.mean(cv.bitwise_and(img.copy(), img.copy(), mask=hole))
# If hole color closer to LV
if np.abs(hole_mean - LV_mean) < np.abs(hole_mean - MY_mean):
mask_copy[labels == i + 1] = 60
else:
mask_copy[labels == i + 1] = 255
return mask_copy
def edge_detection(img):
# Blur the image for better edge detection
img_blur = cv2.GaussianBlur(img, (5, 5), 0)
# cv2_imshow(img_blur)
# Canny Edge Detection
edges = cv2.Canny(image=img_blur, threshold1=0, threshold2=0) # Canny Edge Detection
contours, h = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
big_contour = max(contours, key=cv2.contourArea)
M = cv2.moments(big_contour)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
result = np.zeros((img_blur.shape), np.uint8)
cv2.drawContours(result, contours, -1, (255, 255, 255), thickness=1)
# Display Canny Edge Detection Image
# cv2_imshow(result)
return result, [cX, cY]
def get_normal_vec(points):
p1 = np.array(points[0])
p2 = np.array(points[1])
p3 = np.array(points[2])
# These two vectors are in the plane
v1 = p3 - p1
v2 = p2 - p1
# The cross product is a vector normal to the plane
return np.cross(v1, v2)
def consecutive(data, stepsize=1):
return np.split(data, np.where(np.diff(data) != stepsize)[0] + 1)
def f7(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
from collections import OrderedDict
def extract_ring_path(indices, z_shape=False, spline=True):
ring_indices = [[], []]
x_idx_array = [[]]
count = 0
# Conpensate low resolution
for i in range(len(indices[0])):
x_array = indices[0]
y_array = indices[1]
x = x_array[i]
y = y_array[i]
if i in list(x_idx_array[0]):
continue
x_idx_array = np.where(x_array == x)
y_idx_array = np.where(y_array == y)
consecutive_y = consecutive(y_array[x_idx_array])
# If straight line
if len(consecutive_y) == 1:
ring_indices[0].extend(indices[0][x_idx_array])
ring_indices[1].extend(indices[1][x_idx_array])
# Seperate groupes alone y
if len(consecutive_y) >= 2:
ring_indices[0].extend(indices[0][x_idx_array])
for j in range(len(consecutive_y)):
group_len = len(consecutive_y[j])
ring_indices[1].extend([np.mean(consecutive_y[j])] * group_len)
ring_coords = list(zip(ring_indices[0], ring_indices[1]))
ring_coords = np.array(list(OrderedDict.fromkeys(ring_coords)))
ring_df = pd.DataFrame(data=ring_coords, columns=['x', 'y'])
unique_x_vals = ring_df["x"].unique()
ordered_ring_coords = []
mirror_coords = []
count = 0
for unique_x in unique_x_vals:
points_at_x = ring_df[ring_df["x"] == unique_x]
# display(points_at_x)
conseq_y_bool = points_at_x["y"].diff().eq(1).any()
if conseq_y_bool and count == 0:
ordered_ring_coords.extend(np.array(points_at_x))
else:
points_at_x_array = np.array(points_at_x)
ordered_ring_coords.extend(points_at_x_array[1:][::-1])
mirror_coords.append(points_at_x_array[0])
count += 1
mirror_coords = mirror_coords[::-1]
mirror_coords.append(ordered_ring_coords[0])
ordered_ring_coords.extend(mirror_coords)
# center = tuple(map(operator.truediv, reduce(lambda x, y: map(operator.add, x, y), ring_coords), [len(ring_coords)] * 2))
# ordered_ring_coords = sorted(ring_coords, key=lambda coord: (-135 - math.degrees(math.atan2(*tuple(map(operator.sub, coord, center))[::-1]))) % 360)
# ordered_ring_coords = np.array(ordered_ring_coords)
# plt.figure()
# plt.plot(indices[0], indices[1], "ro")
# plt.plot(indices[0], indices[1])
#
# plt.figure()
# plt.plot(ring_indices[0], ring_indices[1], "ro")
# plt.plot(ring_indices[0], ring_indices[1])
#
# plt.figure()
# plt.plot(*zip(*ordered_ring_coords), "ro")
# plt.plot(*zip(*ordered_ring_coords))
x_new = list(list(zip(*ordered_ring_coords))[0])
y_new = list(list(zip(*ordered_ring_coords))[1])
if spline:
ordered_ring_coords = np.array(ordered_ring_coords)
tck, u = splprep(ordered_ring_coords.T, u=None, s=0.0, per=1)
u_new = np.linspace(u.min(), u.max(), 200)
x_new, y_new = splev(u_new, tck, der=0)
if z_shape:
print(u_new)
# plt.figure()
# plt.plot(ordered_ring_coords[:, 0], ordered_ring_coords[:, 1], "ro")
# plt.plot(x_new, y_new, 'b-')
return x_new, y_new
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
# ax.set_xlabel('x')
# ax.set_ylabel('y')
# ax.set_zlabel('z')
def generate_xzy(indices, z_depth, center_diff_x=0, center_diff_y=0, scatter=True):
coordinates = np.array(
[indices[0] + center_diff_x, indices[1] + center_diff_y, np.array(np.shape(indices)[1] * [z_depth])])
# if scatter:
# ax.scatter(coordinates[0], coordinates[1], coordinates[2])
# else:
# ax.plot(coordinates[0], coordinates[1], coordinates[2], "r-")
coordinates_zipped = list(zip(coordinates[0], coordinates[1], coordinates[2]))
coordinates = np.array(
[indices[0], indices[1], np.array(np.shape(indices)[1] * [z_depth]), np.array(np.shape(indices)[1] * [255]),
np.array(np.shape(indices)[1] * [255]), np.array(np.shape(indices)[1] * [255])])
coordinates_zipped = list(
zip(coordinates[0], coordinates[1], coordinates[2], coordinates[3], coordinates[4], coordinates[5]))
return coordinates_zipped
def save_xzys(xyz_array, folder, filename):
xyz_array = [item for sublist in xyz_array for item in sublist]
xyz_array_np = []
xyz_array_np = np.array(xyz_array)
np.savetxt(folder + filename, xyz_array_np, delimiter=',')
def reconstruct_tip(edge, num_img, range=[0.9, 0.01]):
sizes = np.linspace(range[0], range[1], num=num_img)
l_dim = np.shape(edge)[0]
reconstructed_shapes = []
for size in sizes:
small = cv2.resize(edge, (0, 0), fx=size, fy=size)
s_dim = np.shape(small)[0]
x_offset = y_offset = round((l_dim - s_dim) / 2)
result = np.zeros((edge.shape), np.uint8)
result[y_offset:y_offset + small.shape[0], x_offset:x_offset + small.shape[1]] = small
# cv2_imshow(small)
# cv2_imshow(result)
reconstructed_shapes.append(result)
return reconstructed_shapes
def extract_contour(SA_LV_mask_ED, SA_img_ED, slice_locs_trimed, folder):
if len(SA_LV_mask_ED)>5:
SA_LV_mask_ED = SA_LV_mask_ED[1:-1]
SA_img_ED = SA_img_ED[1:-1]
slice_locs_trimed = slice_locs_trimed[1:-1]
xyz_array_myo = []
xyz_array_lv = []
SA_LV_mask_ED_copy = SA_LV_mask_ED.copy()
full_arr = []
z_depth1 = slice_locs_trimed[0]
z_depth2 = slice_locs_trimed[0]
print(slice_locs_trimed)
for i in range(len(SA_LV_mask_ED)):
# cv2_imshow(tiff_copy[i])
reconstructed = SA_LV_mask_ED_copy[i]
tiff2.append(reconstructed)
np.clip(reconstructed, 0, 255, out=reconstructed)
reconstructed = reconstructed.astype('uint8')
_, _, _, centroids = cv2.connectedComponentsWithStats(reconstructed, None, None, None, 4, cv2.CV_32S)
LV, RV, MY, full = apply_mask(SA_img_ED[i], reconstructed)
full_arr.append(full)
# cv2_imshow(reconstructed)
# cv2_imshow(full)
myo_solid = reconstructed.copy()
myo_solid[myo_solid != 0] = 255
edges, _ = edge_detection(myo_solid)
LV_solid = reconstructed.copy()
LV_solid[LV_solid == 255] = 0
LV_solid[LV_solid == 170] = 0
LV_solid[LV_solid != 0] = 255
edges2, _ = edge_detection(LV_solid)
indices = np.array(np.where(edges != [0]))
ring_x, ring_y = extract_ring_path(indices)
xyz_array_myo.append(generate_xzy([ring_x, ring_y], z_depth1, scatter=False))
try:
z_depth1 = slice_locs_trimed[i + 1]
except:
pass
indices = np.array(np.where(edges2 != [0]))
ring_x, ring_y = extract_ring_path(indices)
xyz_array_lv.append(generate_xzy([ring_x, ring_y], z_depth2, scatter=False))
try:
z_depth2 = slice_locs_trimed[i + 1]
except:
pass
save_xzys(xyz_array_myo, folder, "Myo_point_cloud.xyz")
save_xzys(xyz_array_lv, folder, "LV_point_cloud.xyz")
# import nibabel as nib
# def load_nii(img_path):
# nimg = nib.load(img_path)
# return nimg.get_fdata(), nimg.affine, nimg.header
#
#
# slice_locs = [-20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
#
# name = "seg"
# masks_SA, _, _ = load_nii(name+".nii.gz")
# imgs_SA, _, _ = load_nii("img.nii.gz")
# nn_mask_LV = masks_SA.copy()
# if name == "gt":
# nn_mask_LV[nn_mask_LV == 2] = 255 # Myo
# nn_mask_LV[nn_mask_LV == 3] = 60 # RV
# nn_mask_LV[nn_mask_LV == 1] = 0 # LV
# np.clip(nn_mask_LV, 0, 255, out=nn_mask_LV)
# elif name == "seg":
# nn_mask_LV[nn_mask_LV == 2] = 255 # Myo
# nn_mask_LV[nn_mask_LV == 3] = 0 # RV
# nn_mask_LV[nn_mask_LV == 1] = 60 # LV
# nn_mask_LV[nn_mask_LV < 60] = 0
# nn_mask_LV[nn_mask_LV > 60] = 255
#
# img = []
# seg = []
# for i in range(np.shape(nn_mask_LV)[2]):
# img.append(imgs_SA[:, :, i])
# seg.append(nn_mask_LV[:, :, i])
#
# extract_contour(seg, img, slice_locs[1:len(seg)+1], name+"/")