[110cc9]: / DATA2NPY / nii2npy.py

Download this file

27 lines (22 with data), 697 Bytes

 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
import numpy as np
import os
import nibabel
N = 82
W = 512
H = 512
path1 = 'TCIA_pancreas_labels-02-05-2017'
path2 = 'labels'
if not os.path.exists(path2):
os.makedirs(path2)
for n in range(N):
volumeID = '{:0>4}'.format(n + 1)
print 'Processing File ' + volumeID
filename1 = 'label' + volumeID + '.nii.gz'
directory1 = os.path.join(path1, filename1)
filename2 = volumeID + '.npy'
file1 = os.path.join(path1, filename1)
data = nibabel.load(file1).get_data().transpose(1, 0, 2)
print ' Data shape is ' + str(data.shape) + ' .'
file2 = os.path.join(path2, filename2)
np.save(file2, data)
print 'File ' + volumeID + ' is saved in ' + file2 + ' .'