示例#1
0
void GammaVariate::optimize() {
  Optimizer::Pointer optimizer = Optimizer::New();
  itk::GammaCostFunction::Pointer myCostFunction = itk::GammaCostFunction::New();
  myCostFunction->SetData( samples );
  optimizer->SetCostFunction( myCostFunction );
  Optimizer::ParametersType p(5);
  p[0] = t0;
  p[1] = t0 + t0max;
  p[2] = y0;
  p[3] = y0 + y0max;
  p[4] = findAlpha();
  optimizer->SetInitialPosition( p );
  optimizer->SetMaximumIteration( MaximumNumberOfIterations );
  optimizer->SetValueTolerance(1.0);
  try {
    optimizer->StartOptimization();
  } catch( itk::ExceptionObject & err ) {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
  }
  p = optimizer->GetCurrentPosition();
  setParameters( p[0], p[1], p[2], p[3], p[4]);
}
示例#2
0
    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());
    }
示例#3
0
    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());
    }
示例#4
0
    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());
    }