-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess_Chaos_MRI.py
37 lines (27 loc) · 1.31 KB
/
preprocess_Chaos_MRI.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
import os
import json
import numpy as np
import dicom2nifti
import SimpleITK as sitk
import cv2
data_dir = '../Data/CHAOS_Train_Sets/Train_Sets/MR/'
indexes = os.listdir(data_dir)
dict_lists = []
for index in indexes:
img_dir = os.path.join(data_dir,index, 'T2SPIR/DICOM_anon')
dicom2nifti.dicom_series_to_nifti(img_dir, os.path.join(data_dir,index, 'T2SPIR/DICOM_anon', index+'img.nii.gz'), reorient_nifti=True)
img = sitk.ReadImage(os.path.join(data_dir,index, 'T2SPIR/DICOM_anon', index+'img.nii.gz'))
labels = os.listdir(os.path.join(data_dir,index, 'T2SPIR/Ground'))
labels.sort()
label_np = []
for label in labels:
if 'png' in label:
label_np.append(cv2.flip(cv2.rotate(cv2.imread(os.path.join(data_dir,index, 'T2SPIR/Ground',label)),cv2.ROTATE_180),1)[:,:,0]/63)
label_np = np.stack(label_np, axis=2).transpose([2,0,1])
nii_label = sitk.GetImageFromArray(label_np)
nii_label.CopyInformation(img)
sitk.WriteImage(nii_label, os.path.join(data_dir,index, 'T2SPIR/Ground', index+'label.nii.gz'))
dict_lists.append({'image': os.path.join(data_dir,index, 'T2SPIR/DICOM_anon', index+'img.nii.gz'),
'label': os.path.join(data_dir,index, 'T2SPIR/Ground', index+'label.nii.gz')})
with open('CHAOS_MRI', 'w') as fout:
json.dump(dict_lists, fout)