void RunRigid() { _initialTransform = InitialTransform::New(); _initialTransform->SetFixedParameters(_centerOfRotation); OptiReporter::Pointer optiReporter = OptiReporter::New(); Metric::Pointer metric = Metric::New(); metric->SetFixedImage(_dst); metric->SetMovingImage(_src); metric->SetInterpolator(Interpolator::New()); metric->SetTransform(_initialTransform); //metric->SetFixedImageRegion(_dst->GetBufferedRegion()); metric->SetFixedImageIndexes(_labelIndexes); // metric->SetUseAllPixels(true); metric->Initialize(); Optimizer::Pointer opti = Optimizer::New(); opti->SetCostFunction(metric); Optimizer::ScalesType scales; scales.SetSize(InitialTransform::ParametersDimension); scales.Fill(1); //scales[0] = scales[1] = scales[2] = .1; opti->SetUseUnitLengthGradient(true); opti->SetScales(scales); opti->SetInitialPosition(_initialTransform->GetParameters()); opti->AddObserver(itk::StartEvent(), optiReporter); opti->AddObserver(itk::IterationEvent(), optiReporter); opti->StartOptimization(); cout << "Current Cost: " << opti->GetCurrentCost() << endl; _initialResult = opti->GetCurrentPosition(); _initialTransform->SetParameters(opti->GetCurrentPosition()); }
void RunNine() { _finalTransform = NineTransform::New(); // NineTransform::OutputVectorType initialTranslation; // initialTranslation[0] = _initialResult[0]; // initialTranslation[1] = _initialResult[1]; // initialTranslation[2] = _initialResult[2]; // _finalTransform->SetTranslation(initialTranslation); _finalTransform->SetFixedParameters(_centerOfRotation); OptiReporter::Pointer optiReporter = OptiReporter::New(); Metric::Pointer metric = Metric::New(); metric->SetFixedImage(_dst); metric->SetMovingImage(_src); metric->SetInterpolator(InterpolatorNN::New()); metric->SetTransform(_finalTransform); metric->SetFixedImageIndexes(_labelIndexes); // metric->SetUseAllPixels(true); metric->Initialize(); Optimizer::Pointer opti = Optimizer::New(); opti->SetCostFunction(metric); Optimizer::ScalesType scales; scales.SetSize(NineTransform::ParametersDimension); scales.Fill(1); scales[0] = scales[1] = scales[2] = 10; scales[6] = scales[7] = scales[8] = 100; opti->SetMaximumIteration(500); opti->SetUseUnitLengthGradient(true); opti->SetStepLength(0.5); opti->SetScales(scales); opti->SetInitialPosition(_finalTransform->GetParameters()); opti->AddObserver(itk::StartEvent(), optiReporter); opti->AddObserver(itk::IterationEvent(), optiReporter); opti->StartOptimization(); cout << "Current Cost: " << opti->GetCurrentCost() << endl; _finalResult = opti->GetCurrentPosition(); _finalTransform->SetParameters(opti->GetCurrentPosition()); }
void RunRegistration() { _transform = TransformType::New(); OptiReporter::Pointer optiReporter = OptiReporter::New(); Metric::Pointer metric = Metric::New(); metric->SetFixedImage(_dst); bool useIndexes = false; if (useIndexes) { _centerOfRotation.SetSize(ImageType::ImageDimension); for (int i = 0; i < ImageType::ImageDimension; i++) { _centerOfRotation[i] = i; } itk::ImageRegionConstIteratorWithIndex<LabelType> labelIter(_dstLabel, _dstLabel->GetBufferedRegion()); int nPixels = 0; for (labelIter.GoToBegin(); !labelIter.IsAtEnd(); ++labelIter) { LabelType::PixelType label = labelIter.Get(); if (label > 0) { _labelIndexes.push_back(labelIter.GetIndex()); for (int i = 0; i < ImageType::ImageDimension; i++) { _centerOfRotation[i] += labelIter.GetIndex()[i]; } nPixels ++; } } for (int i = 0; i < ImageType::ImageDimension; i++) { _centerOfRotation[i] /= nPixels; } metric->SetFixedImageIndexes(_labelIndexes); _transform->SetFixedParameters(_centerOfRotation); } else { metric->SetFixedImageRegion(_dst->GetBufferedRegion()); metric->SetUseAllPixels(true); _centerOfRotation.SetSize(ImageType::ImageDimension); for (int i = 0; i < 3; i++) { _centerOfRotation[i] = _dstCenter[i]; } _transform->SetFixedParameters(_centerOfRotation); } cout << "Fixed Parameters: " << _centerOfRotation << endl; metric->SetMovingImage(_src); metric->SetInterpolator(Interpolator::New()); metric->SetTransform(_transform); metric->Initialize(); Optimizer::Pointer opti = Optimizer::New(); opti->SetCostFunction(metric); Optimizer::ScalesType scales; scales.SetSize(TransformType::ParametersDimension); scales.Fill(1); if (_method == "affine") { cout << "apply affine scaling ..." << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) { scales[3*i+j] = 160; } else { scales[3*i+j] = 30; } } } scales[9] = scales[10] = scales[11] = 0.1; } else if (_method == "scale") { scales[0] = scales[1] = scales[2] = 30; scales[3] = scales[4] = scales[5] = .5; scales[6] = scales[7] = scales[8] = 100; } else if (_method == "similar") { scales[0] = scales[1] = scales[2] = 10; scales[3] = scales[4] = scales[5] = 0.5; scales[6] = 100; } opti->SetScales(scales); const int maxIters = 100; #ifdef USE_CG_OPTIMIZER opti->SetMaximumIteration(maxIters); opti->SetMaximumLineIteration(10); opti->SetUseUnitLengthGradient(true); opti->SetStepLength(1); opti->SetToFletchReeves(); #endif #ifdef USE_GD_OPTIMIZER opti->SetNumberOfIterations(maxIters); opti->SetMinimumStepLength(1e-4); opti->SetMaximumStepLength(3); opti->SetRelaxationFactor(.5); opti->SetGradientMagnitudeTolerance(1e-4); #endif opti->SetInitialPosition(_transform->GetParameters()); opti->AddObserver(itk::StartEvent(), optiReporter); opti->AddObserver(itk::IterationEvent(), optiReporter); opti->StartOptimization(); cout << "Current Cost: " << opti->GetValue() << endl; _transformResult = opti->GetCurrentPosition(); _transform->SetParameters(opti->GetCurrentPosition()); }