|
a |
|
b/Extracting_Planes.py |
|
|
1 |
from nibabel import load |
|
|
2 |
from numpy import ndarray, array, int32, uint8 |
|
|
3 |
from time import time |
|
|
4 |
|
|
|
5 |
def Convert_To_Graph(image: ndarray, label: ndarray) -> tuple[ndarray, ndarray]: |
|
|
6 |
r""" |
|
|
7 |
Arguments: |
|
|
8 |
image (numpy.ndarray): Source coronary-CT image. |
|
|
9 |
label (numpy.ndarray): Ground truth segmentation. |
|
|
10 |
|
|
|
11 |
Returns: |
|
|
12 |
out (tuple[numpy.ndarray, numpy.ndarray]): Source coronary-CT image and ground truth segmentation as graphs. |
|
|
13 |
""" |
|
|
14 |
# start = time() |
|
|
15 |
img = array(image, dtype = int32) |
|
|
16 |
lab = array(label, dtype = uint8) |
|
|
17 |
img[1::2, :] = image[1::2, ::-1] |
|
|
18 |
lab[1::2, :] = label[1::2, ::-1] |
|
|
19 |
|
|
|
20 |
img = img.flatten() |
|
|
21 |
img = img.reshape((img.shape[0], 1)) |
|
|
22 |
|
|
|
23 |
lab[lab > 7] = 0 |
|
|
24 |
lab = lab.flatten() |
|
|
25 |
# print('Convert_To_Graph time: ', time() - start) |
|
|
26 |
return (img, lab) |
|
|
27 |
|
|
|
28 |
def Extract_And_Convert(path_to_image: str, path_to_label: str, |
|
|
29 |
plane_type: str, plane_index: int) \ |
|
|
30 |
-> tuple[ndarray, ndarray]: |
|
|
31 |
r""" |
|
|
32 |
Arguments: |
|
|
33 |
path_to_image (str): Full path to the coronary-CT .nii.gz file. |
|
|
34 |
path_to_label (str): Full path to the segmentation label .nii.gz file. |
|
|
35 |
plane_type (str): One-character string with a value of 'A', 'C', or 'S'. |
|
|
36 |
plane_index (int): Index of plane to be extracted from the image and label. |
|
|
37 |
|
|
|
38 |
Returns: |
|
|
39 |
out (tuple[numpy.ndarray, numpy.ndarray]): Source coronary-CT image and ground truth segmentation as graphs. |
|
|
40 |
""" |
|
|
41 |
# start = time() |
|
|
42 |
match plane_type: |
|
|
43 |
case 'A': # Axial plane |
|
|
44 |
image = load(path_to_image).dataobj[:, :, plane_index] |
|
|
45 |
label = load(path_to_label).dataobj[:, :, plane_index] |
|
|
46 |
case 'C': # Coronal plane |
|
|
47 |
image = load(path_to_image).dataobj[:, plane_index, :] |
|
|
48 |
label = load(path_to_label).dataobj[:, plane_index, :] |
|
|
49 |
case 'S': # Sagittal plane |
|
|
50 |
image = load(path_to_image).dataobj[plane_index, :, :] |
|
|
51 |
label = load(path_to_label).dataobj[plane_index, :, :] |
|
|
52 |
# print('Nibabel loading time: ', time() - start) |
|
|
53 |
return Convert_To_Graph(image, label) |