[Insight-users] re gistration error RegularStepGradientDescentOptimizer
Luis Ibanez
luis.ibanez at kitware.com
Mon Feb 21 18:37:55 EST 2011
Hi Esme,
The CenteredSimilarity2DTransform has six parameters:
* The serialization of the optimizable parameters is an array of 6 elements
* ordered as follows:
* p[0] = scale
* p[1] = angle
* p[2] = x coordinate of the center
* p[3] = y coordinate of the center
* p[4] = x component of the translation
* p[5] = y component of the translation
http://www.itk.org/Doxygen/html/classitk_1_1CenteredSimilarity2DTransform.html
How do you arrive to find that the CostFunction (The metric ?) has
a NumberOfParameters = 3 ?
The Metric and the Optimizer normally get their number of parameters from the
transform at run time.
Luis
------------------------------------------------------------------------
On Mon, Feb 21, 2011 at 5:56 AM, EsmeRuiz
<esmeralda.ruizpujadas at gmail.com> wrote:
>
> Hello, I am new with ITK. And I have tried to do a model based registration
> using regular step gradient descent optimizer and I get this error
>
> The size of Scales 6, but the NumberOfParameters for the CostFunction is 3.
>
> I don't understand what it means.
>
> The code is:
>
> void registrations::modelBasedRegistration(float **imgFIXED, int size_row,
> int size_col, int absFrame){
>
>
> typedef itk::GroupSpatialObject< 2 > GroupType;
> typedef itk::EllipseSpatialObject< 2 > EllipseType;
> typedef itk::Image< float, 2 > ImageType;
>
> EllipseType::Pointer ellipse1 = EllipseType::New();
> EllipseType::Pointer ellipse2 = EllipseType::New();
> //EllipseType::Pointer ellipse3 = EllipseType::New();
>
>
> ellipse1->SetRadius( 1.0 );
> ellipse2->SetRadius( 2.0 );
>
>
> // Software Guide : BeginCodeSnippet
> EllipseType::TransformType::OffsetType offset;
> offset[ 0 ] = (int)size_col/2.0;
> offset[ 1 ] = (int)size_row/2.0;
>
> ellipse1->GetObjectToParentTransform()->SetOffset(offset);
> ellipse1->ComputeObjectToWorldTransform();
>
>
> offset[ 0 ] = (int)size_col/2.0;
> offset[ 1 ] = (int)size_row/2.0;
>
> ellipse2->GetObjectToParentTransform()->SetOffset(offset);
> ellipse2->ComputeObjectToWorldTransform();
>
> // Software Guide : BeginCodeSnippet
> GroupType::Pointer group = GroupType::New();
> group->AddSpatialObject( ellipse1 );
> group->AddSpatialObject( ellipse2 );
>
>
> // Software Guide : BeginCodeSnippet
> typedef itk::SpatialObjectToImageFilter< GroupType, ImageType >
> SpatialObjectToImageFilterType;
> // Software Guide : EndCodeSnippet
>
>
> // Software Guide : BeginCodeSnippet
> SpatialObjectToImageFilterType::Pointer imageFilter =
> SpatialObjectToImageFilterType::New();
> // Software Guide : EndCodeSnippet
>
>
> // Software Guide : BeginCodeSnippet
> imageFilter->SetInput( group );
> // Software Guide : EndCodeSnippet
>
>
> // Software Guide : BeginCodeSnippet
> ImageType::SizeType size;
> size[ 0 ] = 200;
> size[ 1 ] = 200;
> imageFilter->SetSize( size );
>
>
> // Software Guide : BeginCodeSnippet
> imageFilter->Update();
> // Software Guide : EndCodeSnippet
>
>
> // Software Guide : BeginCodeSnippet
> typedef itk::DiscreteGaussianImageFilter< ImageType, ImageType >
> GaussianFilterType;
> GaussianFilterType::Pointer gaussianFilter =
> GaussianFilterType::New();
> // Software Guide : EndCodeSnippet
>
>
> // Software Guide : BeginCodeSnippet
> gaussianFilter->SetInput( imageFilter->GetOutput() );
> // Software Guide : EndCodeSnippet
>
>
> // Software Guide : BeginCodeSnippet
> const double variance = 20;
> gaussianFilter->SetVariance(variance);
> gaussianFilter->Update();
> // Software Guide : EndCodeSnippet
>
>
> // Software Guide : BeginCodeSnippet
> typedef itk::ImageToSpatialObjectRegistrationMethod< ImageType, GroupType
>> RegistrationType;
> RegistrationType::Pointer registration = RegistrationType::New();
>
> typedef SimpleImageToSpatialObjectMetric< ImageType, GroupType >
> MetricType;
> MetricType::Pointer metric = MetricType::New();
> // Software Guide : EndCodeSnippet
>
>
>
> // Software Guide : BeginCodeSnippet
> typedef itk::LinearInterpolateImageFunction< ImageType, double >
> InterpolatorType;
> InterpolatorType::Pointer interpolator = InterpolatorType::New();
> // Software Guide : EndCodeSnippet
>
>
>
> typedef itk::RegularStepGradientDescentOptimizer OptimizerType;
> OptimizerType::Pointer optimizer = OptimizerType::New();
>
>
>
> typedef itk::CenteredSimilarity2DTransform< double > TransformType;
> TransformType::Pointer transform = TransformType::New();
>
>
>
>
>
> double steplength = 0.1; // 1.0
> optimizer->SetMaximumStepLength( steplength );
> optimizer->SetMinimumStepLength( 0.0001 );
>
> optimizer->SetNumberOfIterations( 500 );
> typedef OptimizerType::ScalesType OptimizerScalesType;
> int numOfParameters = transform->GetNumberOfParameters();
> OptimizerScalesType optimizerScales( transform->GetNumberOfParameters()
> );
> const double translationScale = 1.0 / 100.0;
>
> optimizerScales[0] = 10.0;
> optimizerScales[1] = 1.0;
> optimizerScales[2] = translationScale;
> optimizerScales[3] = translationScale;
> optimizerScales[4] = translationScale;
> optimizerScales[5] = translationScale;
>
> optimizer->SetScales( optimizerScales );
>
> CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
> optimizer->AddObserver( itk::IterationEvent(), observer );
>
>
> registration->SetFixedImage( gaussianFilter->GetOutput() );
> registration->SetMovingSpatialObject( group );
> registration->SetTransform( transform );
> registration->SetInterpolator( interpolator );
> registration->SetOptimizer( optimizer );
> registration->SetMetric( metric );
>
> typedef float InputPixelType;
> typedef itk::Image< InputPixelType, 2 > InputImageType;
>
> typedef itk::CenteredTransformInitializer<
> TransformType,
> InputImageType,
> ImageType > TransformInitializerType;
>
> TransformInitializerType::Pointer initializer =
> TransformInitializerType::New();
>
> initializer->SetTransform( transform );
> FixedImageType::Pointer fixedImageReader = FixedImageType::New();
> CreateImage(fixedImageReader, size_row, size_col,imgFIXED);
> initializer->SetFixedImage( fixedImageReader );
> initializer->SetMovingImage( imageFilter->GetOutput() );
>
> initializer->MomentsOn();
>
> initializer->InitializeTransform();
>
> double initialScale = 1.0;
>
> double initialAngle = 0.5;
>
> transform->SetScale( initialScale );
> transform->SetAngle( initialAngle );
>
>
>
> registration->SetInitialTransformParameters( transform->GetParameters()
> );
> //optimizer->MaximizeOn();
>
> //Finally, we trigger the execution of the registration process with the
> // \code{StartRegistration()} method. We place this call in a
> // \code{try/catch} block in case any exception is thrown during the
> // process.
>
> try
> {
> registration->StartRegistration();
> }
> catch( itk::ExceptionObject & err )
> {
> std::cerr << "ExceptionObject caught !" << std::endl;
> std::cerr << err << std::endl;
> return ;
> }
>
>
> // Software Guide : BeginCodeSnippet
> RegistrationType::ParametersType finalParameters
> = registration->GetLastTransformParameters();
>
> std::cout << "Final Solution is : " << finalParameters << std::endl;
> // Software Guide : EndCodeSnippet
>
>
>
>
>
>
>
> typedef itk::ResampleImageFilter<ImageType,
> FixedImageType > ResampleFilterType;
>
> TransformType::Pointer finalTransform = TransformType::New();
>
> finalTransform->SetParameters( finalParameters );
>
> ResampleFilterType::Pointer resampler = ResampleFilterType::New();
>
> resampler->SetTransform( finalTransform );
> resampler->SetInput( imageFilter->GetOutput() );
>
> ImageType::Pointer fixedImage = fixedImageReader;
>
> resampler->SetSize( fixedImage->GetLargestPossibleRegion().GetSize()
> );
> resampler->SetOutputOrigin( fixedImage->GetOrigin() );
> resampler->SetOutputSpacing( fixedImage->GetSpacing() );
> resampler->SetDefaultPixelValue( 0 );
>
> typedef unsigned char OutputPixelType;
>
> typedef itk::Image< OutputPixelType, 2> OutputImageType;
>
> typedef itk::CastImageFilter< FixedImageType, OutputImageType >
> CastFilterType;
>
> typedef itk::ImageFileWriter< OutputImageType > writerType;
>
>
> writerType::Pointer writer2 = writerType::New();
> CastFilterType::Pointer caster = CastFilterType::New();
>
> writer2->SetFileName( "reg.jpg" );
>
> caster->SetInput( resampler->GetOutput() );
> writer2->SetInput( caster->GetOutput() );
> try
> {
> writer2->Update();
> }
> catch( itk::ExceptionObject & err )
> {
> std::cerr << "ExceptionObject caught !" << std::endl;
> std::cerr << err << std::endl;
> return ;
> }
>
>
> return;
>
> }
>
> Thank you very much !!
> --
> View this message in context: http://old.nabble.com/registration-error-RegularStepGradientDescentOptimizer-tp30976153p30976153.html
> Sent from the ITK - Users mailing list archive at Nabble.com.
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
More information about the Insight-users
mailing list