ANIMA  4.0
animaPyramidImage.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 
3 #include <itkImageFileReader.h>
4 #include <itkImageFileWriter.h>
5 
8 
12 
13 int main(int ac, const char** av)
14 {
15  TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION);
16 
17  TCLAP::ValueArg<std::string> inArg("i","input","Input image",true,"","input image",cmd);
18  TCLAP::ValueArg<std::string> outArg("o","output","Output resampled image",true,"","output image",cmd);
19 
20  TCLAP::ValueArg<unsigned int> pyrNumArg("p","pyr-num","number of pyramid levels",false,4,"number of pyramid level",cmd);
21  TCLAP::ValueArg<unsigned int> pyrLevelArg("l","pyr-level","pyramid level image required",false,1,"pyramid level",cmd);
22 
23  TCLAP::ValueArg<unsigned int> nbpArg("T","numberofthreads","Number of threads to run on (default : all cores)",false,itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads(),"number of threads",cmd);
24 
25  try
26  {
27  cmd.parse(ac,av);
28  }
29  catch (TCLAP::ArgException& e)
30  {
31  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
32  return(1);
33  }
34 
35  const unsigned int Dimension = 3;
36  typedef double PixelType;
37 
38  typedef itk::Image< PixelType, Dimension > ImageType;
39 
40  typedef itk::ImageFileReader<ImageType> ReaderType;
41  typedef itk::ImageFileWriter<ImageType> WriterType;
42 
43  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(inArg.getValue().c_str(),
44  itk::ImageIOFactory::ReadMode);
45 
46  if (!imageIO)
47  {
48  std::cerr << "Itk could not find suitable IO factory for the input" << std::endl;
49  return EXIT_FAILURE;
50  }
51 
52  // Now that we found the appropriate ImageIO class, ask it to read the meta data from the image file.
53  imageIO->SetFileName(inArg.getValue());
54  imageIO->ReadImageInformation();
55 
56  unsigned int numComponents = imageIO->GetNumberOfComponents();
57 
58  bool tensorImage = false;
59  if (numComponents == 6)
60  tensorImage = true;
61  else if (numComponents != 1)
62  {
63  std::cerr << "Error: not a tensor or scalar image, don't know what to do" << std::endl;
64  return(1);
65  }
66 
67  if (!tensorImage)
68  {
69  ReaderType::Pointer reader = ReaderType::New();
70  reader->SetImageIO(imageIO);
71  reader->SetFileName(inArg.getValue());
72 
73  reader->Update();
74 
77 
78  PyramidType::Pointer imagePyramid = PyramidType::New();
79  imagePyramid->SetInput(reader->GetOutput());
80  ResampleFilterType::Pointer resampler = ResampleFilterType::New();
81  imagePyramid->SetImageResampler(resampler);
82  imagePyramid->SetNumberOfLevels(pyrNumArg.getValue());
83  imagePyramid->SetNumberOfWorkUnits(nbpArg.getValue());
84  imagePyramid->Update();
85 
86  if (pyrLevelArg.getValue() >= imagePyramid->GetNumberOfLevels())
87  {
88  std::cerr << "Error: unable to compute a pyramid image so far, max is: " << imagePyramid->GetNumberOfLevels() - 1 << std::endl;
89  return EXIT_FAILURE;
90  }
91 
92  ImageType::Pointer outputImage = imagePyramid->GetOutput(pyrLevelArg.getValue());
93  outputImage->DisconnectPipeline();
94 
95  WriterType::Pointer outWriter = WriterType::New();
96  outWriter->SetInput(outputImage);
97  outWriter->SetFileName(outArg.getValue());
98  outWriter->SetUseCompression(true);
99  outWriter->Update();
100 
101  return EXIT_SUCCESS;
102  }
103 
104  typedef itk::VectorImage <PixelType,Dimension> VectorImageType;
105  typedef itk::ImageFileReader<VectorImageType> VectorReaderType;
106  typedef itk::ImageFileWriter<VectorImageType> VectorWriterType;
107 
108  VectorReaderType::Pointer vectorReader = VectorReaderType::New();
109  vectorReader->SetImageIO(imageIO);
110  vectorReader->Update();
111 
112  typedef anima::LogTensorImageFilter <PixelType,Dimension> LogTensorFilterType;
113  LogTensorFilterType::Pointer tensorLogger = LogTensorFilterType::New();
114 
115  tensorLogger->SetInput(vectorReader->GetOutput());
116  tensorLogger->SetScaleNonDiagonal(false);
117  tensorLogger->SetNumberOfWorkUnits(nbpArg.getValue());
118 
119  tensorLogger->Update();
120 
123 
124  ResampleFilterType::Pointer vectorResampler = ResampleFilterType::New();
125  vectorResampler->SetFiniteStrainReorientation(true);
126 
127  VectorPyramidType::Pointer vectorImagePyramid = VectorPyramidType::New();
128  vectorImagePyramid->SetInput(vectorReader->GetOutput());
129  vectorImagePyramid->SetImageResampler(vectorResampler);
130  vectorImagePyramid->SetNumberOfLevels(pyrNumArg.getValue());
131  vectorImagePyramid->SetNumberOfWorkUnits(nbpArg.getValue());
132  vectorImagePyramid->Update();
133 
134  if (pyrLevelArg.getValue() >= vectorImagePyramid->GetNumberOfLevels())
135  {
136  std::cerr << "Error: unable to compute a pyramid image so far, max is: " << vectorImagePyramid->GetNumberOfLevels() - 1 << std::endl;
137  return EXIT_FAILURE;
138  }
139 
140  VectorImageType::Pointer vectorOutputImage = vectorImagePyramid->GetOutput(pyrLevelArg.getValue());
141  vectorOutputImage->DisconnectPipeline();
142 
143  typedef anima::ExpTensorImageFilter <PixelType,Dimension> ExpTensorFilterType;
144  ExpTensorFilterType::Pointer tensorExper = ExpTensorFilterType::New();
145 
146  tensorExper->SetInput(vectorOutputImage);
147  tensorExper->SetScaleNonDiagonal(false);
148  tensorExper->SetNumberOfWorkUnits(nbpArg.getValue());
149 
150  tensorExper->Update();
151 
152  VectorWriterType::Pointer outVecWriter = VectorWriterType::New();
153  outVecWriter->SetInput(tensorExper->GetOutput());
154  outVecWriter->SetFileName(outArg.getValue());
155  outVecWriter->SetUseCompression(true);
156  outVecWriter->Update();
157 
158  return EXIT_SUCCESS;
159 }
Computes a pyramid of images using the provided resampler to perform resampling.
int main(int ac, const char **av)