-
Notifications
You must be signed in to change notification settings - Fork 27
/
ReadResampleWriteNifti.cxx
executable file
·131 lines (94 loc) · 4.28 KB
/
ReadResampleWriteNifti.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
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: ReadResampleWriteNifti.cxx
Language: C++
Date: Date: 2010/12/13
Version: 1.0
Author: Jian Wu (eewujian@hotmail.com)
Univerisity of Florida
Virginia Commonwealth University
This Program read a 3D image volume, downsample it, and save it in NIFTI
image format.
This program was modified from the ITK example--ResampleImageFilter2.cxx
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkNiftiImageIO.h"
#include "itkResampleImageFilter.h"
#include "itkAffineTransform.h"
#include "itkLinearInterpolateImageFunction.h"
int main( int argc, char * argv[] )
{
if( argc < 3 )
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " inputImageFile outputImageFile" << std::endl;
return EXIT_FAILURE;
}
if( argc > 3 )
{
std::cerr << "Too many arguments" << std::endl;
}
const unsigned int Dimension = 3;
double outputSpacing[ Dimension ];
outputSpacing[0] = 2.0; // pixel spacing in millimeters along X
outputSpacing[1] = 2.0; // pixel spacing in millimeters along Y
outputSpacing[2] = 2.0; // pixel spacing in millimeters along Z
typedef short InputPixelType;
typedef short OutputPixelType;
typedef itk::Image< InputPixelType, Dimension > InputImageType;
typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
typedef itk::ImageFileReader< InputImageType > ReaderType;
typedef itk::ImageFileWriter< OutputImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
reader->Update();
typedef itk::ResampleImageFilter<
InputImageType, OutputImageType > FilterType;
FilterType::Pointer filter = FilterType::New();
typedef itk::AffineTransform< double, Dimension > TransformType;
TransformType::Pointer transform = TransformType::New();
typedef itk::LinearInterpolateImageFunction<
InputImageType, double > InterpolatorType;
InterpolatorType::Pointer interpolator = InterpolatorType::New();
filter->SetInterpolator( interpolator );
filter->SetDefaultPixelValue( 0 );
InputImageType::SpacingType inputSpacing = reader->GetOutput()->GetSpacing();
InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
InputImageType::SizeType inputSize = inputRegion.GetSize();
double resampleRatio[ Dimension ];
resampleRatio[0] = outputSpacing[0] / inputSpacing[0]; // resample ratio along X
resampleRatio[1] = outputSpacing[1] / inputSpacing[1]; // resample ratio along Y
resampleRatio[2] = outputSpacing[2] / inputSpacing[2]; // resample ratio along Z
filter->SetOutputSpacing( outputSpacing );
filter->SetOutputOrigin( reader->GetOutput()->GetOrigin() );
InputImageType::SizeType outputSize;
outputSize[0] = floor(inputSize[0] / resampleRatio[0] + 0.5); // number of pixels along X
outputSize[1] = floor(inputSize[1] / resampleRatio[1] + 0.5); // number of pixels along Y
outputSize[2] = floor(inputSize[2] / resampleRatio[2] + 0.5); // number of pixels along Z
filter->SetSize( outputSize );
filter->SetInput( reader->GetOutput() );
transform->SetIdentity();
filter->SetTransform( transform );
WriterType::Pointer writer = WriterType::New();
typedef itk::NiftiImageIO ImageIOType;
ImageIOType::Pointer niftiIO = ImageIOType::New();
// The NiftiImageIO object is then connected to the
// ImageFileWriter. This will short-circuit the action of the
// ImageIOFactory mechanism. The ImageFileWriter will
// not attempt to look for other ImageIO objects capable of
// performing the writing tasks. It will simply invoke the one provided by
// the user.
writer->SetImageIO( niftiIO );
writer->SetFileName( argv[2] );
writer->SetInput( filter->GetOutput() );
writer->Update();
return EXIT_SUCCESS;
}