1 #include <tclap/CmdLine.h> 5 #include <itkResampleImageFilter.h> 6 #include <itkNearestNeighborInterpolateImageFunction.h> 7 #include <itkLinearInterpolateImageFunction.h> 8 #include <itkBSplineInterpolateImageFunction.h> 9 #include <itkWindowedSincInterpolateImageFunction.h> 10 #include <itkConstantBoundaryCondition.h> 13 #include <itkTranslationTransform.h> 15 int main(
int argc,
char **argv)
17 TCLAP::CmdLine cmd(
"INRIA / IRISA - VisAGeS/Empenn Team",
' ',ANIMA_VERSION);
19 TCLAP::ValueArg<std::string> inArg(
"i",
"input",
"Input image",
true,
"",
"input image",cmd);
20 TCLAP::ValueArg<std::string> outArg(
"o",
"output",
"Output resampled image",
true,
"",
"output image",cmd);
22 TCLAP::ValueArg<double> xArg(
"x",
"x-res",
"Output voxel spacing on X direction (default: 1.0)",
false,1.0,
"voxel spacing on X",cmd);
23 TCLAP::ValueArg<double> yArg(
"y",
"y-res",
"Output voxel spacing on Y direction (default: 1.0)",
false,1.0,
"voxel spacing on Y",cmd);
24 TCLAP::ValueArg<double> zArg(
"z",
"z-res",
"Output voxel spacing on Z direction (default: 1.0)",
false,1.0,
"voxel spacing on Z",cmd);
26 TCLAP::ValueArg<std::string> interpolationArg(
"n",
"interpolation",
"interpolation method to use [nearest, linear, bspline, sinc]",
27 false,
"linear",
"interpolation method",cmd);
29 TCLAP::ValueArg<unsigned int> nbpArg(
"p",
"numberofthreads",
"Number of threads to run on (default : all cores)",
30 false,itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads(),
"number of threads",cmd);
36 catch (TCLAP::ArgException& e)
38 std::cerr <<
"Error: " << e.error() <<
"for argument " << e.argId() << std::endl;
42 typedef itk::Image <double,3> ImageType;
43 typedef itk::ResampleImageFilter <ImageType,ImageType> ResampleFilterType;
45 ImageType::Pointer inputImage = anima::readImage <ImageType> (inArg.getValue());
47 ResampleFilterType::Pointer resampler = ResampleFilterType::New();
48 resampler->SetInput(inputImage);
50 ImageType::SizeType size = inputImage->GetLargestPossibleRegion().GetSize();
51 ImageType::PointType origin = inputImage->GetOrigin();
52 ImageType::SpacingType spacing = inputImage->GetSpacing();
53 ImageType::DirectionType direction = inputImage->GetDirection();
54 unsigned int dimension = ImageType::GetImageDimension();
56 for (
unsigned int i = 0;i < dimension;++i)
58 double outRes = (i == 0) * xArg.getValue() + (i == 1) * yArg.getValue() + (i == 2) * zArg.getValue();
60 double spacingRatio = spacing[i] / outRes;
61 double oldSize = size[i];
62 size[i] = std::floor(size[i] * spacingRatio);
63 double trueOutRes = oldSize * spacing[i] / size[i];
64 origin[i] += 0.5 * (trueOutRes - spacing[i]);
65 spacing[i] = trueOutRes;
68 resampler->SetSize(size);
69 resampler->SetOutputOrigin(origin);
70 resampler->SetOutputSpacing(spacing);
71 resampler->SetOutputDirection(direction);
73 itk::TranslationTransform <double, 3>::Pointer idTrsf = itk::TranslationTransform <double, 3>::New();
74 idTrsf->SetIdentity();
75 resampler->SetTransform(idTrsf);
77 typename itk::InterpolateImageFunction <ImageType>::Pointer interpolator;
79 if(interpolationArg.getValue() ==
"nearest")
80 interpolator = itk::NearestNeighborInterpolateImageFunction<ImageType>::New();
81 else if(interpolationArg.getValue() ==
"linear")
82 interpolator = itk::LinearInterpolateImageFunction<ImageType>::New();
83 else if(interpolationArg.getValue() ==
"bspline")
84 interpolator = itk::BSplineInterpolateImageFunction<ImageType>::New();
85 else if(interpolationArg.getValue() ==
"sinc")
87 const unsigned int WindowRadius = 4;
88 typedef itk::Function::HammingWindowFunction<WindowRadius> WindowFunctionType;
89 typedef itk::ConstantBoundaryCondition<ImageType> BoundaryConditionType;
90 interpolator = itk::WindowedSincInterpolateImageFunction
91 <ImageType, WindowRadius, WindowFunctionType, BoundaryConditionType,
double >::New();
94 resampler->SetInterpolator(interpolator);
95 resampler->SetNumberOfWorkUnits(nbpArg.getValue());
98 anima::writeImage <ImageType> (outArg.getValue(),resampler->GetOutput());
int main(int argc, char **argv)