ANIMA  4.0
animaImageResolutionChanger.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 #include <iostream>
3 
4 #include <itkImage.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>
11 
13 #include <itkTranslationTransform.h>
14 
15 int main(int argc, char **argv)
16 {
17  TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION);
18 
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);
21 
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);
25 
26  TCLAP::ValueArg<std::string> interpolationArg("n","interpolation","interpolation method to use [nearest, linear, bspline, sinc]",
27  false,"linear","interpolation method",cmd);
28 
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);
31 
32  try
33  {
34  cmd.parse(argc,argv);
35  }
36  catch (TCLAP::ArgException& e)
37  {
38  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
39  return EXIT_FAILURE;
40  }
41 
42  typedef itk::Image <double,3> ImageType;
43  typedef itk::ResampleImageFilter <ImageType,ImageType> ResampleFilterType;
44 
45  ImageType::Pointer inputImage = anima::readImage <ImageType> (inArg.getValue());
46 
47  ResampleFilterType::Pointer resampler = ResampleFilterType::New();
48  resampler->SetInput(inputImage);
49 
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();
55 
56  for (unsigned int i = 0;i < dimension;++i)
57  {
58  double outRes = (i == 0) * xArg.getValue() + (i == 1) * yArg.getValue() + (i == 2) * zArg.getValue();
59 
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;
66  }
67 
68  resampler->SetSize(size);
69  resampler->SetOutputOrigin(origin);
70  resampler->SetOutputSpacing(spacing);
71  resampler->SetOutputDirection(direction);
72 
73  itk::TranslationTransform <double, 3>::Pointer idTrsf = itk::TranslationTransform <double, 3>::New();
74  idTrsf->SetIdentity();
75  resampler->SetTransform(idTrsf);
76 
77  typename itk::InterpolateImageFunction <ImageType>::Pointer interpolator;
78 
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")
86  {
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();
92  }
93 
94  resampler->SetInterpolator(interpolator);
95  resampler->SetNumberOfWorkUnits(nbpArg.getValue());
96  resampler->Update();
97 
98  anima::writeImage <ImageType> (outArg.getValue(),resampler->GetOutput());
99 
100  return EXIT_SUCCESS;
101 }
int main(int argc, char **argv)