ANIMA  4.0
animaBMDistortionCorrection.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 
5 
6 #include <itkTimeProbe.h>
7 
8 int main(int argc, const char** argv)
9 {
11  typedef PyramidBMType::InputImageType InputImageType;
12  typedef PyramidBMType::VectorFieldType VectorFieldType;
13 
14  // Parsing arguments
15  TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION);
16 
17  // Setting up parameters
18  TCLAP::ValueArg<std::string> backwardArg("b","backimage","Backward image (eg PA image)",true,"","backward image",cmd);
19  TCLAP::ValueArg<std::string> forwardArg("f","forwardimage","Forward image (eg AP image)",true,"","forward image",cmd);
20  TCLAP::ValueArg<std::string> outArg("o","outputimage","Output (corrected) image",true,"","output image",cmd);
21  TCLAP::ValueArg<std::string> initialTransformArg("i","initransform","Initial transformation",false,"","initial transform",cmd);
22  TCLAP::ValueArg<std::string> outputTransformArg("O","outtransform","Output transformation",false,"","output transform",cmd);
23 
24  TCLAP::ValueArg<unsigned int> blockSizeArg("","bs","Block size (default: 3)",false,3,"block size",cmd);
25  TCLAP::ValueArg<unsigned int> blockSpacingArg("","sp","Block spacing (default: 2)",false,2,"block spacing",cmd);
26  TCLAP::ValueArg<double> stdevThresholdArg("s","stdev","Threshold block standard deviation (default: 15)",false,15,"block minimal standard deviation",cmd);
27  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);
28 
29  TCLAP::ValueArg<unsigned int> maxIterationsArg("","mi","Maximum block match iterations (default: 10)",false,10,"maximum iterations",cmd);
30 
31  TCLAP::ValueArg<unsigned int> directionArg("d","dir","Gradient phase direction in image (default: 1 = Y axis)",false,1,"gradient phase direction",cmd);
32  TCLAP::ValueArg<unsigned int> optimizerMaxIterationsArg("","oi","Maximum iterations for local optimizer (default: 100)",false,100,"maximum local optimizer iterations",cmd);
33 
34  TCLAP::ValueArg<double> searchRadiusArg("","sr","Search radius in pixels (rho start for bobyqa, default: 2)",false,2,"optimizer search radius",cmd);
35  TCLAP::ValueArg<double> searchScaleRadiusArg("","scr","Search scale radius (rho start for bobyqa, default: 0.1)",false,0.1,"optimizer search scale radius",cmd);
36  TCLAP::ValueArg<double> searchSkewRadiusArg("","skr","Search skew radius (rho start for bobyqa, default: 0.1)",false,0.1,"optimizer search skew radius",cmd);
37  TCLAP::ValueArg<double> finalRadiusArg("","fr","Final radius (rho end for bobyqa, default: 0.001)",false,0.001,"optimizer final radius",cmd);
38 
39  TCLAP::ValueArg<double> translateUpperBoundArg("","tub","Upper bound on translation for bobyqa (in voxels, default: 10)",false,10,"Bobyqa translate upper bound",cmd);
40  TCLAP::ValueArg<double> scaleUpperBoundArg("","scu","Upper bound on scale for bobyqa (default: 5)",false,5,"Bobyqa scale upper bound",cmd);
41  TCLAP::ValueArg<double> skewUpperBoundArg("","sku","Upper bound on skew for bobyqa (in degrees, default: 45)",false,45,"Bobyqa skew upper bound",cmd);
42 
43  TCLAP::ValueArg<unsigned int> blockTransfoArg("t","in-transform","Transformation computed between blocks (0: direction, 1: direction+scale, 2: direction+scale+skew, default: 2)",false,2,"transformation between blocks",cmd);
44  TCLAP::ValueArg<unsigned int> agregatorArg("","agregator","Transformation agregator type (0: Baloo, 1: M-smoother, default: 0)",false,0,"agregator type",cmd);
45  TCLAP::ValueArg<unsigned int> blockMetricArg("","metric","Similarity metric between blocks (0: correlation coefficient, 1: squared correlation coefficient, 2: mean squares, default: 1)",false,1,"similarity metric",cmd);
46 
47  TCLAP::SwitchArg weightedAgregationArg("w","no-weighted-agregation", "If set, weighted agregation is deactivated", cmd, false);
48  TCLAP::ValueArg<double> extrapolationSigmaArg("","fs","Sigma for extrapolation of local pairings (default: 3)",false,3,"extrapolation sigma",cmd);
49  TCLAP::ValueArg<double> elasticSigmaArg("","es","Sigma for elastic regularization (default: 2)",false,2,"elastic regularization sigma",cmd);
50  TCLAP::ValueArg<double> outlierSigmaArg("","os","Sigma for outlier rejection among local pairings (default: 3)",false,3,"outlier rejection sigma",cmd);
51  TCLAP::ValueArg<double> mEstimateConvergenceThresholdArg("","met","Threshold to consider m-estimator converged (default: 0.01)",false,0.01,"m-estimation convergence threshold",cmd);
52  TCLAP::ValueArg<double> neighborhoodApproximationArg("","na","Half size of the neighborhood approximation (multiplied by extrapolation sigma, default: 2.5)",false,2.5,"half size of neighborhood approximation",cmd);
53 
54  TCLAP::ValueArg<unsigned int> expOrderArg("e","exp-order","Order of field exponentiation approximation (in between 0 and 1, default: 0)",false,0,"exponentiation order",cmd);
55 
56  TCLAP::ValueArg<unsigned int> numPyramidLevelsArg("p","pyr","Number of pyramid levels (default: 3)",false,3,"number of pyramid levels",cmd);
57  TCLAP::ValueArg<unsigned int> lastPyramidLevelArg("l","last-level","Index of the last pyramid level explored (default: 0)",false,0,"last pyramid level",cmd);
58  TCLAP::ValueArg<unsigned int> numThreadsArg("T","threads","Number of execution threads (default: 0 = all cores)",false,0,"number of threads",cmd);
59 
60  try
61  {
62  cmd.parse(argc,argv);
63  }
64  catch (TCLAP::ArgException& e)
65  {
66  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
67  return EXIT_FAILURE;
68  }
69 
70  PyramidBMType *matcher = new PyramidBMType;
71 
72  matcher->SetBackwardImage(anima::readImage<InputImageType>(backwardArg.getValue()).GetPointer());
73  matcher->SetForwardImage(anima::readImage<InputImageType>(forwardArg.getValue()).GetPointer());
74 
75  if (initialTransformArg.getValue() != "")
76  matcher->SetInitialTransformField(anima::readImage<VectorFieldType>(initialTransformArg.getValue()));
77 
78  // Setting matcher arguments
79  matcher->SetBlockSize( blockSizeArg.getValue() );
80  matcher->SetBlockSpacing( blockSpacingArg.getValue() );
81  matcher->SetStDevThreshold( stdevThresholdArg.getValue() );
82  matcher->SetMaximumIterations( maxIterationsArg.getValue() );
83  matcher->SetOptimizerMaximumIterations( optimizerMaxIterationsArg.getValue() );
84  matcher->SetSearchRadius( searchRadiusArg.getValue() );
85  matcher->SetSearchScaleRadius( searchScaleRadiusArg.getValue() );
86  matcher->SetSearchSkewRadius( searchSkewRadiusArg.getValue() );
87  matcher->SetFinalRadius(finalRadiusArg.getValue());
88  matcher->SetTranlateUpperBound( translateUpperBoundArg.getValue() );
89  matcher->SetScaleUpperBound( std::log(scaleUpperBoundArg.getValue()) );
90  matcher->SetSkewUpperBound( std::tan(skewUpperBoundArg.getValue() * M_PI / 180.0) );
91 
92  matcher->SetTransformKind( (TransformKind) blockTransfoArg.getValue() );
93  matcher->SetAgregator( (Agregator) agregatorArg.getValue() );
94  matcher->SetMetric( (Metric) blockMetricArg.getValue() );
95 
96  matcher->SetExtrapolationSigma(extrapolationSigmaArg.getValue());
97  matcher->SetElasticSigma(elasticSigmaArg.getValue());
98  matcher->SetOutlierSigma(outlierSigmaArg.getValue());
99  matcher->SetMEstimateConvergenceThreshold(mEstimateConvergenceThresholdArg.getValue());
100  matcher->SetNeighborhoodApproximation(neighborhoodApproximationArg.getValue());
101  matcher->SetWeightedAgregation( weightedAgregationArg.isSet() );
102  matcher->SetNumberOfPyramidLevels( numPyramidLevelsArg.getValue() );
103  matcher->SetLastPyramidLevel( lastPyramidLevelArg.getValue() );
104  matcher->SetTransformDirection(directionArg.getValue());
105  matcher->SetExponentiationOrder(expOrderArg.getValue());
106 
107  if (numThreadsArg.getValue() != 0)
108  matcher->SetNumberOfWorkUnits( numThreadsArg.getValue() );
109 
110  matcher->SetPercentageKept( percentageKeptArg.getValue() );
111 
112  matcher->SetResultFile( outArg.getValue() );
113  matcher->SetOutputTransformFile( outputTransformArg.getValue() );
114 
115  itk::TimeProbe timer;
116  timer.Start();
117 
118  try
119  {
120  matcher->Update();
121  matcher->WriteOutputs();
122  }
123  catch(itk::ExceptionObject &e)
124  {
125  std::cerr << "Error " << e << std::endl;
126  return EXIT_FAILURE;
127  }
128 
129  timer.Stop();
130 
131  std::cout << "Elapsed Time: " << timer.GetTotal() << timer.GetUnit() << std::endl;
132 
133  return EXIT_SUCCESS;
134 }
int main(int argc, const char **argv)