ANIMA  4.0
animaVectorizeImages.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 
5 
6 #include <itkComposeImageFilter.h>
7 
8 struct arguments
9 {
10  std::vector<std::string> inputs;
11  std::string output;
12  bool fileList;
13 };
14 
15 template <class InputImageType>
16 void
17 vectorize(const arguments &args)
18 {
19  unsigned int nbComponents = args.inputs.size();
20  typedef itk::VectorImage<typename InputImageType::PixelType, InputImageType::ImageDimension> OutputImageType;
21 
22  typedef itk::ComposeImageFilter<InputImageType, OutputImageType> ComposeImageFilterType;
23  typename ComposeImageFilterType::Pointer composeImageFilter = ComposeImageFilterType::New();
24 
25  if (!args.fileList)
26  {
27  for(unsigned int i = 0; i < nbComponents; ++i)
28  composeImageFilter->SetInput(i, anima::readImage<InputImageType>(args.inputs[i]));
29  }
30  else
31  {
32  std::string inputName = args.inputs[0];
33  anima::setMultipleImageFilterInputsFromFileName<InputImageType,ComposeImageFilterType>(inputName,composeImageFilter);
34  }
35 
36  composeImageFilter->Update();
37 
38  anima::writeImage<OutputImageType>(args.output, composeImageFilter->GetOutput());
39 }
40 
41 template <class ComponentType, int dimension>
42 void
43 checkIfComponentsAreVectors(itk::ImageIOBase::Pointer imageIO, const arguments &args)
44 {
45  if(imageIO->GetNumberOfComponents() != 1)
46  {
47  itk::ExceptionObject excp(__FILE__, __LINE__, "Number of components not supported.", ITK_LOCATION);
48  throw excp;
49  }
50  else
51  {
52  typedef itk::Image<ComponentType, dimension> ImageType;
53  vectorize<ImageType>(args);
54  }
55 }
56 
57 template <class ComponentType>
58 void
59 retrieveNbDimensions(itk::ImageIOBase::Pointer imageIO, const arguments &args)
60 {
61  ANIMA_RETRIEVE_NUMBER_OF_DIMENSIONS(imageIO, ComponentType, checkIfComponentsAreVectors, imageIO, args);
62 }
63 
64 int main(int ac, const char** av)
65 {
66  TCLAP::CmdLine cmd("animaVectorizeImage is used to combine several scalar images into a multicomponent Vector image."
67  "INRIA / IRISA - VisAGeS/Empenn Team",
68  ' ',
69  ANIMA_VERSION);
70 
71  TCLAP::MultiArg<std::string> inputArg("i",
72  "inputs",
73  "input images (list in text file or multiple arguments)",
74  true,
75  "input images",
76  cmd);
77 
78  TCLAP::ValueArg<std::string> outputArg("o",
79  "output",
80  "output image",
81  true,
82  "",
83  "output image",
84  cmd);
85 
86  try
87  {
88  cmd.parse(ac,av);
89  }
90  catch (TCLAP::ArgException& e)
91  {
92  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
93  return EXIT_FAILURE;
94  }
95 
96  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(inputArg.getValue()[0].c_str(),itk::ImageIOFactory::ReadMode);
97 
98  arguments args;
99  args.fileList = false;
100 
101  if( !imageIO )
102  {
103  std::ifstream fileIn (inputArg.getValue()[0].c_str());
104  char tmpStr[2048];
105  fileIn.getline(tmpStr,2048);
106 
107  while ((strcmp(tmpStr,"") == 0) && (!fileIn.eof()))
108  fileIn.getline(tmpStr,2048);
109 
110  if (!fileIn.eof())
111  imageIO = itk::ImageIOFactory::CreateImageIO(tmpStr,itk::ImageIOFactory::ReadMode);
112 
113  fileIn.close();
114 
115  if( !imageIO )
116  {
117  std::cerr << "Itk could not find suitable IO factory for the input" << std::endl;
118  return EXIT_FAILURE;
119  }
120 
121  imageIO->SetFileName(tmpStr);
122  imageIO->ReadImageInformation();
123  args.fileList = true;
124  }
125 
126  // Now that we found the appropriate ImageIO class, ask it to read the meta data from the image file.
127  if (!args.fileList)
128  {
129  imageIO->SetFileName(inputArg.getValue()[0]);
130  imageIO->ReadImageInformation();
131  }
132 
133  args.inputs = inputArg.getValue(); args.output = outputArg.getValue();
134 
135  if(args.output != "")
136  {
137  try
138  {
139  ANIMA_RETRIEVE_COMPONENT_TYPE(imageIO, retrieveNbDimensions, imageIO, args)
140  }
141  catch ( itk::ExceptionObject & err )
142  {
143  std::cerr << "Itk cannot vectorize, be sure to use valid arguments..." << std::endl;
144  std::cerr << err << std::endl;
145  return EXIT_FAILURE;
146  }
147  }
148 
149  return EXIT_SUCCESS;
150 }
int main(int ac, const char **av)
std::vector< std::string > inputs
void vectorize(const arguments &args)
std::string output
itk::ImageIOBase::Pointer imageIO
unsigned int nbComponents
void checkIfComponentsAreVectors(itk::ImageIOBase::Pointer imageIO, const arguments &args)
#define ANIMA_RETRIEVE_COMPONENT_TYPE(imageIO, function,...)
void retrieveNbDimensions(itk::ImageIOBase::Pointer imageIO, const arguments &args)
#define ANIMA_RETRIEVE_NUMBER_OF_DIMENSIONS(imageIO, ComponentType, function,...)