[Insight-users] On MultiResolutionImageRegistrationMethod
Sri Valli
valli_gummadi@yahoo.co.in
Sat, 1 Feb 2003 13:41:33 +0000 (GMT)
--0-755963147-1044106893=:68970
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Hi Lydia,
Thanks for ur suggestions. Well i could correct the problem which was coming before. Now again i face problem on rotation. This example MultiResImageRegistration2.cxx is not working for registering with a rotated image. I have pasted my code at the end of this mail. Please go through the code and send me the code which have to be added to it or to be modified. I am using a data set of (256 x 256 x 1). The pixel values are from 0 to 120. I have rotated the image in z-direction with angle 10 degrees. I gave this rotated image as moving image and normal image as fixed image to the application with code below. Please go through it and send me the missing code.
Thanks in advance.
-Regards,
Sateesh.
My code......Follows.....
if( argc < 3 )
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " fixedImageFile movingImageFile ";
std::cerr << " outputImagefile" << std::endl;
return 1;
}
const unsigned int Dimension = 2;
typedef unsigned short PixelType;
typedef itk::Image< PixelType, Dimension > FixedImageType;
typedef itk::Image< PixelType, Dimension > MovingImageType;
typedef float InternalPixelType;
typedef itk::AffineTransform< double, Dimension > TransformType;
// Software Guide : EndCodeSnippet
typedef itk::RegularStepGradientDescentOptimizer OptimizerType;
typedef itk::LinearInterpolateImageFunction<
InternalImageType,
double > InterpolatorType;
typedef itk::MattesMutualInformationImageToImageMetric<
InternalImageType,
InternalImageType > MetricType;
typedef OptimizerType::ScalesType OptimizerScalesType;
typedef itk::MultiResolutionImageRegistrationMethod<
InternalImageType,
InternalImageType > RegistrationType;
typedef itk::RecursiveMultiResolutionPyramidImageFilter<
InternalImageType,
InternalImageType > FixedImagePyramidType;
typedef itk::RecursiveMultiResolutionPyramidImageFilter<
InternalImageType,
InternalImageType > MovingImagePyramidType;
OptimizerType::Pointer optimizer = OptimizerType::New();
InterpolatorType::Pointer interpolator = InterpolatorType::New();
RegistrationType::Pointer registration = RegistrationType::New();
MetricType::Pointer metric = MetricType::New();
registration->SetOptimizer( optimizer );
registration->SetInterpolator( interpolator );
registration->SetMetric( metric );
std::cout << "\n Set :: Optimizer, interpolator, metric ";
TransformType::Pointer transform = TransformType::New();
registration->SetTransform( transform );
// Software Guide : EndCodeSnippet
std::cout << "\n Set :: Transform ";
FixedImagePyramidType::Pointer fixedImagePyramid =
FixedImagePyramidType::New();
MovingImagePyramidType::Pointer movingImagePyramid =
MovingImagePyramidType::New();
typedef itk::ImageFileReader< FixedImageType > FixedImageReaderType;
typedef itk::ImageFileReader< MovingImageType > MovingImageReaderType;
FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();
MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();
fixedImageReader->SetFileName( argv[1] );
movingImageReader->SetFileName( argv[2] );
int nx = 256, ny = 256, nz = 5;
typedef itk::RawImageIO<PixelType,Dimension> RawReaderType;
RawReaderType::Pointer rawReader = RawReaderType::New();
rawReader->SetDimensions( 0, nx );
rawReader->SetDimensions( 1, ny );
// rawReader->SetDimensions( 2, nz );
fixedImageReader->SetImageIO( rawReader );
try
{
fixedImageReader->Update();
}
catch( itk::ExceptionObject & e )
{
std::cerr << "Exception caught during Raw file reading " << std::endl;
std::cerr << e << std::endl;
return -1;
}
movingImageReader->SetImageIO( rawReader );
try
{
movingImageReader->Update();
}
catch( itk::ExceptionObject & e )
{
std::cerr << "Exception caught during Raw file reading " << std::endl;
std::cerr << e << std::endl;
return -1;
}
std::cout << "\n First file : " << argv[1] << std::endl;
std::cout << "Second file : " << argv[2] << std::endl;
typedef itk::CastImageFilter<
FixedImageType, InternalImageType > FixedCastFilterType;
typedef itk::CastImageFilter<
MovingImageType, InternalImageType > MovingCastFilterType;
FixedCastFilterType::Pointer fixedCaster = FixedCastFilterType::New();
MovingCastFilterType::Pointer movingCaster = MovingCastFilterType::New();
fixedCaster->SetInput( fixedImageReader->GetOutput() );
std::cout << "\n\n Set Fixed Caster";
movingCaster->SetInput( movingImageReader->GetOutput() );
std::cout << "\n\n Set Moving Caster";
registration->SetFixedImage( fixedCaster->GetOutput() );
std::cout << "\n\n SetFixedImage";
registration->SetMovingImage( movingCaster->GetOutput() );
std::cout << "\n\n SetMovingImage";
fixedCaster->Update();
std::cout << "\n\n Update";
registration->SetFixedImageRegion(fixedCaster->GetOutput()->GetBufferedRegion() );
std::cout << "\n\n SetFixedImageRegion";
transform->SetIdentity();
registration->SetInitialTransformParameters( transform->GetParameters() );
OptimizerScalesType optimizerScales( transform->GetNumberOfParameters() );
std::cout << "\nGet No of Params : " << transform->GetNumberOfParameters() << std::endl;
optimizerScales[0] = 1.0;// / 100.0; // scale for M11
optimizerScales[1] = 1.0;// / 100.0; // scale for M12
optimizerScales[2] = 1.0;// / 100.0; // scale for M21
optimizerScales[3] = 1.0;// / 100.0; // scale for M22
typedef itk::Image< InternalPixelType, Dimension > InternalImageType;
optimizerScales[4] = 1.0 / 1000000.0; // scale for translation on Y
optimizerScales[5] = 1.0 / 1000000.0; // scale for translation on Z
optimizer->SetScales( optimizerScales );
metric->SetNumberOfHistogramBins( 50 );
metric->SetNumberOfSpatialSamples( 1000 );
optimizer->SetNumberOfIterations( 200 );
CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
optimizer->AddObserver( itk::IterationEvent(), observer );
typedef RegistrationInterfaceCommand<RegistrationType> CommandType;
CommandType::Pointer command = CommandType::New();
registration->AddObserver( itk::IterationEvent(), command );
registration->SetNumberOfLevels( 3 );
std::cout << "\n Before Registration ";
try
{
registration->StartRegistration();
}
catch( itk::ExceptionObject & err )
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return -1;
}
std::cout << "Hello";
typedef RegistrationType::ParametersType ParametersType;
ParametersType finalParameters = registration->GetLastTransformParameters();
double TranslationAlongX = finalParameters[4];
double TranslationAlongY = finalParameters[5];
unsigned int numberOfIterations = optimizer->GetCurrentIteration();
double bestValue = optimizer->GetValue();
//
// Print out results
//
std::cout << "Result = " << std::endl;
std::cout << " Translation X = " << TranslationAlongX << std::endl;
std::cout << " Translation Y = " << TranslationAlongY << std::endl;
std::cout << " Iterations = " << numberOfIterations << std::endl;
std::cout << " Metric value = " << bestValue << std::endl;
typedef itk::ResampleImageFilter<
MovingImageType,
FixedImageType > ResampleFilterType;
TransformType::Pointer finalTransform = TransformType::New();
finalTransform->SetParameters( finalParameters );
finalTransform->PrintSelf(std::cout,NULL);
ResampleFilterType::Pointer resample = ResampleFilterType::New();
resample->SetTransform( finalTransform );
resample->SetInput( movingImageReader->GetOutput() );
FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();
resample->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );
resample->SetOutputOrigin( fixedImage->GetOrigin() );
resample->SetOutputSpacing( fixedImage->GetSpacing() );
resample->SetDefaultPixelValue( 100 );
typedef short OutputPixelType;
typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
typedef itk::CastImageFilter<
FixedImageType,
OutputImageType > CastFilterType;
typedef itk::ImageFileWriter< OutputImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
CastFilterType::Pointer caster = CastFilterType::New();
writer->SetFileName( argv[3] );
std::cout << "\n OutPut : " << argv[3] << std::endl;
caster->SetInput( resample->GetOutput() );
writer->SetInput( caster->GetOutput() );
writer->SetImageIO(rawReader);
std::cout << "\n Writer : SetInput, SetImageIO";
try
{
writer->Write();
std::cout << "\n Write()";
}
catch( itk::ExceptionObject & e )
{
std::cerr << "Exception caught during Raw file writing " << std::endl;
std::cerr << e << std::endl;
return -1;
}
std::cout << "File succesfully writen ! " << std::endl;
Catch all the cricket action. Download Yahoo! Score tracker
--0-755963147-1044106893=:68970
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
<P>Hi Lydia,</P>
<P> Thanks for ur suggestions. Well i could correct the problem which was coming before. Now again i face problem on rotation. This example MultiResImageRegistration2.cxx is not working for registering with a rotated image. I have pasted my code at the end of this mail. Please go through the code and send me the code which have to be added to it or to be modified. I am using a data set of (256 x 256 x 1). The pixel values are from 0 to 120. I have rotated the image in z-direction with angle 10 degrees. I gave this rotated image as moving image and normal image as fixed image to the application with code below. Please go through it and send me the missing code.</P>
<P> Thanks in advance.</P>
<P>-Regards,<BR> Sateesh.</P>
<P> </P>
<P>My code......Follows.....</P>
<P> if( argc < 3 )<BR> {<BR> std::cerr << "Missing Parameters " << std::endl;<BR> std::cerr << "Usage: " << argv[0];<BR> std::cerr << " fixedImageFile movingImageFile ";<BR> std::cerr << " outputImagefile" << std::endl;<BR> return 1;<BR> }<BR> <BR> const unsigned int Dimension = 2;<BR> typedef unsigned short PixelType;<BR> <BR> typedef itk::Image< PixelType, Dimension > FixedImageType;<BR> typedef itk::Image< PixelType, Dimension > MovingImageType;</P>
<P> typedef float InternalPixelType;</P>
<P> typedef itk::AffineTransform< double, Dimension > TransformType;<BR> // Software Guide : EndCodeSnippet</P>
<P> </P>
<P> typedef itk::RegularStepGradientDescentOptimizer OptimizerType;<BR> typedef itk::LinearInterpolateImageFunction< <BR> InternalImageType,<BR> double > InterpolatorType;<BR> typedef itk::MattesMutualInformationImageToImageMetric< <BR> InternalImageType, <BR> InternalImageType > MetricType;</P>
<P> typedef OptimizerType::ScalesType OptimizerScalesType;</P>
<P><BR> typedef itk::MultiResolutionImageRegistrationMethod< <BR> InternalImageType, <BR> InternalImageType > RegistrationType;</P>
<P> typedef itk::RecursiveMultiResolutionPyramidImageFilter<<BR> InternalImageType,<BR> InternalImageType > FixedImagePyramidType;</P>
<P> <BR> typedef itk::RecursiveMultiResolutionPyramidImageFilter<<BR> InternalImageType,<BR> InternalImageType > MovingImagePyramidType;</P>
<P><BR> OptimizerType::Pointer optimizer = OptimizerType::New();<BR> InterpolatorType::Pointer interpolator = InterpolatorType::New();<BR> RegistrationType::Pointer registration = RegistrationType::New();<BR> MetricType::Pointer metric = MetricType::New();</P>
<P> registration->SetOptimizer( optimizer );<BR> registration->SetInterpolator( interpolator );<BR> registration->SetMetric( metric );</P>
<P> std::cout << "\n Set :: Optimizer, interpolator, metric ";<BR> <BR> TransformType::Pointer transform = TransformType::New();</P>
<P> registration->SetTransform( transform );<BR> // Software Guide : EndCodeSnippet</P>
<P><BR> std::cout << "\n Set :: Transform ";</P>
<P> </P>
<P> </P>
<P> FixedImagePyramidType::Pointer fixedImagePyramid = <BR> FixedImagePyramidType::New();</P>
<P> MovingImagePyramidType::Pointer movingImagePyramid =<BR> MovingImagePyramidType::New();</P>
<P><BR> typedef itk::ImageFileReader< FixedImageType > FixedImageReaderType;<BR> typedef itk::ImageFileReader< MovingImageType > MovingImageReaderType;</P>
<P> FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();<BR> MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();</P>
<P> fixedImageReader->SetFileName( argv[1] );<BR> movingImageReader->SetFileName( argv[2] );</P>
<P> int nx = 256, ny = 256, nz = 5;</P>
<P> typedef itk::RawImageIO<PixelType,Dimension> RawReaderType;<BR> RawReaderType::Pointer rawReader = RawReaderType::New();<BR> rawReader->SetDimensions( 0, nx );<BR> rawReader->SetDimensions( 1, ny );<BR> // rawReader->SetDimensions( 2, nz );</P>
<P><BR> fixedImageReader->SetImageIO( rawReader );</P>
<P> try<BR> {<BR> fixedImageReader->Update();<BR> }<BR> catch( itk::ExceptionObject & e )<BR> {<BR> std::cerr << "Exception caught during Raw file reading " << std::endl;<BR> std::cerr << e << std::endl;<BR> return -1;<BR> }</P>
<P><BR> movingImageReader->SetImageIO( rawReader );</P>
<P> try<BR> {<BR> movingImageReader->Update();<BR> }<BR> catch( itk::ExceptionObject & e )<BR> {<BR> std::cerr << "Exception caught during Raw file reading " << std::endl;<BR> std::cerr << e << std::endl;<BR> return -1;<BR> }</P>
<P><BR> std::cout << "\n First file : " << argv[1] << std::endl;<BR> std::cout << "Second file : " << argv[2] << std::endl;</P>
<P><BR> typedef itk::CastImageFilter< <BR> FixedImageType, InternalImageType > FixedCastFilterType;<BR> <BR> typedef itk::CastImageFilter< <BR> MovingImageType, InternalImageType > MovingCastFilterType;</P>
<P> FixedCastFilterType::Pointer fixedCaster = FixedCastFilterType::New();</P>
<P> MovingCastFilterType::Pointer movingCaster = MovingCastFilterType::New();</P>
<P> fixedCaster->SetInput( fixedImageReader->GetOutput() );</P>
<P> std::cout << "\n\n Set Fixed Caster";</P>
<P> movingCaster->SetInput( movingImageReader->GetOutput() );<BR> <BR> std::cout << "\n\n Set Moving Caster";</P>
<P> registration->SetFixedImage( fixedCaster->GetOutput() );<BR> std::cout << "\n\n SetFixedImage";</P>
<P> registration->SetMovingImage( movingCaster->GetOutput() );<BR> std::cout << "\n\n SetMovingImage";</P>
<P><BR> fixedCaster->Update();<BR> std::cout << "\n\n Update";</P>
<P> registration->SetFixedImageRegion(fixedCaster->GetOutput()->GetBufferedRegion() );</P>
<P> std::cout << "\n\n SetFixedImageRegion";<BR> <BR> transform->SetIdentity();</P>
<P> registration->SetInitialTransformParameters( transform->GetParameters() );<BR> OptimizerScalesType optimizerScales( transform->GetNumberOfParameters() );</P>
<P> std::cout << "\nGet No of Params : " << transform->GetNumberOfParameters() << std::endl;</P>
<P> optimizerScales[0] = 1.0;// / 100.0; // scale for M11<BR> optimizerScales[1] = 1.0;// / 100.0; // scale for M12<BR> optimizerScales[2] = 1.0;// / 100.0; // scale for M21<BR> optimizerScales[3] = 1.0;// / 100.0; // scale for M22<BR> typedef itk::Image< InternalPixelType, Dimension > InternalImageType;</P>
<P> optimizerScales[4] = 1.0 / 1000000.0; // scale for translation on Y<BR> optimizerScales[5] = 1.0 / 1000000.0; // scale for translation on Z<BR> optimizer->SetScales( optimizerScales );<BR> metric->SetNumberOfHistogramBins( 50 );<BR> metric->SetNumberOfSpatialSamples( 1000 );<BR> optimizer->SetNumberOfIterations( 200 );<BR> CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();<BR> optimizer->AddObserver( itk::IterationEvent(), observer );<BR> typedef RegistrationInterfaceCommand<RegistrationType> CommandType;</P>
<P> CommandType::Pointer command = CommandType::New();<BR> registration->AddObserver( itk::IterationEvent(), command );</P>
<P> </P>
<P> registration->SetNumberOfLevels( 3 );</P>
<P><BR> std::cout << "\n Before Registration ";</P>
<P> try <BR> { <BR> registration->StartRegistration(); <BR> } <BR> catch( itk::ExceptionObject & err ) <BR> { <BR> std::cout << "ExceptionObject caught !" << std::endl; <BR> std::cout << err << std::endl; <BR> return -1;<BR> } </P>
<P><BR> std::cout << "Hello";</P>
<P> typedef RegistrationType::ParametersType ParametersType;<BR> ParametersType finalParameters = registration->GetLastTransformParameters();<BR> <BR> double TranslationAlongX = finalParameters[4];<BR> double TranslationAlongY = finalParameters[5];<BR> <BR> unsigned int numberOfIterations = optimizer->GetCurrentIteration();<BR> <BR> double bestValue = optimizer->GetValue();</P>
<P> <BR> //<BR> // Print out results<BR> //<BR> std::cout << "Result = " << std::endl;<BR> std::cout << " Translation X = " << TranslationAlongX << std::endl;<BR> std::cout << " Translation Y = " << TranslationAlongY << std::endl;<BR> std::cout << " Iterations = " << numberOfIterations << std::endl;<BR> std::cout << " Metric value = " << bestValue << std::endl;<BR>typedef itk::ResampleImageFilter< <BR> MovingImageType, <BR> FixedImageType > ResampleFilterType;</P>
<P> TransformType::Pointer finalTransform = TransformType::New();</P>
<P> finalTransform->SetParameters( finalParameters );</P>
<P> finalTransform->PrintSelf(std::cout,NULL);</P>
<P> ResampleFilterType::Pointer resample = ResampleFilterType::New();</P>
<P> resample->SetTransform( finalTransform );<BR> resample->SetInput( movingImageReader->GetOutput() );</P>
<P> FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();</P>
<P> resample->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );<BR> resample->SetOutputOrigin( fixedImage->GetOrigin() );<BR> resample->SetOutputSpacing( fixedImage->GetSpacing() );<BR> resample->SetDefaultPixelValue( 100 );</P>
<P><BR> typedef short OutputPixelType;</P>
<P> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;<BR> <BR> typedef itk::CastImageFilter< <BR> FixedImageType,<BR> OutputImageType > CastFilterType;<BR> <BR> typedef itk::ImageFileWriter< OutputImageType > WriterType;</P>
<P><BR> WriterType::Pointer writer = WriterType::New();<BR> CastFilterType::Pointer caster = CastFilterType::New();</P>
<P><BR> writer->SetFileName( argv[3] );</P>
<P> std::cout << "\n OutPut : " << argv[3] << std::endl;<BR> </P>
<P> caster->SetInput( resample->GetOutput() );<BR> writer->SetInput( caster->GetOutput() );<BR> writer->SetImageIO(rawReader);</P>
<P> std::cout << "\n Writer : SetInput, SetImageIO";</P>
<P> try<BR> {<BR> writer->Write();<BR> std::cout << "\n Write()";<BR> }<BR> catch( itk::ExceptionObject & e )<BR> {<BR> std::cerr << "Exception caught during Raw file writing " << std::endl;<BR> std::cerr << e << std::endl;<BR> return -1;<BR> }</P>
<P> std::cout << "File succesfully writen ! " << std::endl;<BR></P><p><img src="http://sg.yimg.com/i/aa/icons/28/cricket.gif" height=28 width=28>
Catch all the cricket action. Download <a href="http://in.sports.yahoo.com/cricket/tracker.html" target="_blank">
Yahoo! Score tracker</a>
--0-755963147-1044106893=:68970--