a b/Preprocessing Medical Data Pipeline/friedlander.py
1
import nibabel as nib
2
import numpy as np
3
import matplotlib.pyplot as plt
4
from skimage.transform import resize
5
from MSKMulticlass import CreateMasks4MulticlassMSK
6
7
def superimpose_images(image1, image2):
8
    image1 = image1 / np.max(image1)
9
    image2 = image2 / np.max(image2)
10
    alpha = 0.5
11
    superimposed_image = alpha * image1 + (1 - alpha) * image2
12
    return superimposed_image
13
14
def uniform_resizing(images, new_size):
15
    """
16
    Applies uniform resizing to an array of images. ### bicubic_interpolation
17
18
    Parameters:
19
        images (numpy.ndarray): Input array of images with shape (x, 512, 512, 1).
20
        new_size (tuple or int): The desired size of the resized images. If it's an int,
21
                                 the new size will be (new_size, new_size).
22
23
    Returns:
24
        numpy.ndarray: Array of resized images with shape (x, new_height, new_width, 1).
25
    """
26
    x, height, width, _ = images.shape
27
28
    if isinstance(new_size, int):
29
        new_height, new_width = new_size, new_size
30
    elif isinstance(new_size, tuple) and len(new_size) == 2:
31
        new_height, new_width = new_size
32
    else:
33
        raise ValueError("Invalid new_size argument. It should be an int or a tuple of two ints.")
34
35
    resized_images = np.zeros((x, new_height, new_width, 1))
36
37
    for i in range(x):
38
        resized_images[i, ..., 0] = resize(images[i, ..., 0], (new_height, new_width), mode='reflect', order=1)
39
        # resized_images[i, ..., 0] = resize(images[i, ..., 0], (new_height, new_width), mode='constant', preserve_range=True)
40
41
    return resized_images
42
43
44
45
46
47
48
49
# Pranav Change!
50
51
runVisualisationONLY = True
52
53
# Base Directory (Make a new folder!)
54
base_dir = 'D:/MRI - Friedlander'
55
56
# The mask index is the msk_00X number
57
# mask_index = [1] # Does one at a time
58
mask_index = [2,3,4,5] # Does multiple
59
60
# Make the following folders from your base directory
61
# 1)    First make a folder called nnUNet Data 
62
# 2)    From nnUNet folder make: scans, masks, multiclass_masks
63
# 3)    For unprocessed_scans folder load tbe scans as nii.gz using the msk_00X naming convention (ALWAYS back up scans outside of this file it )
64
# 4)    For masks folder look on discord for a reference pic
65
# 5)    For scans folder leave empty for processed out files
66
individual_mask_directory = ('{}/nnUNet Data/masks').format(base_dir)
67
multiclass_mask_output_dir = ('{}/nnUNet Data/multiclass_masks').format(base_dir)
68
scan_dir = ('{}/nnUNet Data/scans').format(base_dir) #output scans directory not unprocessed scans
69
input_scan_dir = ('{}/nnUNet Data/unprocessed_scans').format(base_dir)
70
71
# DONT EDIT!
72
TIBIA_encoding = 1
73
FEMUR_encoding = 2
74
FIBULA_encoding = 3
75
PELVIS_encoding = 4
76
AOIThresholding = True
77
FriedLanderDataset = True
78
79
if (runVisualisationONLY == False):
80
    print('Multi-Class Segmentation Task Data Preparation!')
81
    for i in range (len(mask_index)):
82
        CreateMasks4MulticlassMSK(input_scan_dir, scan_dir, individual_mask_directory, mask_index[i], TIBIA_encoding, FEMUR_encoding, FIBULA_encoding, PELVIS_encoding, multiclass_mask_output_dir, AOIThresholding, FriedLanderDataset)
83
84
85
# Visualisations
86
87
# Pranav Change!
88
slice = 650
89
msk_index = 3
90
91
img = nib.load(('{}/msk_00{}.nii.gz').format(scan_dir, msk_index))
92
img_data = img.get_fdata()
93
print('Training Scan Shape: ', img_data.shape)
94
95
mask = nib.load(('{}/msk_00{}.nii.gz').format(multiclass_mask_output_dir, msk_index))
96
mask_data = mask.get_fdata()
97
print('Training Mask Shape: ', mask_data.shape)
98
99
superimposed_data = superimpose_images(img_data[slice, :, :, 0], mask_data[slice, :, :, 0])
100
101
plt.imshow(img_data[slice, :, :, 0], cmap='gray')
102
plt.axis('off')
103
plt.show()
104
105
plt.imshow(mask_data[slice, :, :, 0], cmap='gray')
106
plt.axis('off')
107
plt.show()
108
109
plt.imshow(superimposed_data, cmap='gray')
110
plt.axis('off')
111
plt.show()