-
Notifications
You must be signed in to change notification settings - Fork 53
/
helpers_dicom.py
191 lines (162 loc) · 4.64 KB
/
helpers_dicom.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
__author__ = 'Julian'
import dicom
import numpy
class DicomWrapper:
def __init__(self, file_dir, file_name):
self.raw_file = dicom.read_file(file_dir + file_name)
self.file_name = file_name
loc1 = self.get_location()
loc2 = self.slice_location
# print str(loc2) + " - " + str(loc1)
def get_value(self, name):
res = self.raw_file.data_element(name).value
return res
@property
def columns(self):
res = self.get_value("Columns")
return res
@property
def rows(self):
res = self.get_value("Rows")
return res
@property
def spacing(self):
res = self.get_value("PixelSpacing")
return res
@property
def slice_location(self):
return self.get_value("SliceLocation")
@property
def create_time(self):
return str(int(round(float(self.get_value("InstanceCreationTime")))) / 10).rjust(5, '0')
@property
def slice_thickness(self):
return self.get_value("SliceThickness")
@property
def sequence_name(self):
return self.get_value("SequenceName")
@property
def image_position(self):
return self.get_value("ImagePositionPatient")
@property
def series_number(self):
return self.get_value("SeriesNumber")
@property
def series_time(self):
return self.get_value("SeriesTime")
@property
def patient_id(self):
return self.get_value("PatientID")
@property
def series_description(self):
return self.get_value("SeriesDescription")
@property
def image_orientation_patient(self):
return self.get_value("ImageOrientationPatient")
@property
def image_position_patient(self):
return self.get_value("ImagePositionPatient")
@property
def flip_angle(self):
return self.get_value("FlipAngle")
@property
def instance_number(self):
return self.get_value("InstanceNumber")
@property
def in_plane_encoding_direction(self):
return self.get_value("InPlanePhaseEncodingDirection")
def get_location(self):
image_center2d = self.spacing * (numpy.array([self.columns, self.rows]) - numpy.ones(2)) / 2.
image_center3d = numpy.dot(image_center2d, numpy.reshape(self.image_orientation_patient, (2, 3)))
center = self.image_position_patient + image_center3d
direction = numpy.argmax(numpy.abs(numpy.cross(self.image_orientation_patient[:3], self.image_orientation_patient[3:])))
res = numpy.round(center[direction], 2)
return center
@property
def pixel_array(self):
img = self.raw_file.pixel_array.astype(float) / numpy.max(self.raw_file.pixel_array)
return img
def get_csv(self):
res = [self.series_number, self.get_value("InstanceNumber"), self.flip_angle, self.series_description, self.series_time]
return res
def dir(self):
self.raw_file.dir()
#
# ['AcquisitionMatrix',
# 'AcquisitionNumber',
# 'AcquisitionTime',
# 'AngioFlag',
# 'BitsAllocated',
# 'BitsStored',
# 'BodyPartExamined',
# 'CardiacNumberOfImages',
# 'Columns',
# 'CommentsOnThePerformedProcedureStep',
# 'EchoNumbers',
# 'EchoTime',
# 'EchoTrainLength',
# 'FlipAngle',
# 'HighBit',
# 'ImageOrientationPatient',
# 'ImagePositionPatient',
# 'ImageType',
# 'ImagedNucleus',
# 'ImagingFrequency',
# 'InPlanePhaseEncodingDirection',
# 'InstanceCreationTime',
# 'InstanceNumber',
# 'LargestImagePixelValue',
# 'MRAcquisitionType',
# 'MagneticFieldStrength',
# 'Manufacturer',
# 'ManufacturerModelName',
# 'Modality',
# 'NominalInterval',
# 'NumberOfAverages',
# 'NumberOfPhaseEncodingSteps',
# 'PatientAddress',
# 'PatientAge',
# 'PatientBirthDate',
# 'PatientID',
# 'PatientName',
# 'PatientPosition',
# 'PatientSex',
# 'PatientTelephoneNumbers',
# 'PercentPhaseFieldOfView',
# 'PercentSampling',
# 'PerformedProcedureStepID',
# 'PerformedProcedureStepStartTime',
# 'PhotometricInterpretation',
# 'PixelBandwidth',
# 'PixelData',
# 'PixelRepresentation',
# 'PixelSpacing',
# 'PositionReferenceIndicator',
# 'RefdImageSequence',
# 'ReferencedImageSequence',
# 'RepetitionTime',
# 'Rows',
# 'SAR',
# 'SOPClassUID',
# 'SOPInstanceUID',
# 'SamplesPerPixel',
# 'ScanOptions',
# 'ScanningSequence',
# 'SequenceName',
# 'SequenceVariant',
# 'SeriesDescription',
# 'SeriesNumber',
# 'SeriesTime',
# 'SliceLocation',
# 'SliceThickness',
# 'SmallestImagePixelValue',
# 'SoftwareVersions',
# 'SpecificCharacterSet',
# 'StudyTime',
# 'TransmitCoilName',
# 'TriggerTime',
# 'VariableFlipAngleFlag',
# 'WindowCenter',
# 'WindowCenterWidthExplanation',
# 'WindowWidth',
# 'dBdt']