-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprojection.py
1444 lines (1150 loc) · 59.9 KB
/
projection.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 os
import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt
import cv2
import random
import scipy.ndimage as ndimage
import ast
import torch
from PIL import Image
import shutil
import torch.nn.functional as F
import time
import multiprocessing
def plot_organ_projection(list_of_array, organ_name, pid, axis=2,
pngpath=None, th=0.5, ct=False, save=True,
window='organs'):
if axis == 2:
projection = np.zeros((list_of_array[0][:,:,0].shape), dtype='float')
elif axis == 1:
projection = np.zeros((list_of_array[0][:,0,:].shape), dtype='float')
elif axis == 0:
projection = np.zeros((list_of_array[0][0,:,:].shape), dtype='float')
else:
raise ValueError('Axis should be 0, 1, or 2')
for i in range(len(list_of_array)):
x=list_of_array[i]
if ct:
if window=='organs':
x=np.where(x>250,250,x)
x=np.where(x<-150,-150,x)
x=x+150
x=x/400
elif window=='bone':
x=np.where(x>1500,1500,x)
x=np.where(x<-500,-500,x)
x=x+500
x=x/2000
else:
raise ValueError('Window should be organs or bone')
organ_projection = np.sum(x, axis=axis) * 1.0
organ_projection /= np.max(organ_projection)
projection += organ_projection
projection /= np.max(projection)
if th>0:
projection=np.where(projection>0,projection/(1/(1-th))+th,0)
projection *= 255.0
projection = np.rot90(projection)
if save:
if not os.path.exists(pngpath):
os.makedirs(pngpath)
cv2.imwrite(os.path.join(pngpath, pid + '_axis_'+str(axis)+'.png'), projection)
print('Organ projection of ' + organ_name + ' for patient ' + pid + ' is saved to ' + os.path.join(pngpath, pid + '.png'))
return projection
def plot_organ_projection_3_axis(list_of_array, organ_name, pid,
pngpath=None, th=0.5, ct=False, save=True,
window='organs'):
projections=[]
for axis in range(3):
projections.append(plot_organ_projection(list_of_array, organ_name, pid, axis,
pngpath, th=th, ct=ct,save=save,
window=window))
return projections
def resample_image(image, original_spacing, target_spacing=(1, 1, 1),order=1):
"""
Resample the image to the target spacing.
Parameters:
image (nibabel.Nifti1Image): Input image to resample.
target_spacing (tuple): Target spacing in x, y, z directions.
Returns:
numpy.ndarray: Resampled image data.
"""
# Get original spacing
resize_factor = np.array(original_spacing) / np.array(target_spacing)
new_shape = np.round(image.shape * resize_factor).astype(int)
# Resample image
try:image=image.get_fdata()
except:pass
resampled_image = ndimage.zoom(image, resize_factor, order=order)
return resampled_image
def load_ct_and_mask(pid, organ, datapath,
ct_path=None, mask_path=None,
resize=True):
"""
Load and reorient the CT scan and its corresponding mask to the standard LAS orientation.
Parameters:
pid (str): Patient ID.
organ (str): Name of the organ for the mask file.
datapath (str): Path to the dataset.
ct_path (str): Path to the CT scan.
mask_path (str): Path to the mask file.
Returns:
ct (np.ndarray): Reoriented CT scan data.
mask (np.ndarray): Reoriented mask data.
"""
# Load the CT scan
if ct_path is None:
ct_path = os.path.join(datapath, pid, 'ct.nii.gz')
ct_nii = nib.load(ct_path)
spacing=ct_nii.header.get_zooms()
resampled = resample_image(ct_nii, spacing, target_spacing=(1, 1, 1),order=1)
ct_nii = nib.Nifti1Image(resampled, affine=ct_nii.affine, header=ct_nii.header)
# Calculate the orientation transformation based on the CT scan
transform = get_orientation_transform(ct_nii)
# Apply the transformation to the CT scan data
ct = apply_transform(ct_nii.get_fdata(), transform)
# Load the mask using the same transformation
if mask_path is None:
# Try the 'predictions' path first
mask_path = os.path.join(datapath, pid, 'predictions', organ + '.nii.gz')
# If the file doesn't exist in 'predictions', check 'segmentations'
if not os.path.exists(mask_path):
mask_path = os.path.join(datapath, pid, 'segmentations', organ + '.nii.gz')
mask_nii = nib.load(mask_path)
resampled = resample_image(mask_nii, spacing, target_spacing=(1, 1, 1),order=0)
if not np.array_equal(resampled, resampled.astype(bool)):
resampled = np.where(resampled > 0.5, 1, 0)
mask_nii = nib.Nifti1Image(resampled, affine=mask_nii.affine, header=mask_nii.header)
# Apply the same transformation to the mask data
#mask = apply_transform(mask_nii.get_fdata(), transform).astype(np.uint8)
mask = apply_transform(mask_nii.get_fdata(), get_orientation_transform(mask_nii)).astype(np.uint8)
return ct, mask
def get_orientation_transform(nii, orientation=('L', 'A', 'S')):
"""
Compute the transformation needed to reorient the image to LAS standard orientation.
"""
current_orientation = nib.orientations.io_orientation(nii.affine)
standard_orientation = nib.orientations.axcodes2ornt(orientation)
transform = nib.orientations.ornt_transform(current_orientation, standard_orientation)
return transform
def apply_transform(data, transform):
"""
Apply the orientation transformation to the image data.
"""
return nib.orientations.apply_orientation(data, transform)
def apply_spacing_transform(spacing, transform):
"""
Apply the orientation transformation to the spacing values.
"""
# Rearrange the spacing based on the reordering in the transform matrix
reoriented_spacing = [spacing[int(t[0])] for t in transform]
# Take into account the flips (axis direction changes) indicated by the transform
reoriented_spacing = [sp if t[1] == 1 else -sp for sp, t in zip(reoriented_spacing, transform)]
return tuple(np.abs(reoriented_spacing))
def load_ct(pid, datapath, ct_path, device='cuda'):
"""
Load and reorient the CT scan to the standard LAS orientation.
Parameters:
pid (str): Patient ID.
datapath (str): Path to the dataset.
ct_path (str): Path to the CT scan.
Returns:
ct (np.ndarray): Reoriented CT scan data.
spacing (tuple): Reoriented spacing values.
"""
# Load the CT scan
if ct_path is None:
ct_path = os.path.join(datapath, pid, 'ct.nii.gz')
ct_nii = nib.load(ct_path)
# Get original spacing
original_spacing = ct_nii.header.get_zooms()
# Calculate the orientation transformation based on the CT scan
transform = get_orientation_transform(ct_nii, orientation=('L', 'A', 'S'))
# Apply the transformation to the CT scan data
ct = apply_transform(ct_nii.get_fdata(), transform)
# Apply the transformation to the spacing
reoriented_spacing = apply_spacing_transform(original_spacing, transform)
# Move to tensor and device
ct = torch.from_numpy(ct.copy()).float()
if device != 'cpu':
ct = ct.to(device)
return ct, reoriented_spacing
def window_ct(ct):
"""
ct: torch.Tensor of shape (D, H, W)
"""
ct = ct.clone()
windows={'organs':(-150.0,250.0),
'bone':(-500.0,1500.0),
'skeleton':(400.0,2000.0)}
cts={}
for window in windows:
lower_limit, upper_limit = windows[window]
cts[window] = torch.clamp(ct, min=lower_limit, max=upper_limit)
cts[window] = (cts[window] - lower_limit) / (upper_limit - lower_limit)
return cts
def project_cts(cts, spacing, axis=1):
"""
Projects CT scans along a given axis, normalizes the projection, and resamples
the remaining two dimensions to have 1x1 mm spacing.
Parameters:
cts : dict
Dictionary of CT scans, where each key represents a window (or scan),
and the value is a PyTorch tensor representing the CT scan.
spacing : tuple or list
A tuple containing the voxel spacing in each dimension (x, y, z).
axis : int
The axis along which to sum the image (default is 2).
Returns:
resampled_cts : dict
The dictionary of resampled CT scans with 1x1 mm spacing in the remaining two dimensions.
"""
# Identify the two remaining dimensions after summing
remaining_axis = [item for item in range(3) if item != axis]
# List to store normalized images before resampling
normalized_images = []
# Iterate over the CT windows and process each one
for window in cts:
# Sum along the specified axis
summed_image = torch.sum(cts[window], dim=axis)
# Normalize the image by dividing by the maximum value of the summed image
normalized_image = summed_image.unsqueeze(0).unsqueeze(0) / torch.max(summed_image) # Normalize, and add batch and channel dimensions
normalized_images.append(normalized_image)
# Stack all normalized images for batch processing
stacked_images = torch.cat(normalized_images, dim=0)
# Get the shape of the remaining dimensions (same for all images)
remaining_dims = stacked_images.shape[-2:] # Get height and width
# Calculate the new size (in pixels) to achieve 1x1 mm spacing in the remaining dimensions
remaining_spacing = [spacing[remaining_axis[i]] for i in range(2)]
new_size = [int(remaining_dims[i] * remaining_spacing[i]) for i in range(2)]
print('New size:',new_size)
# Resample using bilinear interpolation to 1x1 mm spacing
resampled_images = F.interpolate(stacked_images, size=new_size, mode='bilinear', align_corners=False)
# Squeeze the batch dimension and store the resampled images in the result dictionary
resampled_cts = {window: resampled_images[i].squeeze(0).squeeze(0) for i, window in enumerate(cts)}
return resampled_cts
def clahe_n_gamma(ct, clip_limit=2.0, tile_grid_size=(8, 8), gamma=0.3, apply_clahe=False, apply_gamma=True, threshold = 0.03):
"""
Apply CLAHE and gamma correction to a CT scan normalized between 0 and 1.
Args:
ct (torch.Tensor): Input tensor of shape (H, W) with values between 0 and 1.
clip_limit (float): Threshold for contrast limiting.
tile_grid_size (tuple): Size of grid for histogram equalization.
gamma (float): Gamma correction parameter.
Returns:
torch.Tensor: Processed image tensor with values between 0 and 1.
"""
if apply_clahe:
# Convert the PyTorch tensor to a NumPy array and scale to [0, 255]
ct_np = (ct.cpu().numpy() * 255).astype(np.uint8)
# Create a CLAHE object with the desired parameters
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
# Apply CLAHE to the image
clahe_image_np = clahe.apply(ct_np)
# Convert the processed image back to a PyTorch tensor and scale to [0, 1]
ct = torch.from_numpy(clahe_image_np.astype(np.float32) / 255.0)
ct = (ct-ct.min())/(ct.max()-ct.min())
# Apply threshold to preserve background
ct[ct < threshold] = 0
if apply_gamma:
ct = torch.pow(ct, gamma)
return ct
def load_n_project_ct(pid, datapath, ct_path,axis=1,save=False,save_path=None,device='cpu'):
#start=time.time()
ct,spacing=load_ct(pid, datapath, ct_path, device=device)
print('Spacing:',spacing)
print('Shape of CT:',ct.shape)
#print('time to load:',time.time()-start)
#start=time.time()
cts=window_ct(ct)
#print('time to window:',time.time()-start)
#start=time.time()
cts=project_cts(cts, spacing, axis=axis)
#print('time to project:',time.time()-start)
#start=time.time()
cts['skeleton']=clahe_n_gamma(cts['skeleton'],clip_limit=5, tile_grid_size=(8, 8), gamma=0.6, apply_clahe=True, apply_gamma=True, threshold=0.01)
#print('time to clahe:',time.time()-start)
#start=time.time()
if save:
for window in cts:
projection=cts[window].unsqueeze(-1).repeat(1,1,3) * 255.0
# Rotate the projection by 90 degrees counter-clockwise
projection = torch.rot90(projection, k=1, dims=(0, 1))
projection_np = projection.detach().cpu().numpy()
filename = f"{pid}_ct_window_{window}_axis_{axis}.png"
filepath = os.path.join(save_path, filename) if save_path else filename
cv2.imwrite(filepath, projection_np)
#print('time to save:',time.time()-start)
return cts
def load_mask(pid, organ, datapath, mask_path,device='cuda'):
# Load the CT scan
if mask_path is None:
# Try the 'predictions' path first
mask_path = os.path.join(datapath, pid, 'predictions', organ + '.nii.gz')
# If the file doesn't exist in 'predictions', check 'segmentations'
if not os.path.exists(mask_path):
mask_path = os.path.join(datapath, pid, 'segmentations', organ + '.nii.gz')
mask_nii = nib.load(mask_path)
# Calculate the orientation transformation based on the CT scan
transform = get_orientation_transform(mask_nii, orientation=('L', 'A', 'S'))
# Apply the transformation to the CT scan data
#start=time.time()
mask = apply_transform(np.asanyarray(mask_nii.dataobj).astype(bool), transform)
#print('time to reorient:',time.time()-start)
#start=time.time()
mask=torch.from_numpy(mask.copy()).float()
if device!='cpu':
mask=mask.to(device)
#print('time to move to device:',time.time()-start)
return mask
def load_all_masks(pid, datapath, device='cuda',organs=['spleen','stomach','gall_bladder','liver']):
# Try the first path
mask_path = os.path.join(datapath, pid, 'segmentations')
# Check if the path exists, if not, use the fallback
if not os.path.exists(mask_path):
mask_path = os.path.join(datapath, pid, 'predictions')
if organs is None:
organs=[organ[:-len('.nii.gz')] for organ in os.listdir(mask_path)]
masks=[]
for pth in organs:
mask=load_mask(pid, pth, datapath, None,device=device)
masks.append(mask)
masks=torch.stack(masks,0)
return masks,organs
def project_masks(masks, axis=1,th=0.5):
"""
Projects masks along a given axis and resamples the remaining two dimensions to have 1x1 mm spacing.
Parameters:
masks : torch.Tensor
A tensor of shape (N, D, H, W) containing the masks for each organ.
axis : int
The axis along which to sum the image (default is 1).
"""
# Sum along the specified axis
summed_masks = torch.sum(masks, dim=axis+1)#accounts for batching
# Normalize the masks by dividing by the maximum value of the summed masks
organ_projection = summed_masks / (summed_masks.amax(dim=(-1, -2), keepdim=True) + 1e-8) # Normalize
# Apply threshold if specified
if th > 0:
organ_projection = torch.where(organ_projection > 0,
organ_projection / (1 / (1 - th)) + th,
torch.tensor(0.0).type_as(organ_projection))
return organ_projection
def resize_masks(masks, size):
masks=F.interpolate(masks, size=size, mode='nearest')
return masks
def load_n_project_masks(pid, datapath, size=None, device='cuda',axis=1,th=0.5,save=False,save_path=None,organs=None):
#start=time.time()
masks,organs=load_all_masks(pid, datapath, device=device,organs=organs)
#print('time to load:',time.time()-start)
#start=time.time()
masks=project_masks(masks, axis=axis,th=th)
#print('time to project:',time.time()-start)
#start=time.time()
if size is not None:
masks=F.interpolate(masks.unsqueeze(1), size=size, mode='nearest').squeeze(1)
if save:
for i,organ in enumerate(organs):
projection=masks[i] * 255.0
# Rotate the projection by 90 degrees counter-clockwise
projection = torch.rot90(projection, k=1, dims=(0, 1))
projection_np = projection.detach().cpu().numpy()
filename = f"{pid}_mask_axis_{axis}.png"
filepath = os.path.join(save_path, filename) if save_path else filename
cv2.imwrite(filepath, projection_np)
#print('time to save:',time.time()-start)
return masks,organs
def overlap_ct_and_masks(cts, masks, organs,device='cpu'):
"""
Overlay CT scans and masks for different organs and save the resulting images.
Parameters:
cts : dict
Dictionary of CT scans, where each key represents a window (or scan),
and the value is a PyTorch tensor representing the CT scan.
masks : torch.Tensor
A tensor of shape (N, D, H, W) containing the masks for each organ.
organs : list
A list of organ names corresponding to the masks.
device : str
The device to run the computations on ('cuda:0' or 'cpu').
"""
overlays={}
# Iterate over the CT windows and process each one
for window in cts:
# Get the CT scan for the current window
ct = cts[window]
ov={}
# Iterate over the organs and overlay the CT scan with the masks
for i, organ in enumerate(organs):
# Get the mask for the current organ
mask = masks[i]
mask [mask > 0.5] = 1
mask [mask <= 0.5] = 0
mask = mask.bool()
if 'cuda' in device:
mask = mask.to(device)
# Overlay the CT scan with the mask
overlay=ct.clone().unsqueeze(0).repeat(3,1,1)
if 'cuda' in device:
overlay=overlay.to(device)
overlay[1][mask] = 0.0
overlay[2][mask] = 0.0
if window=='skeleton':
overlay[0][mask] += 0.5
overlay=torch.clamp(overlay,0,1)
ov[organ]=overlay
overlays[window]=ov
return overlays
def project_ct_and_masks(pid, ct_pth, mask_pth, device='cuda',axis=1,th=0.5,save=False,save_path=None,organs=None):
if not os.path.exists(os.path.join(save_path, pid,f"{pid}_ct_window_bone_axis_{axis}.png")):
#start=time.time()
cts=load_n_project_ct(pid, ct_pth, ct_path=None,axis=axis,save=save,save_path=save_path,device=device)
#print('time to load and project ct:',time.time()-start)
else:
cts={}
for window in ['organs','bone','skeleton']:
filename = f"{pid}_ct_window_{window}_axis_{axis}.png"
filepath = os.path.join(save_path, pid, filename) if save_path else filename
cts[window]=torch.rot90(torch.from_numpy(cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)/255.0).float(), k=-1, dims=(0, 1))
if device!='cpu':
cts[window]=cts[window].to(device)
print('ct projection loaded from '+f"{pid}_ct_window_{window}_axis_{axis}.png")
#start=time.time()
masks,organs=load_n_project_masks(pid, mask_pth, size=cts['organs'].shape[-2:], device=device,axis=axis,th=th,save=save,save_path=save_path,organs=organs)
#print('time to load and project masks:',time.time()-start)
if 'cuda' in device:
masks=masks.to(device)
overlay=overlap_ct_and_masks(cts, masks, organs,device=device)
if save:
for window in overlay:
for organ in overlay[window]:
ov=overlay[window][organ]
ov *= 255.0
# Rotate the projection by 90 degrees counter-clockwise
ov = torch.rot90(ov, k=1, dims=(-2, -1))
ov = ov.permute(1, 2, 0)
ov=ov.detach().cpu().numpy()
ov = cv2.cvtColor(ov, cv2.COLOR_RGB2BGR)
filename = pid+'_overlay_window_'+window+'_axis_'+str(axis)+'_'+organ+'.png'
filepath = os.path.join(save_path, filename) if save_path else filename
cv2.imwrite(filepath, ov)
return cts,masks,organs
def project_files_standard(ct_pth, mask_pth, destin, organ, file_list=None, axis=1,device='cpu',skip_existing=True):
#no multiprocessing
if file_list is None:
file_list=[f for f in file_list if f in os.listdir(mask_pth)]
for pid in file_list:
os.makedirs(os.path.join(destin,pid), exist_ok=True)
if skip_existing and os.path.exists(os.path.join(destin,pid,pid+'_overlay_window_bone_axis_'+str(axis)+'_'+organ+'.png')) \
and os.path.exists(os.path.join(destin,pid,pid+'_overlay_window_organs_axis_'+str(axis)+'_'+organ+'.png')) \
and os.path.exists(os.path.join(destin,pid,pid+'_ct_window_bone_axis_'+str(axis)+'.png')) \
and os.path.exists(os.path.join(destin,pid,pid+'_overlay_window_skeleton_axis_'+str(axis)+'_'+organ+'.png')) \
and os.path.exists(os.path.join(destin,pid,pid+'_ct_window_skeleton_axis_'+str(axis)+'.png')):
print(f'Skipping {pid}, already exists')
continue
print(f'Projecting {pid}')
start_proj=time.time()
project_ct_and_masks(pid, ct_pth=ct_pth, mask_pth=mask_pth, device=device,axis=axis,th=0.5,save=True,save_path=os.path.join(destin,pid),organs=[organ])
print(f'Projected {pid}')
print('time to project:',time.time()-start_proj)
print('')
def process_single_file(pid, ct_pth, mask_pth, destin, organ, axis, device, skip_existing):
os.makedirs(os.path.join(destin, pid), exist_ok=True)
# Check if the necessary files already exist
#print(f'Checking if file exists in {os.path.join(destin,pid, pid + "_overlay_window_bone_axis_" + str(axis) + "_" + organ + ".png")}')
if skip_existing and os.path.exists(os.path.join(destin,pid,pid+'_overlay_window_bone_axis_'+str(axis)+'_'+organ+'.png')) \
and os.path.exists(os.path.join(destin,pid,pid+'_overlay_window_organs_axis_'+str(axis)+'_'+organ+'.png')) \
and os.path.exists(os.path.join(destin,pid,pid+'_overlay_window_skeleton_axis_'+str(axis)+'_'+organ+'.png')) \
and os.path.exists(os.path.join(destin,pid,pid+'_ct_window_skeleton_axis_'+str(axis)+'.png')) \
and os.path.exists(os.path.join(destin,pid,pid+'_ct_window_organs_axis_'+str(axis)+'.png')) \
and os.path.exists(os.path.join(destin,pid,pid+'_ct_window_bone_axis_'+str(axis)+'.png')):
print(f'Skipping {pid}, already exists')
return
if not os.path.isdir(os.path.join(ct_pth, pid)):
print(f'Patient {pid} not found')
return
if 'ct.nii.gz' not in os.listdir(os.path.join(ct_pth, pid)):
print(f'CT not found for {pid}')
return
if not os.path.isdir(os.path.join(mask_pth, pid)):
print(f'Mask {pid} not found')
return
# Process the file
print(f'Projecting {pid}')
start_proj = time.time()
# Call the function to project CT and masks (assuming this function is defined elsewhere)
project_ct_and_masks(pid, ct_pth=ct_pth, mask_pth=mask_pth, device=device, axis=axis, th=0.5, save=True, save_path=os.path.join(destin, pid), organs=[organ])
print(f'Projected {mask_pth}/{pid} and saved in {os.path.join(destin, pid)}')
print('Time to project:', time.time() - start_proj)
print('')
# Main function that uses multiprocessing to parallelize the task
def project_files(pth=None, destin=None, organ='liver', file_list=None, axis=1, device='cpu', skip_existing=True, num_processes=10,
ct_pth=None, mask_pth=None):
if ct_pth is None:
ct_pth = pth
if mask_pth is None:
mask_pth = pth
if 'cuda' in device:
project_files_standard(ct_pth=ct_pth, mask_pth=mask_pth, destin=destin, organ=organ,
file_list=file_list, axis=axis,device=device,skip_existing=skip_existing)
return
if file_list is None:
file_list = [f for f in os.listdir(mask_pth) \
if (os.path.isfile(os.path.join(mask_pth, f,'segmentations',organ+'.nii.gz')) \
or os.path.isfile(os.path.join(mask_pth, f,'predictions',organ+'.nii.gz')))] # Load all files in the directory if no list is provided
file_list = [f for f in file_list if 'ct.nii.gz' in os.listdir(os.path.join(ct_pth, f))] # Filter out files without a CT scan
# Create a pool of workers for parallel processing
with multiprocessing.Pool(processes=num_processes) as pool:
# Prepare arguments for the helper function
pool.starmap(
process_single_file,
[(pid, ct_pth, mask_pth, destin, organ, axis, device, skip_existing) for pid in file_list]
)
def overlay_projection(pid, organ, datapath,save_path,th=0.5,
mask_only=False,ct_only=False,
clahe=False,he=False,window='organs',
ct_path=None, mask_path=None):
ct, mask=load_ct_and_mask(pid, organ, datapath,
ct_path=ct_path, mask_path=mask_path)
ct_projections=plot_organ_projection_3_axis([ct], organ, pid, th=0,
ct=True, save=False,
window=window)
if clahe or he:
for i in range(len(ct_projections)):
img=ct_projections[i]
flag=False
#print('max img:',np.max(img))
#print('min img:',np.min(img))
if np.max(img)>1:
img=img/255
flag=True
if clahe:
ct_projections[i] = exposure.equalize_adapthist(img, clip_limit=0.01,
nbins=256,
kernel_size=(16,16))
if he:
ct_projections[i] = exposure.equalize_hist(img)
if flag:
ct_projections[i]=ct_projections[i]*255
mask_projections=plot_organ_projection_3_axis([mask], organ, pid, th=th,
ct=False, save=False)
if mask_only:
ct_projections=[0*proj for proj in ct_projections]
if ct_only:
mask_projections=[0*proj for proj in mask_projections]
for i in range(len(ct_projections)):
ct_proj=ct_projections[i]
mask_proj=mask_projections[i]
print('max ct proj:',np.max(ct_proj))
print('min ct proj:',np.min(ct_proj))
print('max mask proj:',np.max(mask_proj))
print('min mask proj:',np.min(mask_proj))
ct_proj = np.expand_dims(ct_proj, axis=-1)
ct_proj = np.tile(ct_proj, (1, 1, 3))
if not mask_only:
overlay = ct_proj
ct_proj[:,:,0] = ct_proj[:,:,0]-mask_proj
ct_proj[:,:,1] = ct_proj[:,:,1]-mask_proj
else:
overlay = np.expand_dims(mask_proj, axis=-1)
overlay = np.tile(overlay, (1, 1, 3))
overlay[:,:,0] = 0
overlay[:,:,1] = 0
#overlay = overlay.astype(np.uint8)
print('overlay shape:',overlay.shape)
print('max overlay:',np.max(overlay))
print('min overlay:',np.min(overlay))
print('shape of overlay:',overlay.shape)
if not os.path.exists(save_path):
os.makedirs(save_path)
name='_overlay'
if mask_only:
name='_mask'
if ct_only:
name='_ct'
cv2.imwrite(os.path.join(save_path, pid + name + '_axis_'+str(i)+'.png'),
overlay)
print('Organ projection of ' + save_path + ' for patient ' + pid + \
' is saved to ' + os.path.join(save_path, pid + '.png'))
def apply_clahe_to_tensor(image_tensor, clip_limit=2.0, tile_grid_size=(8, 8),apply_erosion=True,erosion_kernel_size=9):
"""
Apply CLAHE to a PyTorch tensor image normalized between 0 and 1.
Args:
image_tensor (torch.Tensor): Input tensor of shape (C, H, W) or (H, W) with values between 0 and 1.
clip_limit (float): Threshold for contrast limiting.
tile_grid_size (tuple): Size of grid for histogram equalization.
Returns:
torch.Tensor: Processed image tensor with values between 0 and 1.
"""
# Ensure the image is a 2D grayscale tensor (H, W) or (1, H, W)
if len(image_tensor.shape) == 3:
image_tensor = image_tensor.squeeze(0) # Remove channel dimension if it exists
# Convert the PyTorch tensor to a NumPy array and scale to [0, 255]
image_np = (image_tensor.cpu().numpy() * 255).astype(np.uint8)
# Create a CLAHE object with the desired parameters
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
# Apply CLAHE to the image
clahe_image_np = clahe.apply(image_np)
# Convert the processed image back to a PyTorch tensor and scale to [0, 1]
clahe_image_tensor = torch.from_numpy(clahe_image_np.astype(np.float32) / 255.0)
# Add back the channel dimension if it was initially present
if len(image_tensor.shape) == 2: # if the original tensor was 3D
clahe_image_tensor = clahe_image_tensor.unsqueeze(0)
if apply_erosion:
_, binary_image = cv2.threshold(clahe_image_np, 0, 1, cv2.THRESH_BINARY)
kernel = np.ones((erosion_kernel_size, erosion_kernel_size), np.uint8) # Create a square kernel
eroded_image = cv2.erode(binary_image, kernel, iterations=1)
binary_image = eroded_image
binary_tensor = torch.tensor(binary_image)
if len(image_tensor.shape) == 2: # if the original tensor was 3D
binary_tensor = binary_tensor.unsqueeze(0)
clahe_image_tensor = clahe_image_tensor * binary_tensor
return clahe_image_tensor
def plot_organ_projection_cuda(list_of_array, organ_name, pid, axis=1,
pngpath=None, th=0.5, ct=False, save=True,
window='organs'):
# Stack tensors along a new dimension
stacked_x = torch.stack(list_of_array, dim=0) # Shape: (N, D1, D2, D3)
# Apply windowing if ct is True
if ct:
if window == 'organs':
upper_limit = 250.0
lower_limit = -150.0
offset = 150.0
divisor = 400.0
elif window == 'bone':
upper_limit = 1500.0
lower_limit = -500.0
offset = 500.0
divisor = 2000.0
elif window == 'skeleton':
upper_limit = 2000.0
lower_limit = 400.0
offset = -400.0
divisor = 1600.0
else:
raise ValueError('Window should be "organs" "skeleton" or "bone"')
# Apply the windowing to the stacked tensor
stacked_x = torch.clamp(stacked_x, min=lower_limit, max=upper_limit)
stacked_x = (stacked_x + offset) / divisor
# Sum over the specified axis (adjusted for batch dimension)
organ_projection = torch.sum(stacked_x, dim=axis+1)
# Normalize each projection in the batch
max_vals = organ_projection.view(organ_projection.size(0), -1).max(dim=1)[0] + 1e-8
organ_projection = organ_projection / max_vals[:, None, None]
# Sum the normalized projections across the batch dimension
projection = torch.sum(organ_projection, dim=0)
# Normalize the final projection
projection = projection / (torch.max(projection) + 1e-8)
# Apply threshold if specified
if th > 0:
projection = torch.where(projection > 0,
projection / (1 / (1 - th)) + th,
torch.tensor(0.0).type_as(projection))
if window == 'skeleton' and ct:
#contrast enhancement
#projection[projection > 0]+=0.05
projection=apply_clahe_to_tensor(projection.unsqueeze(0),clip_limit=5,apply_erosion=False).squeeze(0)
gamma=0.3
projection = torch.pow(projection, gamma)
projection = (projection-projection.min())/(projection.max()-projection.min())
# Apply threshold to preserve background
threshold = 0.03 # Adjust as needed
projection[projection < threshold] = 0
projection = torch.clamp(projection, min=0, max=1)
# Scale to 255 for image representation
projection *= 255.0
# Rotate the projection by 90 degrees counter-clockwise
projection = torch.rot90(projection, k=1, dims=(0, 1))
# Save the projection if required
if save:
projection_np = projection.detach().cpu().numpy()
if pngpath is not None and not os.path.exists(pngpath):
os.makedirs(pngpath)
filename = f"{pid}_axis_{axis}.png"
filepath = os.path.join(pngpath, filename) if pngpath else filename
cv2.imwrite(filepath, projection_np)
print(f'Organ projection of {organ_name} for patient {pid} is saved to {filepath}')
return projection
def overlay_projection_fast(pid, organ, datapath, save_path, th=0.5,
mask_only=False, ct_only=False, window='organs',
ct_path=None, mask_path=None, axis=1, device='cuda:0',
precision=32):
"""
Generate and save overlay projections of CT and mask images.
Parameters:
- pid (str): Patient ID.
- organ (str): Organ name.
- datapath (str): Path to the data directory.
- save_path (str): Directory to save the overlay images.
- th (float): Threshold for mask projection.
- mask_only (bool): If True, only the mask is projected.
- ct_only (bool): If True, only the CT is projected.
- window (str): Windowing option for CT images ('organs' or 'bone').
- ct_path (str): Path to the CT image file.
- mask_path (str): Path to the mask image file.
- axis (int): Axis along which to project (0, 1, or 2).
- device (str): Device to run the computations on ('cuda:0' or 'cpu').
Returns:
- None
"""
print(f'Projecting {organ} for patient {pid} from {datapath}')
# Load CT and mask images
ct, mask = load_ct_and_mask(pid, organ, datapath,
ct_path=ct_path, mask_path=mask_path)
# Convert to torch tensors and move to device
# Ensure arrays have positive strides by making a copy
ct = ct.copy()
mask = mask.copy()
ct = torch.from_numpy(ct).to(device).float()
mask = torch.from_numpy(mask).to(device).float()
# Generate projections
if not mask_only:
ct_projection = plot_organ_projection_cuda([ct], organ, pid, axis=axis,
th=0, ct=True, save=False,
window=window)
if not ct_only:
mask_projection = plot_organ_projection_cuda([mask], organ, pid, axis=axis,
th=th, ct=False, save=False)
# Handle cases where only CT or only mask is desired
if mask_only:
# Create a zero projection for CT
ct_projection = torch.zeros_like(mask_projection, device=device)
if ct_only:
# Create a zero projection for mask
mask_projection = torch.zeros_like(ct_projection, device=device)
# Ensure projections are on the same device and have the same shape
ct_projection = ct_projection.to(device)
mask_projection = mask_projection.to(device)
# Prepare overlay
if mask_only:
# Overlay only the mask in the blue channel
overlay = torch.stack([
torch.zeros_like(mask_projection), # R channel
torch.zeros_like(mask_projection), # G channel
mask_projection # B channel
], dim=2)
elif ct_only:
# Overlay only the CT in all channels
overlay = ct_projection.unsqueeze(-1).repeat(1, 1, 3)
else:
if window!='skeleton':
overlay = torch.stack([
ct_projection - mask_projection, # B channel
ct_projection - mask_projection, # G channel
ct_projection # R channel
], dim=2)
else:
overlay = torch.stack([
ct_projection - mask_projection, # B channel
ct_projection - mask_projection, # G channel
ct_projection + (mask_projection*0.8) # R channel
], dim=2)
# Clamp values to [0, 255] and convert to uint8
overlay = torch.clamp(overlay, 0, 255).byte()
# Move to CPU and convert to NumPy array
overlay_np = overlay.cpu().numpy()
# Print overlay information
#print('overlay shape:', overlay_np.shape)
#print('max overlay:', overlay_np.max())
#print('min overlay:', overlay_np.min())
# Save the overlay image
if not os.path.exists(save_path):
os.makedirs(save_path)
name = '_overlay'
if mask_only:
name = '_mask'
if ct_only:
name = '_ct'
filename = os.path.join(save_path, f"{pid}{name}_window_{window}_axis_{axis}_{organ}.png")
cv2.imwrite(filename, overlay_np)
print(f'Organ projection of {organ} for patient {pid} is saved to {filename}')
def create_composite_image(pth, organ, axis=1, y1_bone=None, y2_bone=None, y1_organs=None, y2_organs=None,name=''):
if y1_bone is None:
y1_bone = os.path.join(pth, f'y1_bone_overlay_window_bone_axis_{axis}_{organ}.png')
y1_organs = os.path.join(pth, f'y1_organs_overlay_window_organs_axis_{axis}_{organ}.png')
y2_bone = os.path.join(pth, f'y2_bone_overlay_window_bone_axis_{axis}_{organ}.png')
y2_organs = os.path.join(pth, f'y2_organs_overlay_window_organs_axis_{axis}_{organ}.png')
# Load the images
image_paths = [y1_bone, y1_organs, y2_bone, y2_organs]
images = [Image.open(path) for path in image_paths]
# Get image dimensions in pixels
img_width, img_height = images[0].size
# DPI (dots per inch)
dpi = 100 # Adjust as needed
# Calculate subplot size in inches
subplot_width = img_width / dpi
subplot_height = img_height / dpi
# Title font size in points and inches
title_font_size_pts = 12 # Default matplotlib font size
title_font_size_inch = title_font_size_pts / 72 # Convert points to inches
# Desired spacing between subplots (1.5 times title font size)
desired_spacing_inch = 1.5 * title_font_size_inch
# Compute wspace and hspace as fractions of subplot size
wspace = desired_spacing_inch / subplot_width
hspace = desired_spacing_inch / subplot_height
# Total figure size in inches
fig_width = 2 * subplot_width + desired_spacing_inch
fig_height = 2 * subplot_height + desired_spacing_inch
# Create figure and axes
fig, axes = plt.subplots(2, 2, figsize=(fig_width, fig_height), dpi=dpi)
axes = axes.flatten()
# Use your original titles
titles = ["Image 1 - Overlay 1", "Image 2 - Overlay 1", "Image 3 - Overlay 2", "Image 4 - Overlay 2"]