forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DicomImageReadWrite.cxx
306 lines (255 loc) · 9.21 KB
/
DicomImageReadWrite.cxx
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
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
// Software Guide : BeginLatex
//
// This example illustrates how to read a single DICOM slice and write it back
// as another DICOM slice. In the process an intensity rescaling is also
// applied.
//
// In order to read and write the slice we use the \doxygen{GDCMImageIO}
// class which encapsulates a connection to the underlying GDCM library. In
// this way we gain access from ITK to the DICOM functionalities offered by
// GDCM. The GDCMImageIO object is connected as the ImageIO object to be used
// by the \doxygen{ImageFileWriter}.
//
// We should first include the following header files.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkGDCMImageIO.h"
// Software Guide : EndCodeSnippet
#include <list>
#include <fstream>
int main( int argc, char* argv[] )
{
// Verify the number of parameters in the command line
if( argc < 5 )
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " DicomImage OutputDicomImage ";
std::cerr << " OutputImage RescaleDicomImage\n";
return EXIT_FAILURE;
}
// Software Guide : BeginLatex
//
// Then we declare the pixel type and image dimension, and use them for
// instantiating the image type to be read.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef signed short InputPixelType;
const unsigned int InputDimension = 2;
typedef itk::Image< InputPixelType, InputDimension > InputImageType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// With the image type we can instantiate the type of the reader, create one,
// and set the filename of the image to be read.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::ImageFileReader< InputImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// GDCMImageIO is an ImageIO class for reading and writing DICOM v3 and
// ACR/NEMA images. The GDCMImageIO object is constructed here and connected to
// the ImageFileReader.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
reader->SetImageIO( gdcmImageIO );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// At this point we can trigger the reading process by invoking the
// \code{Update()} method. Since this reading process may eventually throw an
// exception, we place the invocation inside a \code{try/catch} block.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
try
{
reader->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << "exception in file reader " << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We now have the image in memory and can get access to it using the
// \code{GetOutput()} method of the reader. In the remainder of this current
// example, we focus on showing how to save this image again in DICOM
// format in a new file.
//
// Software Guide : EndLatex
// Software Guide : BeginLatex
//
// First, we must instantiate an ImageFileWriter type. Then, we construct one,
// set the filename to be used for writing, and connect the input image to be
// written. Since in this example we write the image in different ways,
// and in each case use a different writer, we enumerated the variable
// names of the writer objects as well as their types.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::ImageFileWriter< InputImageType > Writer1Type;
Writer1Type::Pointer writer1 = Writer1Type::New();
writer1->SetFileName( argv[2] );
writer1->SetInput( reader->GetOutput() );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We need to explicitly set the proper image IO (GDCMImageIO) to the writer
// filter since the input DICOM dictionary is being passed along the writing
// process. The dictionary contains all necessary information that a valid
// DICOM file should contain, like Patient Name, Patient ID, Institution Name,
// etc.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
writer1->SetImageIO( gdcmImageIO );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The writing process is triggered by invoking the \code{Update()} method.
// Since this execution may result in exceptions being thrown we place the
// \code{Update()} call inside a \code{try/catch} block.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
try
{
writer1->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << "exception in file writer " << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We will now rescale the image using the RescaleIntensityImageFilter. For
// this purpose we use a better suited pixel type: \code{unsigned char}
// instead of \code{signed short}. The minimum and maximum values of the
// output image are explicitly defined in the rescaling filter.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef unsigned char WritePixelType;
typedef itk::Image< WritePixelType, 2 > WriteImageType;
typedef itk::RescaleIntensityImageFilter<
InputImageType, WriteImageType > RescaleFilterType;
RescaleFilterType::Pointer rescaler = RescaleFilterType::New();
rescaler->SetOutputMinimum( 0 );
rescaler->SetOutputMaximum( 255 );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We create a second writer object that will save the rescaled image into a
// new file, which is not in DICOM format. This is done only for the sake of
// verifying the image against the one that will be saved in DICOM format later
// in this example.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::ImageFileWriter< WriteImageType > Writer2Type;
Writer2Type::Pointer writer2 = Writer2Type::New();
writer2->SetFileName( argv[3] );
rescaler->SetInput( reader->GetOutput() );
writer2->SetInput( rescaler->GetOutput() );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The writer can be executed by invoking the \code{Update()} method from
// inside a \code{try/catch} block.
//
// Software Guide : EndLatex
try
{
writer2->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << "exception in file writer " << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginLatex
//
// We proceed now to save the same rescaled image into a file in DICOM format.
// For this purpose we just need to set up a \doxygen{ImageFileWriter} and pass
// to it the rescaled image as input.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::ImageFileWriter< WriteImageType > Writer3Type;
Writer3Type::Pointer writer3 = Writer3Type::New();
writer3->SetFileName( argv[4] );
writer3->SetInput( rescaler->GetOutput() );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We now need to explicitly set the proper image IO (GDCMImageIO), but also
// we must tell the ImageFileWriter to not use the MetaDataDictionary from the
// input but from the GDCMImageIO since this is the one that contains the DICOM
// specific information
//
// The GDCMImageIO object will automatically detect the pixel type, in this
// case \code{unsigned char} and it will update the DICOM header information
// accordingly.
// \index{itk::ImageFileWriter!UseInputMetaDataDictionaryOff()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
writer3->UseInputMetaDataDictionaryOff ();
writer3->SetImageIO( gdcmImageIO );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally we trigger the execution of the DICOM writer by invoking the
// Update() method from inside a try/catch block.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
try
{
writer3->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << "Exception in file writer " << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
// Software Guide : EndCodeSnippet
return EXIT_SUCCESS;
}