ANIMA  4.0
animaCollapseImage.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 
3 #include <itkImage.h>
4 #include <itkVectorImage.h>
5 #include <itkCommand.h>
6 #include <itkExtractImageFilter.h>
7 
10 
11 struct arguments
12 {
13  std::string input, output;
14  double origin, spacing;
15 };
16 
17 template <class ComponentType, unsigned int InputDim>
18 void
19 collapse(itk::ImageIOBase::Pointer imageIO, const arguments &args)
20 {
21 
22  typedef itk::VectorImage<ComponentType, InputDim> InputImageType;
23  typedef itk::Image<ComponentType, InputDim+1> OutputImageType;
24 
25  typename InputImageType::Pointer inputImg = anima::readImage<InputImageType>(args.input);
26  unsigned int nbComp = imageIO->GetNumberOfComponents();
27 
28  std::cout << "number of dimension : " << InputDim << std::endl;
29  std::cout << "number of components : " << nbComp << std::endl;
30 
31 
32  typename OutputImageType::RegionType finalRegion;
33  typename OutputImageType::SizeType finalSize;
34  typename OutputImageType::IndexType finalIndex;
35  typename OutputImageType::PointType finalOrigin;
36  typename OutputImageType::SpacingType finalSpacing;
37  typename OutputImageType::DirectionType finalDirection;
38 
39  for (unsigned int d = 0; d < InputDim; ++d)
40  {
41 
42  finalIndex[d] = inputImg->GetLargestPossibleRegion().GetIndex()[d];
43  finalSize[d] = inputImg->GetLargestPossibleRegion().GetSize()[d];
44  finalOrigin[d] = inputImg->GetOrigin()[d];
45  finalSpacing[d] = inputImg->GetSpacing()[d];
46  for(unsigned int i = 0; i < InputDim; ++i)
47  finalDirection[d][i] = inputImg->GetDirection()[d][i];
48  }
49  finalIndex[InputDim] = 0;
50  finalSize[InputDim] = nbComp;
51  finalOrigin[InputDim] = args.origin;
52  finalSpacing[InputDim] = args.spacing;
53 
54  for(unsigned int i = 0; i < InputDim; ++i)
55  {
56  finalDirection[InputDim][i] = 0;
57  finalDirection[i][InputDim] = 0;
58  }
59  finalDirection[InputDim][InputDim] = 1;
60 
61  finalRegion.SetIndex(finalIndex);
62  finalRegion.SetSize(finalSize);
63 
64  typename OutputImageType::Pointer outputImg = OutputImageType::New();
65  outputImg->Initialize();
66  outputImg->SetRegions(finalRegion);
67  outputImg->SetOrigin(finalOrigin);
68  outputImg->SetSpacing(finalSpacing);
69  outputImg->SetDirection(finalDirection);
70  outputImg->Allocate();
71 
72  typedef itk::ImageRegionIterator <OutputImageType> FillIteratorType;
73  FillIteratorType fillItr(outputImg, outputImg->GetLargestPossibleRegion());
74  typedef itk::ImageRegionConstIterator<InputImageType> SourceIteratorType;
75  SourceIteratorType srcItr(inputImg, inputImg->GetLargestPossibleRegion());
76  int idx = 0;
77  while(!fillItr.IsAtEnd())
78  {
79  if(srcItr.IsAtEnd())
80  {
81  idx++;
82  srcItr.GoToBegin();
83  }
84 
85  fillItr.Set(srcItr.Get()[idx]);
86  ++fillItr; ++srcItr;
87  }
88 
89  anima::writeImage<OutputImageType>(args.output, outputImg);
90 }
91 
92 template <class ComponentType >
93 void
94 retrieveNbDimensions(itk::ImageIOBase::Pointer imageIO, const arguments &args)
95 {
96  ANIMA_RETRIEVE_NUMBER_OF_DIMENSIONS(imageIO, ComponentType, collapse, imageIO, args)
97 }
98 
99 
100 int main(int ac, const char** av)
101 {
102 
103  TCLAP::CmdLine cmd("Collapse a vector image into a scalar image of dim n+1.\n\
104 The last dimension of the output has same size as the component of the input image.\n\
105 You can give the spacing and the origin of the added dimension, default values are 0 for the origin and 1 for the spacing\n\
106 Example: input image is 4x4x4 with a component size of 32, the output will be 4x4x4x32.\n\
107 INRIA / IRISA - VisAGeS/Empenn Team",
108  ' ',ANIMA_VERSION);
109 
110  TCLAP::ValueArg<std::string> inputArg("i",
111  "input",
112  "Input images to collapse",
113  true,
114  "",
115  "Inputs",
116  cmd);
117 
118  TCLAP::ValueArg<std::string> outputArg("o",
119  "output",
120  "Output image",
121  true,
122  "",
123  "Output",
124  cmd);
125  TCLAP::ValueArg<double> origArg("O",
126  "origin",
127  "Origin of the added Dimension image",
128  false,
129  0,
130  "Origin",
131  cmd);
132  TCLAP::ValueArg<double> spacingArg("s",
133  "spacing",
134  "Spacing of the added Dimension image",
135  false,
136  1,
137  "Spacing ",
138  cmd);
139 
140  try
141  {
142  cmd.parse(ac,av);
143  }
144  catch (TCLAP::ArgException& e)
145  {
146  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
147  return EXIT_FAILURE;
148  }
149 
150  // Find out the type of the image in file
151  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(inputArg.getValue().c_str(),
152  itk::ImageIOFactory::ReadMode);
153 
154  if( !imageIO )
155  {
156  std::cerr << "Itk could not find suitable IO factory for the input" << std::endl;
157  return EXIT_FAILURE;
158  }
159 
160  // Now that we found the appropriate ImageIO class, ask it to read the meta data from the image file.
161  imageIO->SetFileName(inputArg.getValue());
162  imageIO->ReadImageInformation();
163 
164  if(imageIO->GetNumberOfComponents() < 2)
165  {
166  std::cerr << "Input have to have component of size > 1. Nothing to do.";
167  return EXIT_SUCCESS;
168  }
169 
170  std::cout<<"\npreparing filter...\n";
171 
172  arguments args;
173 
174  args.input = inputArg.getValue();
175  args.output = outputArg.getValue();
176  args.origin = origArg.getValue();
177  args.spacing = spacingArg.getValue();
178 
179 
180  try
181  {
182  ANIMA_RETRIEVE_COMPONENT_TYPE(imageIO, retrieveNbDimensions, imageIO, args);
183  }
184  catch ( itk::ExceptionObject & err )
185  {
186  std::cerr << "Itk cannot collapse, be sure to use valid input..." << std::endl;
187  std::cerr << err << std::endl;
188  return EXIT_FAILURE;
189  }
190 
191  return EXIT_SUCCESS;
192 }
std::string output
itk::ImageIOBase::Pointer imageIO
int main(int ac, const char **av)
void retrieveNbDimensions(itk::ImageIOBase::Pointer imageIO, const arguments &args)
#define ANIMA_RETRIEVE_COMPONENT_TYPE(imageIO, function,...)
void collapse(itk::ImageIOBase::Pointer imageIO, const arguments &args)
#define ANIMA_RETRIEVE_NUMBER_OF_DIMENSIONS(imageIO, ComponentType, function,...)
std::string input