-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpedicle_detection.py
1360 lines (1263 loc) · 63.6 KB
/
pedicle_detection.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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import nilearn.image
import numpy as np
import pandas as pd
import gc
import scipy.ndimage
from sklearn.decomposition import PCA
from skimage.measure import block_reduce
from nilearn import image
from scipy.interpolate import interp1d
from scipy.ndimage import affine_transform
from scipy.misc import derivative
import sys
import cv2
import os
import matplotlib.patches
import matplotlib.pyplot as plt
import math
from matplotlib import patches
import SimpleITK as sitk
from skimage.feature import canny
from data_utilities import *
class PedicleDetection():
def __init__(self, patient_dir, inpaint_img_path, inpaint_msk_path, inpaint_cent_path, vert, mode='segmentation'):
self.vert = vert
self.mode = mode
self.SLICES_PER_VERTEBRA = 5
self.COUNTER = 0
sys.stdout.write(f'{patient_dir}:\n\n')
"""
Input: segmentation mask and centroids
"""
# Load files
img_nib = nib.load(inpaint_img_path)
msk_nib = nib.load(inpaint_msk_path)
ctd_list = load_centroids(inpaint_cent_path)
"""
Re-orient image and centroids
"""
# Check img zooms
zooms = img_nib.header.get_zooms()
sys.stdout.write(f'img zooms = {zooms}\n')
# Check img orientation
axs_code = nio.ornt2axcodes(nio.io_orientation(img_nib.affine))
sys.stdout.write(f'img orientation code: {axs_code}\n')
# Check centroids
sys.stdout.write(f'Centroid List: {ctd_list}\n')
# Resample and Reorient data
img_iso = resample_nib(img_nib, voxel_spacing=(1, 1, 1), order=3)
msk_iso = resample_nib(msk_nib, voxel_spacing=(1, 1, 1),
order=0) # or resample based on img: resample_mask_to(msk_nib, img_iso)
ctd_iso = rescale_centroids(ctd_list, img_nib, (1, 1, 1))
img_iso = reorient_to(img_iso, axcodes_to=('I', 'P', 'L')) # FINAL!
msk_iso = reorient_to(msk_iso, axcodes_to=('I', 'P', 'L')) # FINAL!
ctd_iso = reorient_centroids_to(ctd_iso, img_iso) # FINAL!
# Check img zooms
zooms = img_iso.header.get_zooms()
sys.stdout.write(f'img zooms = {zooms}\n')
# Check img orientation
axs_code = nio.ornt2axcodes(nio.io_orientation(img_iso.affine))
sys.stdout.write(f'img orientation code: {axs_code}\n')
# Check centroids
sys.stdout.write(f'new centroids: {ctd_iso}\n')
"""
Visualize re-sampled and re-oriented scan
"""
# Get voxel data
im_np = img_iso.get_fdata().astype(np.float32)
msk_np = msk_iso.get_data().astype(np.uint8)
# Get the mid-slice of the scan and mask in both sagittal and coronal planes
im_np_sag = im_np[:, :, int(im_np.shape[2] / 2)]
im_np_cor = im_np[:, int(im_np.shape[1] / 2), :]
msk_np_sag = msk_np[:, :, int(msk_np.shape[2] / 2)]
msk_np_cor = msk_np[:, int(msk_np.shape[1] / 2), :]
# Plot
fig, axs = create_figure(96, im_np_sag, im_np_cor)
axs[0].imshow(im_np_sag, cmap=plt.cm.gray, norm=wdw_sbone)
axs[0].imshow(msk_np_sag, cmap=cm_itk, alpha=0.3, vmin=1, vmax=64)
plot_sag_centroids(axs[0], ctd_iso, zooms)
axs[1].imshow(im_np_cor, cmap=plt.cm.gray, norm=wdw_sbone)
axs[1].imshow(msk_np_cor, cmap=cm_itk, alpha=0.3, vmin=1, vmax=64)
plot_cor_centroids(axs[1], ctd_iso, zooms)
fig.savefig(f'run_{self.mode}/visualized/{self.COUNTER}')
self.COUNTER += 1
self.img_iso = img_iso
self.msk_iso = msk_iso
self.ctd_iso = ctd_iso
self.patient_dir = patient_dir
def apply(self):
"""
General pipeline for spine straightening and hough transform
"""
ctd_iso_df = pd.DataFrame(self.ctd_iso[1:], columns=['vert', self.ctd_iso[0][0], self.ctd_iso[0][1], self.ctd_iso[0][2]])
f = self.interpolation(self.img_iso, self.msk_iso, self.ctd_iso, 'sag')
principal_components = self.pca(ctd_iso_df)
principal_components = self.rescale(principal_components, ctd_iso_df, self.img_iso.shape)
new_centroids = self.create_centroids(self.img_iso, self.msk_iso, self.ctd_iso, principal_components)
diff = self.create_diff(self.ctd_iso, new_centroids)
msk_np, COUNTER, detected_valid_shapes = self.transform_hough(self.img_iso, self.msk_iso, self.ctd_iso, new_centroids, diff, f,
self.patient_dir, self.COUNTER)
new_msk_iso = nib.Nifti1Image(msk_np, self.msk_iso.affine, self.msk_iso.header)
nib.save(new_msk_iso, os.path.join(self.patient_dir, f'{self.mode}_pd_{self.vert}'))
return msk_np
"""
STRAIGHTENING-HOUGH
"""
def pca(self, ctd_iso_df):
"""
Get first principal component from positions of the centroids
"""
vert = ctd_iso_df['vert']
# Input data for the PCA: only coordinates, no vertebra labels
ctd_iso_df_pca = ctd_iso_df.iloc[:, 1:]
# Perform PCA
pca = PCA(n_components=1)
principal_components = pca.fit_transform(ctd_iso_df_pca)
principal_components = pd.DataFrame(data=principal_components, columns=['principal component 1'])
principal_components.insert(0, 'vert', vert)
return principal_components
def rescale(self, principal_components, ctd_iso_df, shape):
"""
Re-scale the principal component positions to match the axis of the scan
"""
# Position in z-axis of first and last centroid in original scan
first, last = ctd_iso_df.iloc[0][1], ctd_iso_df.iloc[-1][1]
original_diff = last - first
# Position in z-axis of first and last centroid in principal components
first_pca, last_pca = principal_components.iloc[0][1], principal_components.iloc[-1][1]
pca_diff = last_pca - first_pca
# Proportion
prop = original_diff / pca_diff
# Re-scale
principal_components['pos'] = [0.0] * principal_components.shape[0]
principal_components.loc[0, 'pos'] = first
for i in range(1, principal_components.shape[0]):
diff = principal_components.loc[i, 'principal component 1'] - principal_components.loc[
i - 1, 'principal component 1']
principal_components.loc[i, 'pos'] = principal_components.loc[i - 1, 'pos'] + diff * prop
return principal_components
def create_centroids(self, img_iso, msk_iso, ctd_iso, principal_components):
"""
Create list of new centroids aligned according to the PCA on the axial plane and in the middle of
the sagittal plane
"""
# Get voxel data
im_np = img_iso.get_fdata()
mid_sag = int(im_np.shape[2] / 2)
mid_cor = int(im_np.shape[1] / 2)
pos = principal_components['pos']
new_centroids = np.array(ctd_iso.copy()[1:]).astype('float')
new_centroids[:, 1] = pos
new_centroids[:, 2] = mid_cor
new_centroids[:, 3] = mid_sag
new_centroids = new_centroids.astype('int')
new_centroids = new_centroids.tolist()
new_centroids.insert(0, ('I', 'P', 'L'))
return new_centroids
def create_diff(self, ctd_iso, new_centroids):
"""
Create array of differences between original centroids and PCA centroids
"""
ctd_iso = np.array(ctd_iso[1:])
new_centroids = np.array(new_centroids[1:])
diff = ctd_iso - new_centroids
return np.delete(diff, 0, 1)
def create_translation(self, diff_arr):
"""
Translation based on offsets
"""
aff = np.identity(4)
aff[:3, 3] = diff_arr
return aff
def apply_affine_to_numpy(self, np_arr, aff, lower, upper, interpolation='linear'):
"""
Apply an affine transformation to a 3-D-numpy-array
"""
aff_inv = np.linalg.inv(aff)
new_arr = np.zeros_like(np_arr).astype(np.int)
for i in range(int(lower), int(upper) + 1):
for j in range(new_arr.shape[1]):
for k in range(new_arr.shape[2]):
i_p, j_p, k_p, _ = aff_inv.dot([i, j, k, 1])
if interpolation == 'nn':
i_p, j_p, k_p = round(i_p), round(j_p), round(k_p)
if i_p >= int(upper):
i_p = int(upper) - 1
if i_p < 0:
i_p = 0
if j_p >= np_arr.shape[1]:
j_p = np_arr.shape[1] - 1
if j_p < 0:
j_p = 0
if k_p >= np_arr.shape[2]:
k_p = np_arr.shape[2] - 1
if k_p < 0:
k_p = 0
if np_arr[i_p][j_p][k_p] != 0:
new_arr[i][j][k] = np_arr[i_p][j_p][k_p]
if interpolation == 'linear':
if i_p >= int(upper) - 1:
i_p = int(upper) - 1 - math.exp(-6)
if i_p < 0:
i_p = 0
if j_p >= np_arr.shape[1] - 1:
j_p = np_arr.shape[1] - 1 - math.exp(-6)
if j_p < 0:
j_p = 0
if k_p >= np_arr.shape[2] - 1:
k_p = np_arr.shape[2] - 1 - math.exp(-6)
if k_p < 0:
k_p = 0
x_0 = math.floor(i_p)
x_1 = math.ceil(i_p)
if x_1 == x_0:
x_1 += 1
y_0 = math.floor(j_p)
y_1 = math.ceil(j_p)
if y_1 == y_0:
y_1 += 1
z_0 = math.floor(k_p)
z_1 = math.ceil(k_p)
if z_1 == z_0:
z_1 += 1
# Difference between point and coordinates on the lattice
x_d = (i_p - x_0) / (x_1 - x_0)
y_d = (j_p - y_0) / (y_1 - y_0)
z_d = (k_p - z_0) / (z_1 - z_0)
# Interpolate along x
c_000 = np_arr[x_0][y_0][z_0]
c_001 = np_arr[x_0][y_0][z_1]
c_010 = np_arr[x_0][y_1][z_0]
c_011 = np_arr[x_0][y_1][z_1]
c_100 = np_arr[x_1][y_0][z_0]
c_101 = np_arr[x_1][y_0][z_1]
c_110 = np_arr[x_1][y_1][z_0]
c_111 = np_arr[x_1][y_1][z_1]
if c_000 == 0 and c_001 == 0 and c_010 == 0 and c_011 == 0 \
and c_100 == 0 and c_101 == 0 and c_110 == 0 and c_111 == 0:
continue
c_00 = c_000 * (1 - x_d) + c_100 * x_d
diff = int(c_100 - c_000)
if diff != 0:
c_00 = round(c_00 / diff) * diff
else:
c_00 = round(c_00)
c_01 = c_001 * (1 - x_d) + c_101 * x_d
diff = int(c_101 - c_001)
if diff != 0:
c_01 = round(c_01 / diff) * diff
else:
c_01 = round(c_01)
c_10 = c_010 * (1 - x_d) + c_110 * x_d
diff = int(c_110 - c_010)
if diff != 0:
c_10 = round(c_10 / diff) * diff
else:
c_10 = round(c_10)
c_11 = c_011 * (1 - x_d) + c_111 * x_d
diff = int(c_111 - c_011)
if diff != 0:
c_11 = round(c_11 / diff) * diff
else:
c_11 = round(c_11)
# Interpolate along y
c_0 = c_00 * (1 - y_d) + c_10 * y_d
diff = int(c_10 - c_00)
if diff != 0:
c_0 = round(c_0 / diff) * diff
else:
c_0 = round(c_0)
c_1 = c_01 * (1 - y_d) + c_11 * y_d
diff = int(c_11 - c_01)
if diff != 0:
c_1 = round(c_1 / diff) * diff
else:
c_1 = round(c_1)
# Interpolate along z
c = c_0 * (1 - z_d) + c_1 * z_d
diff = int(c_1 - c_0)
if diff != 0:
c = round(c / diff) * diff
else:
c = round(c)
new_arr[i][j][k] = c
return new_arr
def create_rotation(self, f, pos, pivot, plane):
"""
Rotation based on gradient of interpolation polynomial
"""
d = derivative(f, pos, n=1)
# Convert derivative to angle in radian
rad = math.atan(d / 1)
if plane == 'sag':
trans_1 = np.identity(4)
trans_1[:2, 3] = -pivot
rot = np.array([[math.cos(-rad), -math.sin(-rad), 0, 0],
[math.sin(-rad), math.cos(-rad), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
trans_2 = np.identity(4)
trans_2[:2, 3] = pivot
elif plane == 'cor':
trans_1 = np.identity(4)
trans_1[[0, 2], 3] = -pivot
rot = np.array([[math.cos(-rad), 0, math.sin(-rad), 0],
[0, 1, 0, 0],
[-math.sin(-rad), 0, math.cos(-rad), 0],
[0, 0, 0, 1]])
trans_2 = np.identity(4)
trans_2[[0, 2], 3] = pivot
return trans_2.dot(rot.dot(trans_1))
def get_slices(self, img, lower, upper):
"""
Get a certain number of axial slices for a given vertebra (based on volume per slice)
"""
# Store number of found voxels per axial slide
num_pixels = []
# Loop over all axial slices
for i in range(int(lower), int(upper) + 1):
slice = self.crop_2D_image(img, i)
voxel_count = np.count_nonzero(slice)
if voxel_count > 0:
num_pixels.append((i, voxel_count))
# Get top x slides in terms of voxel number
num_pixels.sort(key=lambda x: x[1], reverse=True)
num_pixels = [x[0] for x in num_pixels[:self.SLICES_PER_VERTEBRA]]
return num_pixels
def evaluate(self, a, b, c, d):
"""
Check if given parameters for body/canal are valid
:return: True if valid, False if invalid
"""
if c == 0 or d == 0:
return False
return True
def get_downsampling_factor(self, height, width):
"""
Get appropriate downsampling-factor (width should not be bigger than 30)
:param width:
:param height:
:return:
"""
if width <= 35:
return 1
return int(width / 35)
def hough(self, list_slices, img_mask, vert, lower, upper, COUNTER):
"""
Perform Hough transform for a number of axial slices of a given vertebra
"""
# Parameters for the found ellipses (distinguished between body and canal detection)
params_body = []
params_canal = []
for index_slice, x in enumerate(list_slices):
# Create 2D-slices
tmp = self.crop_2D_image(img_mask, x)
# Fit bounding box around vertebra
bound_x, bound_y, bound_w, bound_h, bound_c_x, bound_c_y = self.bounding_box(tmp)
downsampling_factor = self.get_downsampling_factor(bound_h, bound_w)
tmp = block_reduce(tmp, block_size=(downsampling_factor, downsampling_factor), func=np.max)
bound_x = int(bound_x / downsampling_factor)
bound_y = int(bound_y / downsampling_factor)
bound_w = int(bound_w / downsampling_factor)
bound_h = int(bound_h / downsampling_factor)
bound_c_x = int(bound_c_x / downsampling_factor)
bound_c_y = int(bound_c_y / downsampling_factor)
# Crop images to bounding box
tmp_ROI = tmp[bound_y:bound_y + bound_h, bound_x:bound_x + bound_w]
# Width and height of ROI
tmp_width = tmp_ROI.shape[1]
tmp_height = tmp_ROI.shape[0]
# Utility variables
perc_x = tmp_width / 100
perc_y = tmp_height / 100
middle_x = round(tmp_width / 2)
middle_y = round(tmp_height / 2)
"""
Parameters for body
"""
if vert in list(range(4)): # Cervical
params_body_ab = round(tmp_width * 0.01 / 2), round(tmp_width * 0.93 / 2), round(
tmp_height * 0.01 / 2), round(
(tmp_height * 0.53) / 2)
params_body_cd = round(bound_c_x - 10 * perc_x), round(bound_c_x + 10 * perc_x), \
1, round(bound_c_y * (7 / 8))
elif vert in list(range(4, 8)): # Cervical
params_body_ab = round(tmp_width * 0.24 / 2), round(tmp_width * 0.93 / 2), round(
tmp_height * 0.06 / 2), round(
(tmp_height * 0.53) / 2)
params_body_cd = round(bound_c_x - 10 * perc_x), round(bound_c_x + 10 * perc_x), \
1, round(bound_c_y * (7 / 8))
elif vert in list(range(8, 10)): # Thoracic 8-9
params_body_ab = round(tmp_width * 0.27 / 2), round(tmp_width * 0.82 / 2), round(
tmp_height * 0.10 / 2), round(
(tmp_height * 0.70) / 2)
params_body_cd = round(bound_c_x - 10 * perc_x), round(bound_c_x + 10 * perc_x), round(
1 / 3 * bound_c_y), round(
bound_c_y * (7 / 8))
elif vert in list(range(10, 18)): # Thoracic 10-17
params_body_ab = round(tmp_width * 0.23 / 2), round(tmp_width * 0.85 / 2), round(
tmp_height * 0.13 / 2), round(
(tmp_height * 0.70) / 2)
params_body_cd = round(bound_c_x - 10 * perc_x), round(bound_c_x + 10 * perc_x), round(
1 / 3 * bound_c_y), round(
bound_c_y * (7 / 8))
elif vert in list(range(18, 20)): # Thoracic 18-19
params_body_ab = round(tmp_width * 0.47 / 2), round(tmp_width * 0.96 / 2), round(
tmp_height * 0.26 / 2), round(
(tmp_height * 0.56) / 2)
params_body_cd = round(bound_c_x - 10 * perc_x), round(bound_c_x + 10 * perc_x), round(
1 / 3 * bound_c_y), round(
bound_c_y * (7 / 8))
elif vert in list(range(20, 24)): # Lumbar 20-23
params_body_ab = round(tmp_width * 0.28 / 2), round(tmp_width * 0.98 / 2), round(
tmp_height * 0.14 / 2), round(
(tmp_height * 0.62) / 2)
params_body_cd = round(bound_c_x - 10 * perc_x), round(bound_c_x + 10 * perc_x), round(
1 / 3 * bound_c_y), round(
bound_c_y * (7 / 8))
elif vert >= 24: # Lumbar 24
params_body_ab = round(tmp_width * 0.37 / 2), round(tmp_width * 0.78 / 2), round(
tmp_height * 0.15 / 2), round(
(tmp_height * 0.57) / 2)
params_body_cd = round(bound_c_x - 10 * perc_x), round(bound_c_x + 10 * perc_x), round(
1 / 3 * bound_c_y), round(
bound_c_y * (7 / 8))
else:
params_body_ab = round(tmp_width * (1 / 4) / 2), round(tmp_width / 2), 1, round(
(tmp_height * (3 / 4)) / 2)
params_body_cd = round(bound_c_x - 10 * perc_x), round(bound_c_x + 10 * perc_x), round(
1 / 3 * bound_c_y), round(
bound_c_y * (7 / 8))
# Additional constraint
params_body_ab = max(params_body_ab[0], 1), max(params_body_ab[1], 1), max(params_body_ab[2], 1), max(
params_body_ab[3], 1)
"""
Find body
"""
hough_space, d_body, c_body, a_body, b_body = self.hough_transform_ellipse_filled(tmp_ROI, params_body_ab,
params_body_cd, tmp_width,
tmp_height, vert,
to_detect='body',
c_x=bound_c_x, c_y=bound_c_y)
"""
Parameters for canal
"""
if vert in list(range(8)): # Cervical
params_canal_ab = round((tmp_width * 0.18) / 2), round((tmp_width * 0.45) / 2), round(
(tmp_height * 0.11) / 2), round((tmp_height * 0.42) / 2)
params_canal_cd = round(c_body - 20 * perc_x), round(c_body + 20 * perc_x), \
round(d_body + b_body + 1), round(bound_c_y + (tmp_height - 2 - bound_c_y) * (3 / 5))
elif vert in list(range(8, 10)): # Thoracic 8-9
params_canal_ab = round((tmp_width * 0.09) / 2), round((tmp_width * 0.45) / 2), round(
(tmp_height * 0.14) / 2), round((tmp_height * 0.33) / 2)
params_canal_cd = round(c_body - 8 * perc_x), round(c_body + 8 * perc_x), \
round(d_body + b_body + 1), round(bound_c_y + (tmp_height - 2 - bound_c_y) * (3 / 5))
elif vert in list(range(10, 18)): # Thoracic 10-17
params_canal_ab = round((tmp_width * 0.09) / 2), round((tmp_width * 0.47) / 2), round(
(tmp_height * 0.08) / 2), round((tmp_height * 0.38) / 2)
params_canal_cd = round(c_body - 8 * perc_x), round(c_body + 8 * perc_x), \
round(d_body + b_body + 1), round(bound_c_y + (tmp_height - 2 - bound_c_y) * (4 / 5))
elif vert in list(range(18, 20)): # Thoracic 18-19
params_canal_ab = round((tmp_width * 0.16) / 2), round((tmp_width * 0.46) / 2), round(
(tmp_height * 0.08) / 2), round((tmp_height * 0.33) / 2)
params_canal_cd = round(c_body - 8 * perc_x), round(c_body + 8 * perc_x), \
round(d_body + b_body + 1), round(bound_c_y + (tmp_height - 2 - bound_c_y) * (4 / 5))
elif vert in list(range(20, 24)): # Lumbar 20-23
params_canal_ab = round((tmp_width * 0.05) / 2), round((tmp_width * 0.46) / 2), round(
(tmp_height * 0.06) / 2), round((tmp_height * 0.41) / 2)
params_canal_cd = round(c_body - 8 * perc_x), round(c_body + 8 * perc_x), \
round(d_body + b_body + 1), round(bound_c_y + (tmp_height - 2 - bound_c_y) * (4 / 5))
elif vert >= 24: # Lumbar 24
params_canal_ab = round((tmp_width * 0.09) / 2), round((tmp_width * 0.36) / 2), round(
(tmp_height * 0.05) / 2), round((tmp_height * 0.48) / 2)
params_canal_cd = round(c_body - 8 * perc_x), round(c_body + 8 * perc_x), \
round(d_body + b_body + 1), round(bound_c_y + (tmp_height - 2 - bound_c_y) * (4 / 5))
else:
params_canal_ab = 1, round((tmp_width * 0.8) / 2), round((tmp_height * (1 / 15)) / 2), round(
(tmp_height * 0.5) / 2)
params_canal_ab = max(params_canal_ab[0], 1), max(params_canal_ab[1], 1), max(params_canal_ab[2], 1), max(
params_canal_ab[3], 1)
"""
Find canal
"""
hough_space, d_canal, c_canal, a_canal, b_canal = self.hough_transform_ellipse_filled(tmp_ROI, params_canal_ab,
params_canal_cd, tmp_width,
tmp_height, vert,
inverted=True,
lowest_body=d_body + b_body,
body_height=b_body * 2,
to_detect='canal')
"""
Show found ellipses
"""
self.show_ellipses([(a_body, b_body, c_body, d_body), (a_canal, b_canal, c_canal, d_canal)], tmp_ROI,
pedicle_detection=False, save=True, dir=f'run_{self.mode}/visualized/{COUNTER}',
center=(bound_c_x, bound_c_y))
COUNTER += 1
self.show_ellipses([(a_body, b_body, c_body, d_body), (a_canal, b_canal, c_canal, d_canal)], tmp_ROI,
pedicle_detection=True, save=True, dir=f'run_{self.mode}/visualized/{COUNTER}',
center=(bound_c_x, bound_c_y))
COUNTER += 1
if self.evaluate(a_body, b_body, c_body, d_body) and \
self.evaluate(a_canal, b_canal, c_canal, d_canal):
# Parameters are considered valid -> store them and multiply by downsampling-factor
params_body.append(((
a_body * downsampling_factor, b_body * downsampling_factor,
(bound_x + c_body) * downsampling_factor,
(bound_y + d_body) * downsampling_factor)))
params_canal.append((a_canal * downsampling_factor, b_canal * downsampling_factor,
(bound_x + c_canal) * downsampling_factor,
(bound_y + d_canal) * downsampling_factor))
return params_body, params_canal, COUNTER
def bounding_box(self, img):
"""
Bounding box around vertebra in axial slice
Function taken from https://stackoverflow.com/questions/21104664/extract-all-bounding-boxes-using-opencv-python
"""
src = np.where(img == 0, 255, 0)
src = src.astype(np.uint8)
original = src.copy()
thresh = cv2.threshold(src, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find contours, obtain bounding boxes
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# Find bounding box surrounding all objects
glob_ul, glob_ur, glob_ll, glob_lr = None, None, None, None
# Filter out bounding boxes that are too small
cnts_new = [c for c in cnts if cv2.boundingRect(c)[2] > 10]
if len(cnts_new) != 0:
cnts = cnts_new
for c in cnts:
x, y, w, h = cv2.boundingRect(c)
ul = x, y
ur = x + w, y
ll = x, y + h
lr = x + w, y + h
if glob_ul is None:
glob_ul = ul[0], ul[1]
glob_ur = ur[0], ur[1]
glob_ll = ll[0], ll[1]
glob_lr = lr[0], lr[1]
else:
glob_ul = min(ul[0], glob_ul[0]), min(ul[1], glob_ul[1])
glob_ur = max(ur[0], glob_ur[0]), min(ur[1], glob_ur[1])
glob_ll = min(ll[0], glob_ll[0]), max(ll[1], glob_ll[1])
glob_lr = max(lr[0], glob_lr[0]), max(lr[1], glob_lr[1])
glob_x, glob_y = min(glob_ul[0], glob_ll[0]), min(glob_ul[1], glob_ur[1])
glob_x0, glob_y0 = max(glob_ur[0], glob_lr[0]), max(glob_ll[1], glob_lr[1])
glob_w, glob_h = glob_x0 - glob_x, glob_y0 - glob_y
ROI = original[glob_y:glob_y + glob_h, glob_x:glob_x + glob_w]
thresh = cv2.threshold(ROI, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Calculate moments of binary image
M = cv2.moments(thresh)
# Calculate x,y coordinate of center
centroid_X = int(M['m10'] / M['m00'])
centroid_Y = int(M['m01'] / M['m00'])
return glob_x, glob_y, glob_w, glob_h, centroid_X, centroid_Y
def fit_cylinder(self, ellipse_params):
"""
Fit elliptical cylinder to a set of ellipses
"""
# Simple method to fit elliptical cylinder (minimizes mean-squared distances between lowermost point
# and rightmost point of target cylinder and all ellipses)
sum_m = np.zeros(2)
sum_n = np.zeros(2)
for a, b, c, d in ellipse_params:
m = np.array([c + a, d])
n = np.array([c, d + b])
sum_m += m
sum_n += n
sum_m /= len(ellipse_params)
sum_n /= len(ellipse_params)
c = sum_n[0]
d = sum_m[1]
a = sum_m[0] - c
b = sum_n[1] - d
# Return parameters of cylinder
return a, b, c, d
def relabel(self, msk, vert_index, lower, upper, valid, m=None, b=None):
"""
Re-label mask according to the plain detected during pedicle detection
"""
for i in range(int(lower), int(upper) + 1):
slice = self.crop_2D_image(msk, i)
if valid:
res = np.fromfunction(lambda j, k: k * m + b < j, (msk.shape[1], msk.shape[2]))
res = np.where(res == True, 30, vert_index)
bool_mask = np.where(slice == 0, False, True)
res = np.where(bool_mask == False, 0, res)
# res = np.where(res == 30, 0, res)
msk[i] = res
else:
msk[i] = np.where(slice != 0, vert_index, 0)
def count_pixels_body(self, msk, type):
"""
Counts the number of pixels belonging to vertebral bodies
:param msk:
:param type: type of input msk (as labeling differs)
'sub': ground truth mask from Anduin
'gen': generated mask from pipeline
:return:
"""
if type == 'sub':
return len(np.where(msk == 49 or msk == 50))
elif type == 'gen':
return len(np.where(msk == 50))
def count_pixels_processes(self, msk, type):
"""
Counts the number of pixels belonging to spinal processes
:param msk:
:param type: type of input msk (as labeling differs)
'sub': ground truth mask from Anduin
'gen': generated mask from pedicle detection pipeline
:return:
"""
if type == 'sub':
return len(np.where(msk in list(range(41, 49))))
elif type == 'gen':
return len(np.where(msk == 41))
def count_pixels_no_spine(self, msk):
"""
Counts the number of pixels that do not belong to the spine (in the case of masks, background images)
:param msk:
:param type: type of input msk (as labeling differs)
'sub': ground truth mask from Anduin
'gen': generated mask from pipeline
:return:
"""
return msk.shape[0] * msk.shape[1] * msk.shape[2] - np.count_nonzero(msk)
def performance_metrics(self, pred, ground_truth):
"""
Calculates performance metrics based on the vertebral bodies
:param pred: predicted segmentation mask from pedicle detection pipeline
:param ground_truth: segmentation mask from Anduin
:return:
"""
pred = np.where(pred != 0, 50, 0)
ground_truth = np.where((ground_truth == 49) | (ground_truth == 50), 50, 100)
matching = np.count_nonzero(ground_truth == pred)
only_pred = (pred == 50).sum()
only_gt = (ground_truth == 50).sum()
dice = (2 * matching) / (only_gt + only_pred)
intersection_over_union = matching / (only_gt + only_pred - matching)
return dice, intersection_over_union
def scale_centroids(self, ctr_list, scaling_factor):
"""
Scale list of centroids according to scaling factor
"""
for i in range(1, len(ctr_list)):
ctr_list[i] = [ctr_list[i][0]] + [x * scaling_factor for x in ctr_list[i][1:]]
def transform_hough(self, img_iso, msk_iso, ctd_iso, new_centroids, diff, f, scan, COUNTER):
"""
Transformation (Rotation) of each vertebra and hough transform
"""
# List to store transformation for each vertebra
transformations = []
org_shape = msk_iso.shape
zooms = img_iso.header.get_zooms()
# Get voxel data
im_np = img_iso.get_fdata()
mid_sag = int(im_np.shape[2] / 2)
mid_cor = int(im_np.shape[1] / 2)
# Did the algorithm succeed?
detected_valid_shapes = True
for v_index, v in enumerate(ctd_iso[1:]):
detected_valid_shapes = False
vert = v[0]
if vert != self.vert:
continue
pos = v[1]
# Filter mask to current vertebra
msk_iso_new = nib.Nifti1Image(np.where(msk_iso.get_fdata() == vert, vert, 0), msk_iso.affine,
msk_iso.header)
# Rotation to straighten vertebra
rot_1 = self.create_rotation(f, pos, np.array([v[1], v[2]]), 'sag')
# Optimization: Set upper and lower border for the transformation
if v_index == 0:
upper = ctd_iso[v_index + 3][1]
elif v_index >= len(ctd_iso) - 3:
upper = msk_iso_new.get_fdata().shape[0] - 1
else:
upper = ctd_iso[v_index + 3][1]
if v_index in [0, 1]:
lower = 0
else:
lower = ctd_iso[v_index - 1][1]
# Define 4x4-affine matrix
affine = rot_1
inv_affine = np.linalg.inv(affine)
msk_np_new = msk_iso_new.get_data()
"""
Visualization of vertebra before rotation (not re-labeled)
"""
# Get the mid-slice of the scan and mask in both sagittal and coronal planes
msk_np_sag = msk_np_new[:, :, mid_sag]
msk_np_cor = msk_np_new[:, mid_cor, :]
# Plot
fig, axs = create_figure(96, msk_np_sag, msk_np_cor)
axs[0].imshow(msk_np_sag, cmap=cm_itk, alpha=0.3, vmin=1, vmax=64)
plot_sag_centroids(axs[0], new_centroids, zooms)
axs[1].imshow(msk_np_cor, cmap=cm_itk, alpha=0.3, vmin=1, vmax=64)
plot_cor_centroids(axs[1], new_centroids, zooms)
fig.savefig(f'run_{self.mode}/visualized/{COUNTER}')
COUNTER += 1
msk_iso_new = nib.Nifti1Image(msk_np_new, msk_iso.affine, msk_iso.header)
msk_iso_new = nilearn.image.resample_img(msk_iso_new, target_affine=msk_iso_new.affine.dot(inv_affine),
target_shape=msk_iso_new.shape, interpolation='nearest')
# Current vertebra volume
msk_np_new = msk_iso_new.get_data()
msk_iso_new_hough = nib.Nifti1Image(np.where(msk_np_new == vert, 1, 0), msk_iso_new.affine,
msk_iso_new.header)
c = 0
hole_filling_factor = 0.05
# Fill holes
tmp = sitk.GetImageFromArray(msk_iso_new_hough.get_data())
tmp = sitk.BinaryFillhole(tmp)
tmp = sitk.GetArrayFromImage(tmp)
msk_iso_new_hough = nib.Nifti1Image(tmp, msk_iso_new_hough.affine, msk_iso_new_hough.header)
while True:
# Search for ellipses until enough valid assignments were found
if c != 0:
# If there was already an unsuccessful run of hough transforms, there are likely holes in the ellipse
# -> hole-filling procedure
width = im_np.shape[2]
tmp = sitk.GetImageFromArray(msk_iso_new_hough.get_data())
mc = sitk.BinaryMorphologicalClosingImageFilter()
radius = round((width * hole_filling_factor) / 2)
mc.SetKernelRadius(radius)
tmp = mc.Execute(tmp)
tmp = sitk.GetArrayFromImage(tmp)
msk_iso_new_hough = nib.Nifti1Image(tmp, msk_iso_new_hough.affine, msk_iso_new_hough.header)
"""
Hough transform
"""
# Slices to perform the hough transform
list_slices = self.get_slices(msk_iso_new_hough.get_data(), lower, upper)
# Hough transform
params_body, params_canal, COUNTER = self.hough(list_slices, msk_iso_new_hough.get_data(), vert, lower,
upper,
COUNTER)
if len(params_body) >= 2:
# We successfully found an assignment with the hough transform
# Constraint can be varied: for instance, we could demand at least 2 valid ellipses
detected_valid_shapes = True
break
if hole_filling_factor > 0.10:
# We did not find a successful assignment, even after morphological closing
break
COUNTER -= 10
hole_filling_factor += 0.01
c += 1
# TODO: CHANGED
if detected_valid_shapes:
a_body, b_body, c_body, d_body = self.fit_cylinder(params_body)
a_canal, b_canal, c_canal, d_canal = self.fit_cylinder(params_canal)
m, b = self.pedicle_detection([(a_body, b_body, c_body, d_body), (a_canal, b_canal, c_canal, d_canal)],
msk_np_new)
self.relabel(msk_np_new, vert, lower, upper, True, m=m, b=b)
else:
self.relabel(msk_np_new, vert, lower, upper, False)
msk_np_new = msk_iso_new.get_data()
"""
Visualization of the vertebra after rotation (re-labeled)
"""
# Get the mid-slice of the scan and mask in both sagittal and coronal planes
msk_np_sag = msk_np_new[:, :, mid_sag]
msk_np_cor = msk_np_new[:, mid_cor, :]
# Plot
fig, axs = create_figure(96, msk_np_sag, msk_np_cor)
axs[0].imshow(msk_np_sag, cmap=cm_itk, alpha=0.3, vmin=1, vmax=64)
plot_sag_centroids(axs[0], new_centroids, zooms)
axs[1].imshow(msk_np_cor, cmap=cm_itk, alpha=0.3, vmin=1, vmax=64)
plot_cor_centroids(axs[1], new_centroids, zooms)
fig.savefig(f'run_{self.mode}/visualized/{COUNTER}')
COUNTER += 1
"""
Rotate vertebra back to original position
"""
msk_iso_new = nib.Nifti1Image(msk_np_new, msk_iso_new.affine, msk_iso_new.header)
msk_iso_new = nilearn.image.resample_img(msk_iso_new, target_affine=msk_iso_new.affine.dot(affine),
target_shape=msk_iso_new.shape, interpolation='nearest')
msk_np_new = msk_iso_new.get_data().astype(np.uint8)
"""
Visualization of the vertebra in original position (re-labeled)
"""
# Get the mid-slice of the scan and mask in both sagittal and coronal planes
msk_np_sag = msk_np_new[:, :, mid_sag]
msk_np_cor = msk_np_new[:, mid_cor, :]
# Plot
fig, axs = create_figure(96, msk_np_sag, msk_np_cor)
axs[0].imshow(msk_np_sag, cmap=cm_itk, alpha=0.3, vmin=1, vmax=64)
plot_sag_centroids(axs[0], new_centroids, zooms)
axs[1].imshow(msk_np_cor, cmap=cm_itk, alpha=0.3, vmin=1, vmax=64)
plot_cor_centroids(axs[1], new_centroids, zooms)
fig.savefig(f'run_{self.mode}/visualized/{COUNTER}')
COUNTER += 1
# Append re-labeled array to global array
transformations.append(msk_np_new)
sys.stdout.write(f'Transformed vertebra {vert}\n')
transformations = np.array(transformations)
transformed = np.sum(transformations, axis=0)
"""
Visualization of global mask
"""
# Get the mid-slice of the scan and mask in both sagittal and coronal planes
msk_np_sag = transformed[:, :, mid_sag]
msk_np_cor = transformed[:, mid_cor, :]
# Plot
fig, axs = create_figure(96, msk_np_sag, msk_np_cor)
axs[0].imshow(msk_np_sag, cmap=cm_itk, alpha=0.3, vmin=1, vmax=64)
plot_sag_centroids(axs[0], new_centroids, zooms)
axs[1].imshow(msk_np_cor, cmap=cm_itk, alpha=0.3, vmin=1, vmax=64)
plot_cor_centroids(axs[1], new_centroids, zooms)
fig.savefig(f'run_{self.mode}/visualized/{COUNTER}')
COUNTER += 1
return transformed, COUNTER, True
def interpolation(self, img_iso, msk_iso, ctd_iso, plane, kind=1):
"""
Interpolate through centroids to compute rotation
"""
# Get "middle" in the sagittal/coronal plane
msk_np = msk_iso.get_data()
mid_sag = int(msk_np.shape[2] / 2)
mid_cor = int(msk_np.shape[1] / 2)
# Distances from centroids to middle in the coronal/sagittal direction
if plane == 'sag':
ctd_cor = np.array(ctd_iso[1:])[:, 2]
dist = ctd_cor - mid_cor
elif plane == 'cor':
ctd_sag = np.array(ctd_iso[1:])[:, 3]
dist = ctd_sag - mid_sag
x = np.array(ctd_iso[1:])[:, 1]
# Create interpolation function
f = interp1d(x, dist, kind=kind, fill_value='extrapolate')
return f
def non_zero_entries(self, arr):
"""
Non-zero entries of a numpy-array
"""
return np.count_nonzero(arr)
def print_vert_volume(self, msk_np, vert, metrics_file):
"""
Print the volume of a given vertebra in mm^3
"""
volume = self.non_zero_entries(msk_np)
sys.stdout.write('-----------------------------------------------\n')
metrics_file.write('-----------------------------------------------\n')
sys.stdout.write(f'\t\t\tVolume of vertebra {vert}: {volume}\n')
metrics_file.write(f'\t\t\tVolume of vertebra {vert}: {volume}\n')
sys.stdout.write('-----------------------------------------------\n')
metrics_file.write('-----------------------------------------------\n')
def compare_volumes(self, msk_np, sub_np, metrics_file):
"""
Compare volumes of the spine between two masks
"""
after = self.non_zero_entries(msk_np)
before = self.non_zero_entries(sub_np)
sys.stdout.write('-----------------------------------------------\n')
metrics_file.write('-----------------------------------------------\n')
sys.stdout.write(f'Number of voxels before transformation: {before}\n')
metrics_file.write(f'Number of voxels before transformation: {before}\n')
sys.stdout.write(f'Number of voxels after transformation: {after}\n')
metrics_file.write(f'Number of voxels after transformation: {after}\n')
relative_error = (max(before, after) - min(before, after)) / max(before, after)
sys.stdout.write(f'Relative error: {relative_error}\n')
metrics_file.write(f'Relative error: {relative_error}\n')
sys.stdout.write('-----------------------------------------------\n')
metrics_file.write('-----------------------------------------------\n')
return relative_error
def print_performance_metrics(self, dice, IoU, metrics_file):
"""
Print performance metrics (dice coefficient, IoU)
"""
sys.stdout.write('-----------------------------------------------\n')
metrics_file.write('-----------------------------------------------\n')
sys.stdout.write('Performance metrics:\n')
metrics_file.write('Performance metrics:\n')
sys.stdout.write(f'Dice coefficient: {dice:.2f} ({(dice * 100):.2f} %)\n')
metrics_file.write(f'Dice coefficient: {dice:.2f} ({(dice * 100):.2f} %)\n')
sys.stdout.write(f'IoU: {IoU:.2f} ({(IoU * 100):.2f} %)\n')
metrics_file.write(f'IoU: {IoU:.2f} ({(IoU * 100):.2f} %)\n')
sys.stdout.write('-----------------------------------------------\n')
metrics_file.write('-----------------------------------------------\n')
return dice, IoU
"""
HOUGH TRANSFORM
"""
def convert_to_binary(self, path, threshold):
"""
Converts input image in arbitrary format to binary images containing only black and white pixels
:return:
"""
img = cv2.imread(path, 2)
ret, bw_img = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
return bw_img
def hough_transform_ellipse_filled(self, input, hyperparams_ab, hyperparams_cd, img_width, img_height, vert,
inverted=False, lowest_body=None, body_height=None, to_detect='body', c_x=None,
c_y=None):
"""
Performs a hough transform for ellipse detection
"""
min_a, max_a, min_b, max_b = hyperparams_ab
min_c, max_c, min_d, max_d = hyperparams_cd
def hough_accumulation():
# Initialize Hough space
hough_space = [[[[0] * (max_b - min_b + 2) for _ in range(max_a - min_a + 2)] for _ in range(img_width)] for
_ in range(img_height)]
# Loop over permitted ellipse shapes
for a in range(min_a, max_a + 1):
for b in range(min_b, max_b + 1):
# For every ellipse shape, loop over all possible center points
for c in range(min_c, max_c + 1):
# Break if ellipse is invalid with respect to c and a
if c - a < 1 or c + a > img_width - 2:
continue
for d in range(min_d, max_d + 1):
# Break if ellipse is invalid with respect to d and b
if d - b < 1 or d + b > img_height - 2:
continue
if to_detect == 'canal':
"""
If we currently detect spinal canal, ensure that uppermost point is not too far away
from lowermost point of body
"""
upper_canal = d - b
# Distinguish between different spine regions
if vert in list(range(2)): # Cervical
pass
elif vert in list(range(2, 8)):
if upper_canal - lowest_body > body_height * 2 / 5:
continue
elif vert in list(range(8, 10)): # Thoracic 8-9
if upper_canal - lowest_body > body_height * 2 / 5:
continue
elif vert in list(range(10, 18)): # Thoracic 10-17