-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimagereader.py
320 lines (287 loc) · 14 KB
/
imagereader.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python
## Program: VMTK
## Module: $RCSfile: vmtkimagereader.py,v $
## Language: Python
## Date: $Date: 2006/05/22 08:33:12 $
## Version: $Revision: 1.16 $
## Copyright (c) Luca Antiga, David Steinman. All rights reserved.
## See LICENCE file for details.
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notices for more information.
from __future__ import absolute_import #NEEDS TO STAY AS TOP LEVEL MODULE FOR Py2-3 COMPATIBILITY
import vtk
from vmtk import vtkvmtk
import sys
from vmtk import pypes
from pipe import Pipe
@Pipe
class imageReader(pypes.pypeScript):
def __init__(self):
pypes.pypeScript.__init__(self)
self.Format = ''
self.GuessFormat = 1
self.UseITKIO = 1
self.InputFileName = ''
self.InputFilePrefix = ''
self.InputFilePattern = ''
self.Image = 0
self.Output = 0
self.DataExtent = [-1, -1, -1, -1, -1, -1]
self.DataSpacing = [1.0, 1.0, 1.0]
self.DataOrigin = [0.0, 0.0, 0.0]
self.DataByteOrder = 'littleendian'
self.DataScalarType = 'float'
self.DesiredOrientation = 'native'
self.HeaderSize = 0
self.FileDimensionality = 3
self.Flip = [0, 0, 0]
self.AutoOrientDICOMImage = 1
self.RasToIjkMatrixCoefficients = [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]
self.XyzToRasMatrixCoefficients = [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]
self.SetScriptName('imagereader')
self.SetScriptDoc('read an image and stores it in a vtkImageData object')
self.SetInputMembers([
['Format','f','str',1,'["vtkxml","vtk","dicom","raw","meta","tiff","png"]','file format'],
['GuessFormat','guessformat','bool',1,'','guess file format from extension'],
['UseITKIO','useitk','bool',1,'','use ITKIO mechanism'],
['Image','i','vtkImageData',1,'','the input image'],
['InputFileName','ifile','str',1,'','input file name'],
['InputFilePrefix','prefix','str',1,'','input file prefix (e.g. foo_)'],
['InputFilePattern','pattern','str',1,'','input file pattern (e.g. %s%04d.png)'],
['DataExtent','extent','int',6,'','3D extent of the image - raw and png'],
['HeaderSize','headersize','int',1,'(0,)','size of the image header - raw only'],
['DataSpacing','spacing','float',3,'','spacing of the image - raw, tiff, png, itk'],
['DataOrigin','origin','float',3,'','origin of the image - raw, tiff, png, itk'],
['DesiredOrientation','orientation','str',1,'["native","axial","coronal","sagittal"]','desired data orientation - itk only'],
['DataByteOrder','byteorder','str',1,'["littleendian","bigendian"]','byte ordering - raw only'],
['DataScalarType','scalartype','str',1,'["float","double","int","short","ushort","uchar"]','scalar type - raw only'],
['FileDimensionality','filedimensionality','int',1,'(2,3)','dimensionality of the file to read - raw only'],
['Flip','flip','bool',3,'','toggle flipping of the corresponding axis'],
['AutoOrientDICOMImage','autoorientdicom','bool',1,'','flip a dicom stack in order to have a left-to-right, posterio-to-anterior, inferior-to-superior image; this is based on the \"image orientation (patient)\" field in the dicom header']
])
self.SetOutputMembers([
['Image','o','vtkImageData',1,'','the output image','vmtkimagewriter'],
['RasToIjkMatrixCoefficients','rastoijkmatrix','float',16],
['XyzToRasMatrixCoefficients','xyztorasmatrix','float',16]
])
def ReadVTKXMLImageFile(self):
if (self.InputFileName == ''):
self.PrintError('Error: no InputFileName.')
self.PrintLog('Reading VTK XML image file.')
reader = vtk.vtkXMLImageDataReader()
reader.SetFileName(self.InputFileName)
reader.Update()
self.Image = reader.GetOutput()
def ReadVTKImageFile(self):
if (self.InputFileName == ''):
self.PrintError('Error: no InputFileName.')
self.PrintLog('Reading VTK image file.')
reader = vtk.vtkStructuredPointsReader()
reader.SetFileName(self.InputFileName)
reader.Update()
self.Image = reader.GetOutput()
def ReadRawImageFile(self):
if (self.InputFileName == '') & (self.InputFilePrefix == ''):
self.PrintError('Error: no InputFileName or InputFilePrefix.')
self.PrintLog('Reading RAW image file.')
reader = vtk.vtkImageReader()
if self.InputFileName != '':
reader.SetFileName(self.InputFileName)
else:
reader.SetFilePrefix(self.InputFilePrefix)
if self.InputFilePattern != '':
reader.SetFilePattern(self.InputFilePattern)
else:
reader.SetFilePattern("%s%04d.png")
reader.SetFileDimensionality(self.FileDimensionality)
if self.DataByteOrder == 'littleendian':
reader.SetDataByteOrderToLittleEndian()
elif self.DataByteOrder == 'bigendian':
reader.SetDataByteOrderToBigEndian()
reader.SetDataExtent(self.DataExtent)
reader.SetDataSpacing(self.DataSpacing)
reader.SetDataOrigin(self.DataOrigin)
reader.SetHeaderSize(self.HeaderSize)
if self.DataScalarType == 'float':
reader.SetDataScalarTypeToFloat()
elif self.DataScalarType == 'double':
reader.SetDataScalarTypeToDouble()
elif self.DataScalarType == 'int':
reader.SetDataScalarTypeToInt()
elif self.DataScalarType == 'short':
reader.SetDataScalarTypeToShort()
elif self.DataScalarType == 'ushort':
reader.SetDataScalarTypeToUnsignedShort()
elif self.DataScalarType == 'uchar':
reader.SetDataScalarTypeToUnsignedChar()
reader.Update()
self.Image = reader.GetOutput()
def ReadMetaImageFile(self):
if (self.InputFileName == ''):
self.PrintError('Error: no InputFileName.')
self.PrintLog('Reading meta image file.')
reader = vtk.vtkMetaImageReader()
reader.SetFileName(self.InputFileName)
reader.Update()
self.Image = reader.GetOutput()
def ReadTIFFImageFile(self):
if (self.InputFileName == '') & (self.InputFilePrefix == ''):
self.PrintError('Error: no InputFileName or InputFilePrefix.')
self.PrintLog('Reading TIFF image file.')
reader = vtk.vtkTIFFReader()
if self.InputFileName != '':
reader.SetFileName(self.InputFileName)
else:
reader.SetFilePrefix(self.InputFilePrefix)
if self.InputFilePattern != '':
reader.SetFilePattern(self.InputFilePattern)
else:
reader.SetFilePattern("%s%04d.png")
reader.SetDataExtent(self.DataExtent)
reader.SetDataSpacing(self.DataSpacing)
reader.SetDataOrigin(self.DataOrigin)
reader.Update()
self.Image = reader.GetOutput()
def ReadPNGImageFile(self):
if (self.InputFileName == '') & (self.InputFilePrefix == ''):
self.PrintError('Error: no InputFileName or InputFilePrefix.')
self.PrintLog('Reading PNG image file.')
reader = vtk.vtkPNGReader()
if self.InputFileName != '':
reader.SetFileName(self.InputFileName)
else:
reader.SetFilePrefix(self.InputFilePrefix)
if self.InputFilePattern != '':
reader.SetFilePattern(self.InputFilePattern)
else:
reader.SetFilePattern("%s%04d.png")
reader.SetDataExtent(self.DataExtent)
reader.SetDataSpacing(self.DataSpacing)
reader.SetDataOrigin(self.DataOrigin)
reader.Update()
self.Image = reader.GetOutput()
def ReadITKIO(self):
if self.InputFileName == '':
self.PrintError('Error: no InputFileName.')
reader = vtkvmtk.vtkvmtkITKArchetypeImageSeriesScalarReader()
reader.SetArchetype(self.InputFileName)
reader.SetDefaultDataSpacing(self.DataSpacing)
reader.SetDefaultDataOrigin(self.DataOrigin)
reader.SetOutputScalarTypeToNative()
if self.DesiredOrientation == 'native':
reader.SetDesiredCoordinateOrientationToNative()
elif self.DesiredOrientation == 'axial':
reader.SetDesiredCoordinateOrientationToAxial()
elif self.DesiredOrientation == 'coronal':
reader.SetDesiredCoordinateOrientationToCoronal()
elif self.DesiredOrientation == 'sagittal':
reader.SetDesiredCoordinateOrientationToSagittal()
reader.SetSingleFile(0)
reader.Update()
self.Image = vtk.vtkImageData()
self.Image.DeepCopy(reader.GetOutput())
matrix = reader.GetRasToIjkMatrix()
self.RasToIjkMatrixCoefficients = [
matrix.GetElement(0,0), matrix.GetElement(0,1), matrix.GetElement(0,2), matrix.GetElement(0,3),
matrix.GetElement(1,0), matrix.GetElement(1,1), matrix.GetElement(1,2), matrix.GetElement(1,3),
matrix.GetElement(2,0), matrix.GetElement(2,1), matrix.GetElement(2,2), matrix.GetElement(2,3),
matrix.GetElement(3,0), matrix.GetElement(3,1), matrix.GetElement(3,2), matrix.GetElement(3,3)]
matrix.Invert()
origin = [matrix.GetElement(0,3), matrix.GetElement(1,3), matrix.GetElement(2,3)]
translationToOrigin = [-origin[0], -origin[1], -origin[2]]
for i in range(3):
direction = [matrix.GetElement(0,i), matrix.GetElement(1,i), matrix.GetElement(2,i)]
vtk.vtkMath.Normalize(direction)
matrix.SetElement(0,i,direction[0])
matrix.SetElement(1,i,direction[1])
matrix.SetElement(2,i,direction[2])
matrix.SetElement(0,3,0.0)
matrix.SetElement(1,3,0.0)
matrix.SetElement(2,3,0.0)
transform = vtk.vtkTransform()
transform.PostMultiply()
transform.Translate(translationToOrigin)
transform.Concatenate(matrix)
transform.Translate(origin)
matrix = transform.GetMatrix()
self.XyzToRasMatrixCoefficients = [
matrix.GetElement(0,0), matrix.GetElement(0,1), matrix.GetElement(0,2), matrix.GetElement(0,3),
matrix.GetElement(1,0), matrix.GetElement(1,1), matrix.GetElement(1,2), matrix.GetElement(1,3),
matrix.GetElement(2,0), matrix.GetElement(2,1), matrix.GetElement(2,2), matrix.GetElement(2,3),
matrix.GetElement(3,0), matrix.GetElement(3,1), matrix.GetElement(3,2), matrix.GetElement(3,3)]
def Execute(self):
extensionFormats = {'vti':'vtkxml',
'vtkxml':'vtkxml',
'vtk':'vtk',
'dcm':'dicom',
'raw':'raw',
'mhd':'meta',
'mha':'meta',
'tif':'tiff',
'png':'png'}
if self.InputFileName == 'BROWSER':
import tkinter.filedialog
import os.path
initialDir = pypes.pypeScript.lastVisitedPath
self.InputFileName = tkinter.filedialog.askopenfilename(title="Input image",initialdir=initialDir)
pypes.pypeScript.lastVisitedPath = os.path.dirname(self.InputFileName)
if not self.InputFileName:
self.PrintError('Error: no InputFileName.')
if self.GuessFormat and self.InputFileName and not self.Format:
import os.path
extension = os.path.splitext(self.InputFileName)[1]
if extension:
extension = extension[1:]
if extension in list(extensionFormats.keys()):
self.Format = extensionFormats[extension]
if self.UseITKIO and self.InputFileName and self.Format not in ['vtkxml','vtk','raw']:
self.ReadITKIO()
else:
if self.Format == 'vtkxml':
self.ReadVTKXMLImageFile()
elif self.Format == 'vtk':
self.ReadVTKImageFile()
elif self.Format == 'raw':
self.ReadRawImageFile()
elif self.Format == 'meta':
self.ReadMetaImageFile()
elif self.Format == 'png':
self.ReadPNGImageFile()
elif self.Format == 'tiff':
self.ReadTIFFImageFile()
elif self.Format == 'dicom':
self.PrintError('Error: please enable parameter UseITKIO in order to read dicom files')
else:
self.PrintError('Error: unsupported format '+ self.Format + '.')
if (self.Flip[0] == 1) | (self.Flip[1] == 1) | (self.Flip[2] == 1):
temp0 = self.Image
if self.Flip[0] == 1:
flipFilter = vtk.vtkImageFlip()
flipFilter.SetInputData(self.Image)
flipFilter.SetFilteredAxis(0)
flipFilter.Update()
temp0 = flipFilter.GetOutput()
temp1 = temp0
if self.Flip[1] == 1:
flipFilter = vtk.vtkImageFlip()
flipFilter.SetInputData(temp0)
flipFilter.SetFilteredAxis(1)
flipFilter.Update()
temp1 = flipFilter.GetOutput()
temp2 = temp1
if self.Flip[2] == 1:
flipFilter = vtk.vtkImageFlip()
flipFilter.SetInputData(temp1)
flipFilter.SetFilteredAxis(2)
flipFilter.Update()
temp2 = flipFilter.GetOutput()
self.Image = temp2
self.PrintLog('Spacing %f %f %f' % self.Image.GetSpacing())
self.PrintLog('Origin %f %f %f' % self.Image.GetOrigin())
self.PrintLog('Dimensions %d %d %d' % self.Image.GetDimensions())
self.Output = self.Image
if __name__=='__main__':
main = pypes.pypeMain()
main.Arguments = sys.argv
main.Execute()