-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_nifti_image.py
87 lines (64 loc) · 3.32 KB
/
show_nifti_image.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
import nilearn
import nibabel as nib
from nilearn import plotting, image
from helper import compute_scores
import numpy as np
from nilearn.image import resample_to_img
from matplotlib.colors import ListedColormap
def expand_dim_of_nifti_image(nifti_image):
new_data = np.expand_dims(nifti_image.get_data(), axis=-1)
return nib.Nifti1Image(new_data, None, nifti_image.get_header())
def show_images():
# prediction_file_location = "D:\\Master\\predicted\\predicted\\oasis\\CNNAllFinal_pred__OAS1_0012_MR1_mpr_n4_anon_111_t88_gfc_masked.nii.gz"
# prediction_file_location = "D:\\Master\\predicted\\predictions\\"
prediction_file_location = "D:\\Master\\predicted\\predictions\\volume-0_processed_pred_Segm.nii.gz"
data_file_location = "D:\\MRISCANS\\LITS\\LITSTrainingData\\"
data_file_name = "volume-0_processed.nii.gz"
label_file_location = "D:\\MRISCANS\\LITS\\LITSTrainingLabels\\segmentation-0_processed.nii.gz"
arcSaveName = "DeepMedic"
prediction = nib.load(prediction_file_location)
data = nib.load(data_file_location + data_file_name)
label = nib.load(label_file_location)
# prediction_plot = plotting.plot_roi(prediction, bg_img=data, cmap='Greys')
# label_plot = plotting.plot_roi(label, bg_img=data, cmap='Greys')
pred = resample_to_img(prediction, label, interpolation='linear').get_data()
error_map = np.zeros(pred.shape)
lab = label.get_data()
error_map = np.zeros(pred.shape)
error_map += np.array((pred != lab), dtype=float) * 2
error_map += lab
# Don't know why I have to have these random colours in the beginning.
cmap = ListedColormap(['green', 'green', 'red', 'red', 'red', 'green', 'red', 'cyan'], 'indexed')
print("jjjj")
nif = nib.Nifti1Image(error_map, label.affine)
# label_and_prediction_plot = plotting.plot_stat_map(nif, bg_img=label, cmap=cmap, cut_coords=(10, 10, 10))
label_and_prediction_plot = plotting.plot_roi(nif, bg_img=data, cmap=cmap, colorbar=False, cut_coords=(-4, -71, 10))
print("jjjj")
data_file_name = data_file_name.split('.')[0]
plotting.show()
# prediction_plot.savefig(arcSaveName + data_file_name + "Prediction")
# label_plot.savefig(arcSaveName + data_file_name + "Label")
# label_and_prediction_plot.savefig(arcSaveName + data_file_name + "LabelAndPrediction")
def calculate_percentage_true():
# data = nib.load("D:\\MRISCANS\\LITS\\LITSTrainingLabels\\segmentation-0_processed.nii.gz").get_data()
# data = nib.load("D:\MRISCANS\\la\\OAS1_0001_MR1_mpr_n4_anon_111_t88_masked_gfc_fseg_processed.nii.gz").get_data()
elems = data.size
liver = np.count_nonzero(data)
percentage_liver = float(liver)/float(elems)
print(percentage_liver * 100)
def show_image_and_label():
data_file_location = "D:\\MRISCANS\\StOlavs\\data\\coreg_T13.nii"
label_file_location = "D:\\MRISCANS\\StOlavs\\labels\\icvMaskAuto_2013_T13.nii"
data = nib.load(data_file_location)
label = nib.load(label_file_location)
label_and_prediction_plot = plotting.plot_roi(label, bg_img=data)
plotting.show()
def show_image():
data_file_location = "D:\\MRISCANS\\StOlavs\\data\\coreg_T13.nii"
data = nib.load(data_file_location)
label_and_prediction_plot = plotting.plot_anat(data)
plotting.show()
def main():
# calculate_percentage_true()
show_image_and_label()
main()