ANIMA  4.0
animaFlipTensors.cxx
Go to the documentation of this file.
2 #include <tclap/CmdLine.h>
5 #include <cctype>
6 
7 //Update progression of the process
8 void eventCallback(itk::Object* caller, const itk::EventObject& event, void* clientData)
9 {
10  itk::ProcessObject *processObject = (itk::ProcessObject*) caller;
11  std::cout << "\033[K\rProgression: " << (int)(processObject->GetProgress() * 100) << "%" << std::flush;
12 }
13 
14 struct arguments
15 {
16  std::string input, output, mask, axis;
17  unsigned int nthreads;
18 };
19 
20 template <class ComponentType, unsigned int ImageDimension>
21 void
22 flipTensors(itk::ImageIOBase::Pointer imageIO, const arguments &args)
23 {
24  itk::CStyleCommand::Pointer callback = itk::CStyleCommand::New();
25  callback->SetCallback(eventCallback);
26 
28  typedef typename FilterType::InputImageType InputImageType;
29  typedef typename FilterType::OutputImageType OutputImageType;
30  typedef typename FilterType::MaskImageType MaskImageType;
31 
32  typename FilterType::Pointer filter = FilterType::New();
33  filter->SetInput(anima::readImage<InputImageType>(args.input));
34  filter->SetFlippedAxis(args.axis);
35 
36  if (args.mask != "")
37  filter->SetComputationMask(anima::readImage<MaskImageType>(args.mask));
38 
39  filter->SetNumberOfWorkUnits(args.nthreads);
40  filter->AddObserver(itk::ProgressEvent(), callback);
41  filter->Update();
42 
43  std::cout << std::endl;
44 
45  anima::writeImage<OutputImageType>(args.output, filter->GetOutput());
46 }
47 
48 template <class ComponentType>
49 void
50 retrieveNbDimensions(itk::ImageIOBase::Pointer imageIO, const arguments &args)
51 {
52  ANIMA_RETRIEVE_NUMBER_OF_DIMENSIONS(imageIO, ComponentType, flipTensors, imageIO, args)
53 }
54 
55 int main(int argc, char **argv)
56 {
57 
58  TCLAP::CmdLine cmd("Flip tensors in a DTI volume.\nINRIA / IRISA - VisAGeS/Empenn Team",' ',ANIMA_VERSION);
59 
60  TCLAP::ValueArg<std::string> inArg("i", "input", "Input tensor image.", true, "", "input image", cmd);
61  TCLAP::ValueArg<std::string> outArg("o", "output", "Output tensor image.", true, "", "output image", cmd);
62 
63  TCLAP::ValueArg<std::string> maskArg("m", "mask", "Computation mask", false, "", "mask image", cmd);
64  TCLAP::ValueArg<std::string> axisArg("a", "axis", "Axis to be flipped (choices are X, Y [default] or Z).", false, "Y", "axis name", cmd);
65 
66  TCLAP::ValueArg<unsigned int> nbpArg("p", "nthreads", "Number of thread to use (default: all)", false, itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads(), "number of thread", cmd);
67 
68  try
69  {
70  cmd.parse(argc,argv);
71  }
72  catch (TCLAP::ArgException& e)
73  {
74  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
75  return EXIT_FAILURE;
76  }
77 
78  // Retrieve image info
79  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(inArg.getValue().c_str(),
80  itk::ImageIOFactory::ReadMode);
81  if (!imageIO)
82  {
83  std::cerr << "Itk could not find suitable IO factory for the input" << std::endl;
84  return EXIT_FAILURE;
85  }
86 
87  // Now that we found the appropriate ImageIO class, ask it to read the meta data from the image file.
88  imageIO->SetFileName(inArg.getValue());
89  imageIO->ReadImageInformation();
90 
91  arguments args;
92  args.input = inArg.getValue();
93  args.output = outArg.getValue();
94  args.mask = maskArg.getValue();
95 
96  std::string axisStr = axisArg.getValue();
97  std::transform(axisStr.begin(),axisStr.end(),axisStr.begin(),[](unsigned char c){ return std::tolower(c); });
98  args.axis = axisStr;
99  args.nthreads = nbpArg.getValue();
100 
101  try
102  {
103  ANIMA_RETRIEVE_COMPONENT_TYPE(imageIO, retrieveNbDimensions, imageIO, args);
104  }
105  catch (itk::ExceptionObject &err)
106  {
107  std::cerr << err << std::endl;
108  return EXIT_FAILURE;
109  }
110 
111  return EXIT_SUCCESS;
112 
113 }// end of main
std::string mask
std::string output
void flipTensors(itk::ImageIOBase::Pointer imageIO, const arguments &args)
void retrieveNbDimensions(itk::ImageIOBase::Pointer imageIO, const arguments &args)
int main(int argc, char **argv)
std::string axis
itk::ImageIOBase::Pointer imageIO
void eventCallback(itk::Object *caller, const itk::EventObject &event, void *clientData)
#define ANIMA_RETRIEVE_COMPONENT_TYPE(imageIO, function,...)
unsigned int nthreads
#define ANIMA_RETRIEVE_NUMBER_OF_DIMENSIONS(imageIO, ComponentType, function,...)
std::string input