[Insight-users] Problem in image registration

Rafael S. Medeiros devilmaycrytherockbaby at gmail.com
Mon May 25 08:49:02 EDT 2009


Hello,

I`m trying to align two images in analyze format. However, the output is
always a blank image with a black line (always in the same place - around
the top of the image).
I use the class itkImageFileReader to read the two images and I don`t think
the problem is related with it. I think there`s something missing, but don`t
know what.

Here is my sourcecode: (C++)

void Alinhador::alinhar() {
    const    unsigned int    Dimension = 3;
    typedef  short           PixelType;

    // TIPOS
    typedef itk::VersorRigid3DTransform< double > TransformType;
    typedef itk::RegularStepGradientDescentOptimizer       OptimizerType;
    typedef itk::MattesMutualInformationImageToImageMetric<ImageType,
ImageType> MetricType;
    typedef itk::LinearInterpolateImageFunction<ImageType, double>
InterpolatorType;
    typedef itk::ImageRegistrationMethod< ImageType, ImageType >
RegistrationType;

    //INSTANCIAS
    MetricType::Pointer metric = MetricType::New();
    OptimizerType::Pointer optimizer = OptimizerType::New();
    InterpolatorType::Pointer interpolator = InterpolatorType::New();
    TransformType::Pointer transform = TransformType::New();
    RegistrationType::Pointer registration  = RegistrationType::New();

    metric->UseAllPixelsOn();

    registration->SetMetric(metric);
    registration->SetOptimizer(optimizer);
    registration->SetInterpolator(interpolator);
    registration->SetTransform(transform );

    registration->SetFixedImage(this->fixedImage);
    registration->SetMovingImage(this->movingImage);
    this->fixedImage->Update();

    registration->SetFixedImageRegion( this->fixedImage->GetBufferedRegion()
);


    //INITIALIZER
    typedef itk::CenteredTransformInitializer< TransformType, ImageType,
ImageType >  TransformInitializerType;
    TransformInitializerType::Pointer initializer =
TransformInitializerType::New();

    initializer->SetTransform(   transform );
    initializer->SetFixedImage(this->fixedImage );
    initializer->SetMovingImage(this->movingImage );
    //initializer->GeometryOn();
    initializer->MomentsOn();

    initializer->InitializeTransform();

    registration->SetInitialTransformParameters( transform->GetParameters()
);

    typedef OptimizerType::ScalesType       OptimizerScalesType;
    OptimizerScalesType optimizerScales( transform->GetNumberOfParameters()
);
    const double translationScale = 1.0 / 1000.0;

    std::cout<< transform->GetNumberOfParameters() << std::endl;
    optimizerScales[0] = 1.0;
    optimizerScales[1] = 1.0;
    optimizerScales[2] = 1.0;
    optimizerScales[3] = translationScale;
    optimizerScales[4] = translationScale;
    optimizerScales[5] = translationScale;

    optimizer->SetScales( optimizerScales );

    optimizer->SetMaximumStepLength( 1.0  );
    optimizer->SetMinimumStepLength( 0.06 );

    optimizer->SetNumberOfIterations( 100 );


    CommandIterationUpdate::Pointer observer =
CommandIterationUpdate::New();
    optimizer->AddObserver( itk::IterationEvent(), observer );


    try
    {
        registration->StartRegistration();
    }
    catch( itk::ExceptionObject & err )
    {
        std::cerr << "ExceptionObject caught !" << std::endl;
        std::cerr << err << std::endl;
        return;
    }

    OptimizerType::ParametersType finalParameters =
        registration->GetLastTransformParameters();


    const double versorX              = finalParameters[0];
    const double versorY              = finalParameters[1];
    const double versorZ              = finalParameters[2];
    const double finalTranslationX    = finalParameters[3];
    const double finalTranslationY    = finalParameters[4];
    const double finalTranslationZ    = finalParameters[5];

    const unsigned int numberOfIterations =
optimizer->GetCurrentIteration();

    const double bestValue = optimizer->GetValue();

    transform->SetParameters( finalParameters );

    TransformType::MatrixType matrix = transform->GetRotationMatrix();
    TransformType::OffsetType offset = transform->GetOffset();

    std::cout << "Matrix = " << std::endl << matrix << std::endl;
    std::cout << "Offset = " << std::endl << offset << std::endl;


    TransformType::Pointer finalTransform = TransformType::New();

    finalTransform->SetCenter( transform->GetCenter() );

    finalTransform->SetParameters( finalParameters );

    resampler = ResampleFilterType::New();

    resampler->SetTransform( finalTransform );
    resampler->SetInput( this->movingImage );

    ImageType::Pointer fixedImage = this->fixedImage;

    resampler->SetSize(    fixedImage->GetLargestPossibleRegion().GetSize()
);
    resampler->SetOutputOrigin(  fixedImage->GetOrigin() );
    resampler->SetOutputSpacing( fixedImage->GetSpacing() );
    resampler->SetDefaultPixelValue( 100 );
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090525/d586f64b/attachment-0001.htm>


More information about the Insight-users mailing list