Hi All:<br><br>After trial and error in my program that include Mattes MI, Affine transform, and RegularStepGradientDescentOptimizer.<br>The result is also appearing dispiritedly.<br><br>I have several questions and appreciated with your help.
<br><br>1)Is it right to simply set Mattes MI using <br> metric->SetNumberOfHistogramBins( 50 );<br> metric->SetNumberOfSpatialSamples( 10000 );<br> metric->ReinitializeSeed( 76926294 ); ?<br><br>2)Is it right to set Optimizer with
<br> optimizerScales[0] = 1.0;<br> optimizerScales[1] = 1.0;<br> optimizerScales[2] = 1.0;<br> optimizerScales[3] = 1.0;<br> optimizerScales[4] = 1/1000000;<br> optimizerScales[5] = 1/1000000;<br> optimizer->SetScales( optimizerScales );
<br> optimizer->MinimizeOn();<br> optimizer->SetMaximumStepLength( 0.5 ); <br> optimizer->SetMinimumStepLength( 0.1 );<br> optimizer->SetNumberOfIterations( 200 );<br> optimizer->SetRelaxationFactor( 0.8
);<br><br>3) How to determine an appropriate parameter for Optimizer?<br><br>My program is following as below, If anyone can give me any suggestion I will appreciate with your help.<br><br>Goo :)<br><br>=======================================================================
<br><br>int main( int argc, char *argv[] )<br>{<br> if( argc < 4 )<br> {<br> std::cerr << "Missing Parameters " << std::endl;<br> std::cerr << "Usage: " << argv[0];
<br> std::cerr << " fixedImageFile movingImageFile " << std::endl;<br> std::cerr << " outputImagefile [differenceBeforeRegistration] " << std::endl;<br> std::cerr << " [differenceAfterRegistration] " << std::endl;
<br> std::cerr << " [stepLength] [maxNumberOfIterations] "<< std::endl;<br> return 1;<br> }<br><br> const unsigned int Dimension = 2;<br> typedef float PixelType;<br><br>
typedef itk::Image< PixelType, Dimension > FixedImageType;<br> typedef itk::Image< PixelType, Dimension > MovingImageType;<br><br> typedef itk::AffineTransform< double, Dimension > TransformType;
<br><br> typedef itk::RegularStepGradientDescentOptimizer OptimizerType;<br><br><br> typedef itk::MattesMutualInformationImageToImageMetric< <br> FixedImageType, <br> MovingImageType > MetricType;
<br><br><br> typedef itk:: LinearInterpolateImageFunction< <br> MovingImageType,<br> double > InterpolatorType;<br> <br> typedef itk::ImageRegistrationMethod<
<br> FixedImageType, <br> MovingImageType > RegistrationType;<br><br> MetricType::Pointer metric = MetricType::New();<br> OptimizerType::Pointer optimizer = OptimizerType::New();
<br> InterpolatorType::Pointer interpolator = InterpolatorType::New();<br> RegistrationType::Pointer registration = RegistrationType::New();<br><br> registration->SetMetric( metric );<br> registration->SetOptimizer( optimizer );
<br> registration->SetInterpolator( interpolator );<br><br> TransformType::Pointer transform = TransformType::New();<br> registration->SetTransform( transform );<br><br><br> metric->SetNumberOfHistogramBins( 50 );
<br> metric->SetNumberOfSpatialSamples( 10000 );<br> metric->ReinitializeSeed( 76926294 );<br><br><br> typedef itk::ImageFileReader< FixedImageType > FixedImageReaderType;<br> typedef itk::ImageFileReader< MovingImageType > MovingImageReaderType;
<br> FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();<br> MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();<br> fixedImageReader->SetFileName( argv[1] );
<br> movingImageReader->SetFileName( argv[2] );<br><br><br> registration->SetFixedImage( fixedImageReader->GetOutput() );<br> registration->SetMovingImage( movingImageReader->GetOutput() );<br>
<br> fixedImageReader->Update();<br><br> registration->SetFixedImageRegion( <br> fixedImageReader->GetOutput()->GetBufferedRegion() );<br><br><br> typedef itk::CenteredTransformInitializer< <br> TransformType,
<br> FixedImageType, <br> MovingImageType > TransformInitializerType;<br> TransformInitializerType::Pointer initializer = TransformInitializerType::New();
<br> initializer->SetTransform( transform );<br> initializer->SetFixedImage( fixedImageReader->GetOutput() );<br> initializer->SetMovingImage( movingImageReader->GetOutput() );<br> initializer->MomentsOn();
<br> initializer->InitializeTransform();<br><br> registration->SetInitialTransformParameters( <br> transform->GetParameters() );<br><br><br> double translationScale = 1.0 / 100000.0
;<br> if( argc > 8 ){<br> translationScale = atof( argv[8] );<br> }<br><br> typedef OptimizerType::ScalesType OptimizerScalesType;<br> OptimizerScalesType optimizerScales( transform->GetNumberOfParameters() );
<br><br> optimizerScales[0] = 1.0;<br> optimizerScales[1] = 1.0;<br> optimizerScales[2] = 1.0;<br> optimizerScales[3] = 1.0;<br> optimizerScales[4] = 1/1000000;<br> optimizerScales[5] = 1/1000000;<br><br> optimizer->SetScales( optimizerScales );
<br><br><br> optimizer->MinimizeOn();<br> optimizer->SetMaximumStepLength( 0.5 ); <br> optimizer->SetMinimumStepLength( 0.1 );<br> optimizer->SetNumberOfIterations( 200 );<br> optimizer->SetRelaxationFactor(
0.8 );<br> <br><br> CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();<br> optimizer->AddObserver( itk::IterationEvent(), observer );<br><br> try <br> { <br> registration->StartRegistration();
<br> } <br> catch( itk::ExceptionObject & err ) <br> { <br> std::cerr << "ExceptionObject caught !" << std::endl; <br> std::cerr << err << std::endl; <br> return -1;<br>
} <br><br> OptimizerType::ParametersType finalParameters = <br> registration->GetLastTransformParameters();<br><br> const double finalRotationCenterX = transform->GetCenter()[0];<br> const double finalRotationCenterY = transform->GetCenter()[1];
<br> const double finalTranslationX = finalParameters[4];<br> const double finalTranslationY = finalParameters[5];<br><br> const unsigned int numberOfIterations = optimizer->GetCurrentIteration();<br> const double bestValue = optimizer->GetValue();
<br> <br> std::cout << "Result = " << std::endl;<br> std::cout << " Center X = " << finalRotationCenterX << std::endl;<br> std::cout << " Center Y = " << finalRotationCenterY << std::endl;
<br> std::cout << " Translation X = " << finalTranslationX << std::endl;<br> std::cout << " Translation Y = " << finalTranslationY << std::endl;<br> std::cout << " Iterations = " << numberOfIterations << std::endl;
<br> std::cout << " Metric value = " << bestValue << std::endl;<br> <br> vnl_matrix<double> p(2, 2);<br> p[0][0] = (double) finalParameters[0];<br> p[0][1] = (double) finalParameters[1];
<br> p[1][0] = (double) finalParameters[2];<br> p[1][1] = (double) finalParameters[3];<br> vnl_svd<double> svd(p);<br> vnl_matrix<double> r(2, 2);<br> r = svd.U() * vnl_transpose(svd.V());<br> double angle = asin(r[1][0]);
<br> <br> std::cout << " Scale 1 = " << svd.W(0) << std::endl;<br> std::cout << " Scale 2 = " << svd.W(1) << std::endl;
<br> std::cout << " Angle (degrees) = " << angle * 45.0 / atan(1.0) << std::endl;<br> <br><br> typedef itk::ResampleImageFilter< <br> MovingImageType, <br> FixedImageType > ResampleFilterType;
<br><br> TransformType::Pointer finalTransform = TransformType::New();<br><br> finalTransform->SetCenter( transform->GetCenter() );<br> finalTransform->SetParameters( finalParameters );<br><br> ResampleFilterType::Pointer resampler = ResampleFilterType::New();
<br><br> resampler->SetTransform( finalTransform );<br> resampler->SetInput( movingImageReader->GetOutput() );<br><br> FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();<br><br> resampler->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );
<br> resampler->SetOutputOrigin( fixedImage->GetOrigin() );<br> resampler->SetOutputSpacing( fixedImage->GetSpacing() );<br> resampler->SetDefaultPixelValue( 100 );<br> <br> typedef unsigned char OutputPixelType;
<br><br> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;<br> <br> typedef itk::CastImageFilter< <br> FixedImageType,<br> OutputImageType > CastFilterType;
<br> <br> typedef itk::ImageFileWriter< OutputImageType > WriterType;<br><br><br> WriterType::Pointer writer = WriterType::New();<br> CastFilterType::Pointer caster = CastFilterType::New();
<br><br><br> writer->SetFileName( argv[3] );<br><br><br> caster->SetInput( resampler->GetOutput() );<br> writer->SetInput( caster->GetOutput() );<br> writer->Update();<br><br><br> typedef itk::SubtractImageFilter<
<br> FixedImageType, <br> FixedImageType, <br> FixedImageType > DifferenceFilterType;<br><br> DifferenceFilterType::Pointer difference = DifferenceFilterType::New();
<br><br> difference->SetInput1( fixedImageReader->GetOutput() );<br> difference->SetInput2( resampler->GetOutput() );<br><br> WriterType::Pointer writer2 = WriterType::New();<br> <br> typedef itk::RescaleIntensityImageFilter<
<br> FixedImageType, <br> OutputImageType > RescalerType;<br><br> RescalerType::Pointer intensityRescaler = RescalerType::New();<br><br> intensityRescaler->SetInput( difference->GetOutput() );
<br> intensityRescaler->SetOutputMinimum( 0 );<br> intensityRescaler->SetOutputMaximum( 255 );<br> <br> writer2->SetInput( intensityRescaler->GetOutput() ); <br> resampler->SetDefaultPixelValue( 1 );
<br> <br><br> if( argc > 5 )<br> {<br> writer2->SetFileName( argv[5] );<br> writer2->Update();<br> }<br><br><br> typedef itk::IdentityTransform< double, Dimension > IdentityTransformType;<br> IdentityTransformType::Pointer identity = IdentityTransformType::New();
<br><br> if( argc > 4 )<br> {<br> resampler->SetTransform( identity );<br> writer2->SetFileName( argv[4] );<br> writer2->Update();<br> }<br><br><br> return 0;<br>}<br><br>==================================================================
<br>