<DIV>Hi all,</DIV>
<DIV>I try to combine registration example: ImageRegistration2 and 9 to perform an CenteredAffine+MutualInfo registration.</DIV>
<DIV>The code bellow compile well, but when I try with: BrainT1SliceBorder20.png and BrainProtonDensitySliceR10X13Y17S12.png I have the following error:</DIV>
<DIV> </DIV>
<DIV>---------------</DIV>
<DIV>Initial Transform Parameters<BR>[1, 0, 0, 1, -1.00276e+008, -4.25387e+008, 4.90708e+007, 3.85172e+008]<BR>ExceptionObject caught !</DIV>
<DIV>itk::ExceptionObject (00FCFD30)<BR>Location: "Unknown"<BR>File: D:\InsightToolkit-1.8.1\Code\Algorithms\itkMutualInformationImageToImageMetric.txx<BR>Line: 155<BR>Description: itk::ERROR: MutualInformationImageToImageMetric(012F4780): All the sampled point mapped to outside of the moving image</DIV>
<DIV>-----------------</DIV>
<DIV> </DIV>
<DIV>->The Typedef is set to float since Images are unsigned chart is it the problem ? </DIV>
<DIV>If I change the initialisation of the CenteredTransformInitializer as:</DIV>
<DIV> initializer->SetFixedImage( fixedImageReader->GetOutput() );<BR> initializer->SetMovingImage( movingImageReader->GetOutput() );</DIV>
<DIV>There is no error, but the after 9 iterations it stop with a wrong image results....</DIV>
<DIV> </DIV>
<DIV>Is it just a problem of initial typeddef for my fixed and moved image ?</DIV>
<DIV> </DIV>
<DIV>thanks,</DIV>
<DIV> </DIV>
<DIV>lagaffe</DIV>
<DIV> </DIV>
<DIV>---------------------------------------------------------------------------------------------------------------------</DIV>
<DIV>/*<BR>* Code from ImageRegistration2 and ImageRegistration9 <BR>*/</DIV>
<DIV>#include "itkImageRegistrationMethod.h"<BR>//#include "itkMeanSquaresImageToImageMetric.h"<BR>#include "itkMutualInformationImageToImageMetric.h"<BR>#include "itkNormalizeImageFilter.h"<BR>#include "itkDiscreteGaussianImageFilter.h"</DIV>
<DIV>#include "itkLinearInterpolateImageFunction.h"<BR>#include "itkRegularStepGradientDescentOptimizer.h"<BR>#include "itkImage.h"</DIV>
<DIV><BR>#include "itkCenteredTransformInitializer.h"<BR>#include "itkCenteredAffineTransform.h"<BR>#include "itkImageFileReader.h"<BR>#include "itkImageFileWriter.h"</DIV>
<DIV>#include "itkResampleImageFilter.h"<BR>#include "itkCastImageFilter.h"<BR>#include "itkSquaredDifferenceImageFilter.h"<BR>#include "itkCommand.h"<BR>class CommandIterationUpdate : public itk::Command <BR>{<BR>public:<BR> typedef CommandIterationUpdate Self;<BR> typedef itk::Command Superclass;<BR> typedef itk::SmartPointer<Self> Pointer;<BR> itkNewMacro( Self );<BR>protected:<BR> CommandIterationUpdate() {};<BR>public:<BR> typedef itk::RegularStepGradientDescentOptimizer OptimizerType;<BR> typedef const OptimizerType * OptimizerPointer;</DIV>
<DIV> void Execute(itk::Object *caller, const itk::EventObject & event)<BR> {<BR> Execute( (const itk::Object *)caller, event);<BR> }</DIV>
<DIV> void Execute(const itk::Object * object, const itk::EventObject & event)<BR> {<BR> OptimizerPointer optimizer = <BR> dynamic_cast< OptimizerPointer >( object );<BR> if( typeid( event ) != typeid( itk::IterationEvent ) )<BR> {<BR> return;<BR> }<BR> std::cout << optimizer->GetCurrentIteration() << " ";<BR> std::cout << optimizer->GetValue() << " ";<BR> std::cout << optimizer->GetCurrentPosition() << std::endl;<BR> }<BR>};</DIV>
<DIV><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 [differenceOutputfile] [differenceBeforeRegistration] " << std::endl;<BR> std::cerr << " [stepLength] [maxNumberOfIterations] "<< std::endl;<BR> return 1;<BR> }</DIV>
<DIV> const unsigned int Dimension = 2;<BR> typedef float PixelType;</DIV>
<DIV> typedef float InternalPixelType;<BR> typedef itk::Image< InternalPixelType, Dimension > InternalImageType;</DIV>
<DIV> typedef itk::Image< PixelType, Dimension > FixedImageType;<BR> typedef itk::Image< PixelType, Dimension > MovingImageType;<BR> <BR> typedef itk::CenteredAffineTransform< <BR> double, <BR> Dimension > TransformType;<BR> typedef itk::RegularStepGradientDescentOptimizer OptimizerType;<BR> /*<BR> typedef itk::MeanSquaresImageToImageMetric<
<BR> FixedImageType, <BR> MovingImageType > MetricType;<BR> */<BR> typedef itk::MutualInformationImageToImageMetric< <BR> InternalImageType,
<BR> InternalImageType > MetricType;<BR> typedef itk:: LinearInterpolateImageFunction< <BR> MovingImageType,<BR> double > InterpolatorType;<BR> typedef itk::ImageRegistrationMethod<
<BR> FixedImageType, <BR> MovingImageType > RegistrationType;</DIV>
<DIV> MetricType::Pointer metric = MetricType::New();<BR> OptimizerType::Pointer optimizer = OptimizerType::New();<BR> InterpolatorType::Pointer interpolator = InterpolatorType::New();<BR> RegistrationType::Pointer registration = RegistrationType::New();<BR> TransformType::Pointer transform = TransformType::New();</DIV>
<DIV><BR> registration->SetMetric( metric );<BR> registration->SetOptimizer( optimizer );<BR> registration->SetInterpolator( interpolator );<BR> registration->SetTransform( transform );</DIV>
<DIV> metric->SetFixedImageStandardDeviation( 0.4 );<BR> metric->SetMovingImageStandardDeviation( 0.4 );<BR> metric->SetNumberOfSpatialSamples( 50 );</DIV>
<DIV> 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] );</DIV>
<DIV>typedef itk::NormalizeImageFilter< <BR> FixedImageType, <BR> InternalImageType <BR> > FixedNormalizeFilterType;</DIV>
<DIV> typedef itk::NormalizeImageFilter< <BR> MovingImageType, <BR> InternalImageType <BR> > MovingNormalizeFilterType;</DIV>
<DIV> FixedNormalizeFilterType::Pointer fixedNormalizer = <BR> FixedNormalizeFilterType::New();</DIV>
<DIV> MovingNormalizeFilterType::Pointer movingNormalizer =<BR> MovingNormalizeFilterType::New();<BR> <BR> typedef itk::DiscreteGaussianImageFilter<<BR> InternalImageType, <BR>
InternalImageType<BR> > GaussianFilterType;<BR> <BR> GaussianFilterType::Pointer fixedSmoother = GaussianFilterType::New();<BR> GaussianFilterType::Pointer movingSmoother = GaussianFilterType::New();</DIV>
<DIV> fixedSmoother->SetVariance( 2.0 );<BR> movingSmoother->SetVariance( 2.0 );<BR> <BR> fixedNormalizer->SetInput( fixedImageReader->GetOutput() );<BR> movingNormalizer->SetInput( movingImageReader->GetOutput() );</DIV>
<DIV> fixedSmoother->SetInput( fixedNormalizer->GetOutput() );<BR> movingSmoother->SetInput( movingNormalizer->GetOutput() );</DIV>
<DIV> registration->SetFixedImage( fixedSmoother->GetOutput() );<BR> registration->SetMovingImage( movingSmoother->GetOutput() );<BR> <BR> fixedNormalizer->Update();<BR> registration->SetFixedImageRegion( <BR> fixedNormalizer->GetOutput()->GetBufferedRegion() );</DIV>
<DIV><BR> typedef itk::CenteredTransformInitializer< <BR> TransformType, <BR> FixedImageType, <BR> MovingImageType > TransformInitializerType;<BR> TransformInitializerType::Pointer initializer = TransformInitializerType::New();<BR> initializer->SetTransform( transform );<BR> /*<BR>
initializer->SetFixedImage( fixedImageReader->GetOutput() );<BR> initializer->SetMovingImage( movingImageReader->GetOutput() );<BR>*/<BR> <BR> initializer->SetFixedImage( fixedSmoother->GetOutput() );<BR> initializer->SetMovingImage( movingSmoother->GetOutput() );</DIV>
<DIV> initializer->MomentsOn();<BR> initializer->InitializeTransform();<BR> std::cout << "Initial Transform Parameters " << std::endl;<BR> std::cout << transform->GetParameters() << std::endl;<BR> <BR>// -----------------------<BR> registration->SetInitialTransformParameters( <BR> transform->GetParameters() );</DIV>
<DIV><BR> double translationScale = 1.0 / 1000.0;<BR> if( argc > 8 )<BR> {<BR> translationScale = atof( argv[8] );<BR> }<BR> typedef OptimizerType::ScalesType OptimizerScalesType;<BR> OptimizerScalesType optimizerScales( transform->GetNumberOfParameters() );</DIV>
<DIV> optimizerScales[0] = 1.0;<BR> optimizerScales[1] = 1.0;<BR> optimizerScales[2] = 1.0;<BR> optimizerScales[3] = 1.0;<BR> optimizerScales[4] = translationScale;<BR> optimizerScales[5] = translationScale;<BR> optimizerScales[6] = translationScale;<BR> optimizerScales[7] = translationScale;</DIV>
<DIV> optimizer->SetScales( optimizerScales );<BR> double steplength = 0.1;</DIV>
<DIV> if( argc > 6 )<BR> {<BR> steplength = atof( argv[6] );<BR> }</DIV>
<DIV><BR> unsigned int maxNumberOfIterations = 300;</DIV>
<DIV> if( argc > 7 )<BR> {<BR> maxNumberOfIterations = atoi( argv[7] );<BR> }</DIV>
<DIV> optimizer->SetMaximumStepLength( steplength ); <BR> optimizer->SetMinimumStepLength( 0.001 );<BR> optimizer->SetNumberOfIterations( maxNumberOfIterations );<BR> optimizer->MinimizeOn();<BR> CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();<BR> optimizer->AddObserver( itk::IterationEvent(), observer );<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> OptimizerType::ParametersType finalParameters = <BR>
registration->GetLastTransformParameters();</DIV>
<DIV> const double finalRotationCenterX = finalParameters[4];<BR> const double finalRotationCenterY = finalParameters[5];<BR> const double finalTranslationX = finalParameters[6];<BR> const double finalTranslationY = finalParameters[7];</DIV>
<DIV> const unsigned int numberOfIterations = optimizer->GetCurrentIteration();<BR> const double bestValue = optimizer->GetValue();<BR> // Software Guide : EndCodeSnippet</DIV>
<DIV><BR> // Print out results<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> typedef itk::ResampleImageFilter<
<BR> MovingImageType, <BR> FixedImageType > ResampleFilterType;</DIV>
<DIV> TransformType::Pointer finalTransform = TransformType::New();</DIV>
<DIV> finalTransform->SetParameters( finalParameters );</DIV>
<DIV> ResampleFilterType::Pointer resample = ResampleFilterType::New();</DIV>
<DIV> resample->SetTransform( finalTransform );<BR> resample->SetInput( movingImageReader->GetOutput() );</DIV>
<DIV> FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();</DIV>
<DIV> resample->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );<BR> resample->SetOutputOrigin( fixedImage->GetOrigin() );<BR> resample->SetOutputSpacing( fixedImage->GetSpacing() );<BR> resample->SetDefaultPixelValue( 100 );<BR> <BR> typedef unsigned char OutputPixelType;</DIV>
<DIV> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;<BR> <BR> typedef itk::CastImageFilter< <BR> FixedImageType,<BR> OutputImageType > CastFilterType;<BR> <BR> typedef itk::ImageFileWriter< OutputImageType > WriterType;</DIV>
<DIV><BR> WriterType::Pointer writer = WriterType::New();<BR> CastFilterType::Pointer caster = CastFilterType::New();</DIV>
<DIV><BR> writer->SetFileName( argv[3] );</DIV>
<DIV><BR> caster->SetInput( resample->GetOutput() );<BR> writer->SetInput( caster->GetOutput() );<BR> writer->Update();</DIV>
<DIV><BR> typedef itk::SquaredDifferenceImageFilter< <BR> FixedImageType, <BR> FixedImageType, <BR> OutputImageType > DifferenceFilterType;</DIV>
<DIV> DifferenceFilterType::Pointer difference = DifferenceFilterType::New();</DIV>
<DIV> WriterType::Pointer writer2 = WriterType::New();<BR> writer2->SetInput( difference->GetOutput() ); <BR> </DIV>
<DIV> if( argc > 4 )<BR> {<BR> difference->SetInput1( fixedImageReader->GetOutput() );<BR> difference->SetInput2( resample->GetOutput() );<BR> writer2->SetFileName( argv[4] );<BR> writer2->Update();<BR> }</DIV>
<DIV><BR> if( argc > 5 )<BR> {<BR> writer2->SetFileName( argv[5] );<BR> difference->SetInput1( fixedImageReader->GetOutput() );<BR> difference->SetInput2( movingImageReader->GetOutput() );<BR> writer2->Update();<BR> }</DIV>
<DIV><BR> return 0;<BR>}</DIV>
<DIV> </DIV><p>
                <hr size=1>
Créez gratuitement votre Yahoo! Mail avec <font color="red"><b>100 Mo de stockage !</b></font>
<br><a href="http://fr.rd.yahoo.com/mail/taglines/*http://fr.rd.yahoo.com/evt=25917/*http://fr.rd.yahoo.com/mail/mail_taglines_100/default/*http://fr.benefits.yahoo.com/">Créez votre Yahoo! Mail</a><br><br>
<font color="red"><b>Le nouveau Yahoo! Messenger est arrivé !</b></font> Découvrez toutes les nouveautés pour dialoguer instantanément avec vos amis.
<a href="http://fr.rd.yahoo.com/mail/taglines/*http://fr.rd.yahoo.com/evt=26111/*http://fr.rd.yahoo.com/messenger/mail_taglines/default/*http://fr.messenger.yahoo.com">Téléchargez GRATUITEMENT ici !</a>