ANIMA  4.0
animaPyramidalBMRegistration.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 
5 
6 #include <itkTimeProbe.h>
7 #include <itkTransformFileReader.h>
8 
9 int main(int argc, const char** argv)
10 {
11  const unsigned int Dimension = 3;
12 
13  typedef itk::Image <double,Dimension> InputImageType;
15  typedef anima::BaseTransformAgregator < Dimension > AgregatorType;
16  typedef itk::AffineTransform<AgregatorType::ScalarType,Dimension> AffineTransformType;
17  typedef AffineTransformType::Pointer AffineTransformPointer;
18 
19  PyramidBMType::Pointer matcher = PyramidBMType::New();
20 
21  // Parsing arguments
22  TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION);
23 
24  // Setting up parameters
25  TCLAP::ValueArg<std::string> fixedArg("r","refimage","Fixed image",true,"","fixed image",cmd);
26  TCLAP::ValueArg<std::string> movingArg("m","movingimage","Moving image",true,"","moving image",cmd);
27  TCLAP::ValueArg<std::string> outArg("o","outputimage","Output (registered) image",true,"","output image",cmd);
28  TCLAP::ValueArg<unsigned int> outTrTypeArg("","ot","Output transformation type (0: rigid, 1: translation, 2: affine, 3: anisotropic_sim, default: 0)",false,0,"output transformation type",cmd);
29 
30  TCLAP::ValueArg<std::string> initialTransformArg("i","inittransform","Initial transformation",false,"","initial transform",cmd);
31  TCLAP::ValueArg<std::string> directionTransformArg("U", "dirtransform", "Input direction transformation for anisotropic similarity", false, "", "input direction transform", cmd);
32 
33  TCLAP::ValueArg<std::string> outputTransformArg("O","outtransform","Output transformation",false,"","output transform",cmd);
34  TCLAP::ValueArg<std::string> outputNRTransformArg("","out-rigid","Output nearest rigid transformation",false,"","output nearest rigid transform",cmd);
35  TCLAP::ValueArg<std::string> outputNSTransformArg("","out-sim","Output nearest similarity transformation",false,"","output nearest similarity transform",cmd);
36 
37  TCLAP::ValueArg<std::string> blockMaskArg("M","mask-im","Mask image for block generation",false,"","block mask image",cmd);
38  TCLAP::ValueArg<unsigned int> blockSizeArg("","bs","Block size (default: 5)",false,5,"block size",cmd);
39  TCLAP::ValueArg<unsigned int> blockSpacingArg("","sp","Block spacing (default: 5)",false,5,"block spacing",cmd);
40  TCLAP::ValueArg<double> stdevThresholdArg("s","stdev","Threshold block standard deviation (default: 5)",false,5,"block minimal standard deviation",cmd);
41  TCLAP::ValueArg<double> percentageKeptArg("k","per-kept","Percentage of blocks with the highest variance kept (default: 0.8)",false,0.8,"percentage of blocks kept",cmd);
42 
43  TCLAP::ValueArg<unsigned int> blockTransfoArg("t","in-transform","Transformation computed between blocks (0: translation, 1: rigid, 2: affine, 3: directional affine, default: 0)",false,0,"transformation between blocks",cmd);
44  TCLAP::ValueArg<unsigned int> directionArg("d","dir","Affine direction for directional transform output (default: 1 = Y axis)",false,1,"direction of directional affine",cmd);
45  TCLAP::ValueArg<unsigned int> blockMetricArg("","metric","Similarity metric between blocks (0: squared correlation coefficient, 1: correlation coefficient, 2: mean squares, default: 0)",false,0,"similarity metric",cmd);
46  TCLAP::ValueArg<unsigned int> optimizerArg("","opt","Optimizer for optimal block search (0: Exhaustive, 1: Bobyqa, default: 1)",false,1,"optimizer",cmd);
47 
48  TCLAP::ValueArg<unsigned int> maxIterationsArg("","mi","Maximum block match iterations (default: 10)",false,10,"maximum iterations",cmd);
49  TCLAP::ValueArg<double> minErrorArg("","me","Minimal distance between consecutive estimated transforms (default: 0.01)",false,0.01,"minimal distance between transforms",cmd);
50 
51  TCLAP::ValueArg<unsigned int> optimizerMaxIterationsArg("","oi","Maximum iterations for local optimizer (default: 100)",false,100,"maximum local optimizer iterations",cmd);
52  TCLAP::ValueArg<unsigned int> initTypeArg("I","init-type", "If no input transformation is given, initialization type (0: identity, 1: align gravity centers, 2: gravity PCA closest transform, default: 1)",false,1,"initialization type",cmd);
53 
54  TCLAP::ValueArg<double> searchRadiusArg("","sr","Search radius in pixels (exhaustive search window, rho start for bobyqa, default: 2)",false,2,"optimizer search radius",cmd);
55  TCLAP::ValueArg<double> searchAngleRadiusArg("","sar","Search angle radius in degrees (rho start for bobyqa, default: 5)",false,5,"optimizer search angle radius",cmd);
56  TCLAP::ValueArg<double> searchScaleRadiusArg("","scr","Search scale radius (rho start for bobyqa, default: 0.1)",false,0.1,"optimizer search scale radius",cmd);
57  TCLAP::ValueArg<double> finalRadiusArg("","fr","Final radius (rho end for bobyqa, default: 0.001)",false,0.001,"optimizer final radius",cmd);
58  TCLAP::ValueArg<double> searchStepArg("","st","Search step for exhaustive search (default: 2)",false,2,"exhaustive optimizer search step",cmd);
59 
60  TCLAP::ValueArg<double> translateUpperBoundArg("","tub","Upper bound on translation for bobyqa (in voxels, default: 10)",false,10,"Bobyqa translate upper bound",cmd);
61  TCLAP::ValueArg<double> angleUpperBoundArg("","aub","Upper bound on angles for bobyqa (in degrees, default: 180)",false,180,"Bobyqa angle upper bound",cmd);
62  TCLAP::ValueArg<double> scaleUpperBoundArg("","scu","Upper bound on scale for bobyqa (default: 3)",false,3,"Bobyqa scale upper bound",cmd);
63 
64  TCLAP::ValueArg<unsigned int> symmetryArg("","sym-reg","Registration symmetry type (0: asymmetric, 1: symmetric, 2: kissing, default: 0)",false,0,"symmetry type",cmd);
65 
66  TCLAP::ValueArg<unsigned int> agregatorArg("a","agregator","Transformation agregator type (0: M-Estimation, 1: least squares, 2: least trimmed squares, default: 0)",false,0,"agregator type",cmd);
67  TCLAP::ValueArg<double> agregThresholdArg("","at","Agregator threshold value (for M-estimation or LTS)",false,0.5,"agregator threshold value",cmd);
68  TCLAP::ValueArg<double> seStoppingThresholdArg("","lst","LTS Stopping Threshold (default: 0.01)",false,0.01,"LTS stopping threshold",cmd);
69 
70  TCLAP::ValueArg<unsigned int> numPyramidLevelsArg("p","pyr","Number of pyramid levels (default: 3)",false,3,"number of pyramid levels",cmd);
71  TCLAP::ValueArg<unsigned int> lastPyramidLevelArg("l","last-level","Index of the last pyramid level explored (default: 0)",false,0,"last pyramid level",cmd);
72  TCLAP::ValueArg<unsigned int> numThreadsArg("T","threads","Number of execution threads (default: 0 = all cores)",false,0,"number of threads",cmd);
73 
74  try
75  {
76  cmd.parse(argc,argv);
77  }
78  catch (TCLAP::ArgException& e)
79  {
80  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
81  return EXIT_FAILURE;
82  }
83 
84  // Setting matcher arguments
85  matcher->SetBlockSize( blockSizeArg.getValue() );
86  matcher->SetBlockSpacing( blockSpacingArg.getValue() );
87  matcher->SetStDevThreshold( stdevThresholdArg.getValue() );
88  matcher->SetTransform( (PyramidBMType::Transform) blockTransfoArg.getValue() );
89  matcher->SetAffineDirection(directionArg.getValue());
90  matcher->SetMetric( (PyramidBMType::Metric) blockMetricArg.getValue() );
91  matcher->SetOptimizer( (PyramidBMType::Optimizer) optimizerArg.getValue() );
92  matcher->SetMaximumIterations( maxIterationsArg.getValue() );
93  matcher->SetMinimalTransformError( minErrorArg.getValue() );
94  matcher->SetFinalRadius(finalRadiusArg.getValue());
95  matcher->SetOptimizerMaximumIterations( optimizerMaxIterationsArg.getValue() );
96  matcher->SetSearchRadius( searchRadiusArg.getValue() );
97  matcher->SetSearchAngleRadius( searchAngleRadiusArg.getValue() );
98  matcher->SetSearchScaleRadius( searchScaleRadiusArg.getValue() );
99  matcher->SetStepSize( searchStepArg.getValue() );
100  matcher->SetTranslateUpperBound( translateUpperBoundArg.getValue() );
101  matcher->SetAngleUpperBound( angleUpperBoundArg.getValue() );
102  matcher->SetScaleUpperBound( scaleUpperBoundArg.getValue() );
103  matcher->SetSymmetryType( (PyramidBMType::SymmetryType) symmetryArg.getValue() );
104  matcher->SetAgregator( (PyramidBMType::Agregator) agregatorArg.getValue() );
105  matcher->SetOutputTransformType( (PyramidBMType::OutputTransform) outTrTypeArg.getValue() );
106  matcher->SetAgregThreshold( agregThresholdArg.getValue() );
107  matcher->SetSeStoppingThreshold( seStoppingThresholdArg.getValue() );
108  matcher->SetNumberOfPyramidLevels( numPyramidLevelsArg.getValue() );
109  matcher->SetLastPyramidLevel( lastPyramidLevelArg.getValue() );
110 
111  if (blockMaskArg.getValue() != "")
112  matcher->SetBlockGenerationMask(anima::readImage<PyramidBMType::MaskImageType>(blockMaskArg.getValue()));
113 
114  if (numThreadsArg.getValue() != 0)
115  matcher->SetNumberOfWorkUnits( numThreadsArg.getValue() );
116 
117  matcher->SetPercentageKept( percentageKeptArg.getValue() );
118 
119  matcher->SetTransformInitializationType((PyramidBMType::InitializationType)initTypeArg.getValue());
120 
121  matcher->SetResultFile(outArg.getValue());
122  matcher->SetOutputTransformFile(outputTransformArg.getValue());
123  matcher->SetOutputNearestRigidTransformFile(outputNRTransformArg.getValue());
124  matcher->SetOutputNearestSimilarityTransformFile(outputNSTransformArg.getValue());
125 
126  matcher->SetReferenceImage(anima::readImage <InputImageType> (fixedArg.getValue()));
127  matcher->SetFloatingImage(anima::readImage <InputImageType> (movingArg.getValue()));
128 
129  if (initialTransformArg.getValue() != "")
130  matcher->SetInitialTransform(initialTransformArg.getValue());
131 
132  if (directionTransformArg.getValue() != "")
133  matcher->SetDirectionTransform(directionTransformArg.getValue());
134 
135  AffineTransformPointer tmpTrsf = AffineTransformType::New();
136  tmpTrsf->SetIdentity();
137 
138  matcher->SetOutputTransform(tmpTrsf.GetPointer());
139 
140  // Process
141  itk::TimeProbe timer;
142  timer.Start();
143 
144  try
145  {
146  matcher->Update();
147  matcher->WriteOutputs();
148  }
149  catch (itk::ExceptionObject &e)
150  {
151  std::cerr << e << std::endl;
152  return EXIT_FAILURE;
153  }
154 
155  timer.Stop();
156 
157  std::cout << "Elapsed Time: " << timer.GetTotal() << timer.GetUnit() << std::endl;
158 
159  return EXIT_SUCCESS;
160 }
int main(int argc, const char **argv)