forked from dave3d/dicom2stl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dicomutils.py
executable file
·116 lines (96 loc) · 2.78 KB
/
dicomutils.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#! /usr/bin/env python
#
# Function to load the largest Dicom series in a directory.
#
# It scans the directory recursively search for files with the ".dcm"
# suffix. Note that DICOM fails don't always have that suffix. In
# that case this function will fail.
#
# Written by David T. Chen from the National Library of Medicine, [email protected].
# It is covered by the Apache License, Version 2.0:
# http://www.apache.org/licenses/LICENSE-2.0
#
from __future__ import print_function
import sys
import os
import fnmatch
import zipfile
import SimpleITK as sitk
def scanDirForDicom(dicomdir):
matches = []
dirs = []
for root, dirnames, filenames in os.walk(dicomdir):
for filename in fnmatch.filter(filenames, '*.dcm'):
matches.append(os.path.join(root, filename))
if root not in dirs:
dirs.append(root)
return (matches, dirs)
def getAllSeries(dirs):
isr = sitk.ImageSeriesReader()
seriessets = []
for d in dirs:
series = isr.GetGDCMSeriesIDs(d)
for s in series:
files = isr.GetGDCMSeriesFileNames(d, s)
print(s, d, len(files))
seriessets.append([s, d, files])
return seriessets
def getModality(img):
modality = ""
if (sitk.Version.MinorVersion() > 8) or (sitk.Version.MajorVersion() > 0):
try:
modality = img.GetMetaData("0008|0060")
except:
modality = ""
return modality
# Load the largest Dicom series it finds in a recursive scan of
# a directory. Largest means has the most slices. It also returns
# the modality.
#
def loadLargestSeries(dicomdir):
files, dirs = scanDirForDicom(dicomdir)
seriessets = getAllSeries(dirs)
maxsize = 0
maxindex = -1
count = 0
for ss in seriessets:
size = len(ss[2])
if size > maxsize:
maxsize = size
maxindex = count
count = count + 1
if maxindex < 0:
print("Error: no series found")
return None
isr = sitk.ImageSeriesReader()
ss = seriessets[maxindex]
files = ss[2]
isr.SetFileNames(files)
print("\nLoading series", ss[0], "in directory", ss[1])
img = isr.Execute()
firstslice = sitk.ReadImage(files[0])
modality = getModality(firstslice)
return img, modality
#
# Main (test code)
#
if __name__ == "__main__":
print("")
print("dicomutils.py")
print(sys.argv[1])
# img = loadLargestSeries(sys.argv[1])
# print (img)
# sys.exit(0)
files, dirs = scanDirForDicom(sys.argv[1])
print("")
print("files")
print(files)
print("")
print("dirs")
print(dirs)
print("series")
seriessets = getAllSeries(dirs)
for ss in seriessets:
print(ss[0], " ", ss[1])
print(len(ss[2]))
print("")