-
Notifications
You must be signed in to change notification settings - Fork 45
/
dicom2Nii.py
81 lines (61 loc) · 2.48 KB
/
dicom2Nii.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
'''
05/02, at Chapel Hill
Dong
convert dicom series to nifti format
'''
import numpy
import SimpleITK as sitk
import os
from doctest import SKIP
class ScanFile(object):
def __init__(self,directory,prefix=None,postfix=None):
self.directory=directory
self.prefix=prefix
self.postfix=postfix
def scan_files(self):
files_list=[]
for dirpath,dirnames,filenames in os.walk(self.directory):
'''''
dirpath is a string, the path to the directory.
dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
'''
for special_file in filenames:
if self.postfix:
special_file.endswith(self.postfix)
files_list.append(os.path.join(dirpath,special_file))
elif self.prefix:
special_file.startswith(self.prefix)
files_list.append(os.path.join(dirpath,special_file))
else:
files_list.append(os.path.join(dirpath,special_file))
return files_list
def scan_subdir(self):
subdir_list=[]
for dirpath,dirnames,files in os.walk(self.directory):
subdir_list.append(dirpath)
return subdir_list
def main():
path='/home/dongnie/warehouse/pelvicSeg/newData/pelvic_0118/'
subpath='atkinson_lafayette'
outfn=subpath+'.nii.gz'
inputdir=path+subpath
scan=ScanFile(path)
subdirs=scan.scan_subdir()
for subdir in subdirs:
if subdir==path or subdir=='..':
continue
print 'subdir is, ',subdir
ss=subdir.split('/')
print 'ss is, ',ss, 'and s7 is, ',ss[7]
outfn=ss[7]+'.nii.gz'
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(subdir)
reader.SetFileNames(dicom_names)
image = reader.Execute()
size = image.GetSize()
print( "Image size:", size[0], size[1], size[2] )
print( "Writing image:", outfn)
sitk.WriteImage(image,outfn)
if __name__ == '__main__':
main()