30 #pragma warning ( disable : 4786 )
42 #include "itkTranslationTransform.h"
43 #include "itkEuclideanDistancePointMetric.h"
44 #include "itkLevenbergMarquardtOptimizer.h"
45 #include "itkPointSet.h"
46 #include "itkPointSetToPointSetRegistrationMethod.h"
67 void Execute(itk::Object *caller,
const itk::EventObject & event)
69 Execute((
const itk::Object *) caller, event);
72 void Execute(
const itk::Object *
object,
const itk::EventObject & event)
76 if (!itk::IterationEvent().CheckEvent(&event))
81 std::cout <<
"iteration Value = " << optimizer->GetCachedValue() << std::endl;
82 std::cout <<
"iteration Position = " << optimizer->GetCachedCurrentPosition();
83 std::cout << std::endl << std::endl;
105 typedef itk::EuclideanDistancePointMetric<PointSetType, PointSetType>
MetricType;
121 typedef itk::PointSetToPointSetRegistrationMethod<PointSetType, PointSetType>
RegistrationType;
126 bool registerPoints(std::vector<cx::Vector3D> ref, std::vector<cx::Vector3D> target);
127 bool registerPoints(PointSetType::Pointer fixedPointSet, PointSetType::Pointer movingPointSet);
128 PointSetType::Pointer
toItk(std::vector<cx::Vector3D> input);
135 PointSetType::Pointer pointSet = PointSetType::New();
136 PointsContainer::Pointer pointContainer = PointsContainer::New();
139 for (
unsigned i = 0; i < input.size(); ++i)
141 point[0] = input[i][0];
142 point[1] = input[i][1];
143 point[2] = input[i][2];
144 pointContainer->InsertElement(i, point);
146 pointSet->SetPoints(pointContainer);
161 MetricType::Pointer metric = MetricType::New();
167 TransformType::Pointer
transform = TransformType::New();
170 OptimizerType::Pointer optimizer = OptimizerType::New();
171 optimizer->SetUseCostFunctionGradient(
false);
174 RegistrationType::Pointer registration = RegistrationType::New();
177 OptimizerType::ScalesType scales(
transform->GetNumberOfParameters());
178 std::cout <<
"transform->GetNumberOfParameters() " <<
transform->GetNumberOfParameters() << std::endl;
181 unsigned long numberOfIterations = 100;
182 double gradientTolerance = 1e-5;
183 double valueTolerance = 1e-5;
184 double epsilonFunction = 1e-6;
186 optimizer->SetScales(scales);
187 optimizer->SetNumberOfIterations(numberOfIterations);
188 optimizer->SetValueTolerance(valueTolerance);
189 optimizer->SetGradientTolerance(gradientTolerance);
190 optimizer->SetEpsilonFunction(epsilonFunction);
196 registration->SetInitialTransformParameters(
transform->GetParameters());
201 registration->SetMetric(metric);
202 registration->SetOptimizer(optimizer);
204 registration->SetFixedPointSet(fixedPointSet);
205 registration->SetMovingPointSet(movingPointSet);
214 registration->Update();
215 }
catch (itk::ExceptionObject & e)
217 std::cout << e << std::endl;
221 mResult = cx::Transform3D::Identity();
222 for (
unsigned i = 0; i <
transform->GetNumberOfParameters(); ++i)
243 std::vector<Vector3D> target,
bool* ok)
245 if (ref.size() != target.size() || ref.empty())
247 std::cout <<
"Different sizes in ref and target: aborting registration." << std::endl;
249 return Transform3D::Identity();
261 Vector3D rr = (ref[0] + ref[1]) / 2.0;
262 Vector3D tt = (target[0] + target[1]) / 2.0;