<div dir="ltr"><p class=""><span lang="EN-US">Hello everyone,</span></p>
<p class="" align="left"><span lang="EN-US">I do a registration example in ITK,it
is “ImageRegistration1.cxx”.But I want to use “itkParticleSwarmOptimizer.h”
instead of "itkRegularStepGradientDescentOptimizer.h".I have modified
this programming,but it has been an error.</span></p>
<p class="" align="left"><span lang="EN-US"> </span></p>
<p class="" align="left"><span lang="EN-US">The code is as follows:</span><span lang="EN-US"></span></p><p class="" align="left"><span lang="EN-US"><br></span></p><p class="" align="left"><br></p><p class="" align="left">
#include "itkImageRegistrationMethod.h"</p><p class="" align="left">#include "itkTranslationTransform.h"</p><p class="" align="left">#include "itkMeanSquaresImageToImageMetric.h"</p><p class="" align="left">
#include "itkParticleSwarmOptimizer.h"</p><p class="" align="left">// Software Guide : EndCodeSnippet</p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left">#include "itkImageFileReader.h"</p>
<p class="" align="left">#include "itkImageFileWriter.h"</p><p class="" align="left"><br></p><p class="" align="left">#include "itkResampleImageFilter.h"</p><p class="" align="left">#include "itkCastImageFilter.h"</p>
<p class="" align="left">#include "itkRescaleIntensityImageFilter.h"</p><p class="" align="left">#include "itkSubtractImageFilter.h"</p><p class="" align="left"><br></p><p class="" align="left"><br></p>
<p class="" align="left"><br></p><p class="" align="left">class CommandIterationUpdate : public itk::Command</p><p class="" align="left">{</p><p class="" align="left">public:</p><p class="" align="left"> typedef CommandIterationUpdate Self;</p>
<p class="" align="left"> typedef itk::Command Superclass;</p><p class="" align="left"> typedef itk::SmartPointer<Self> Pointer;</p><p class="" align="left"> itkNewMacro( Self );</p><p class="" align="left">
<br></p><p class="" align="left">protected:</p><p class="" align="left"> CommandIterationUpdate() {};</p><p class="" align="left"><br></p><p class="" align="left">public:</p><p class="" align="left"><br></p><p class="" align="left">
typedef itk::ParticleSwarmOptimizer OptimizerType;</p><p class="" align="left"> typedef const OptimizerType *OptimizerPointer;</p><p class="" align="left"><br></p><p class="" align="left"> void Execute(itk::Object *caller, const itk::EventObject & event)</p>
<p class="" align="left"> {</p><p class="" align="left"> Execute( (const itk::Object *)caller, event);</p><p class="" align="left"> }</p><p class="" align="left"><br></p><p class="" align="left"> void Execute(const itk::Object * object, const itk::EventObject & event)</p>
<p class="" align="left"> {</p><p class="" align="left"> OptimizerPointer optimizer =</p><p class="" align="left"> dynamic_cast< OptimizerPointer >( object );</p><p class="" align="left">
<br></p><p class="" align="left"> if( ! itk::IterationEvent().CheckEvent( &event ) )</p><p class="" align="left"> {</p><p class="" align="left"> return;</p><p class="" align="left"> }</p><p class="" align="left">
<br></p><p class="" align="left"> //std::cout << optimizer->GetMaximalNumberOfIterations () << " = ";</p><p class="" align="left"> std::cout << optimizer->GetValue() << " : ";</p>
<p class="" align="left"> std::cout << optimizer->GetCurrentPosition() << std::endl;</p><p class="" align="left"> }</p><p class="" align="left"><br></p><p class="" align="left">};</p><p class="" align="left">
<br></p><p class="" align="left"><br></p><p class="" align="left">int main( int argc, char *argv[] )</p><p class="" align="left">{</p><p class="" align="left"> </p><p class="" align="left"> const unsigned int Dimension = 2;</p>
<p class="" align="left"> typedef float PixelType;</p><p class="" align="left"> </p><p class="" align="left"> typedef itk::Image< PixelType, Dimension > FixedImageType;</p><p class="" align="left"> typedef itk::Image< PixelType, Dimension > MovingImageType;</p>
<p class="" align="left"> </p><p class="" align="left"> typedef itk::TranslationTransform< double, Dimension > TransformType;</p><p class="" align="left"> </p><p class="" align="left"> typedef itk::ParticleSwarmOptimizer OptimizerType;</p>
<p class="" align="left"> static OptimizerType::RandomVariateGeneratorType::IntegerType seedOffset = 0;</p><p class="" align="left"><br></p><p class="" align="left"> typedef itk::MeanSquaresImageToImageMetric<</p><p class="" align="left">
FixedImageType,</p><p class="" align="left"> MovingImageType > MetricType;</p><p class="" align="left"> </p><p class="" align="left"> typedef itk:: LinearInterpolateImageFunction<</p>
<p class="" align="left"> MovingImageType,</p><p class="" align="left"> double > InterpolatorType;</p><p class="" align="left"> </p><p class="" align="left">
typedef itk::ImageRegistrationMethod<</p><p class="" align="left"> FixedImageType,</p><p class="" align="left"> MovingImageType > RegistrationType;</p>
<p class="" align="left"> </p><p class="" align="left"> MetricType::Pointer metric = MetricType::New();</p><p class="" align="left"> TransformType::Pointer transform = TransformType::New();</p><p class="" align="left">
OptimizerType::Pointer optimizer = OptimizerType::New();</p><p class="" align="left"> InterpolatorType::Pointer interpolator = InterpolatorType::New();</p><p class="" align="left"> RegistrationType::Pointer registration = RegistrationType::New();</p>
<p class="" align="left"> </p><p class="" align="left"> registration->SetMetric( metric );</p><p class="" align="left"> registration->SetOptimizer( optimizer );</p><p class="" align="left"> registration->SetTransform( transform );</p>
<p class="" align="left"> registration->SetInterpolator( interpolator );</p><p class="" align="left"> // Software Guide : EndCodeSnippet</p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left">
typedef itk::ImageFileReader< FixedImageType > FixedImageReaderType;</p><p class="" align="left"> typedef itk::ImageFileReader< MovingImageType > MovingImageReaderType;</p><p class="" align="left"> FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();</p>
<p class="" align="left"> MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();</p><p class="" align="left"><br></p><p class="" align="left"> fixedImageReader->SetFileName( "BrainProtonDensitySliceBorder20.png" );</p>
<p class="" align="left"> movingImageReader->SetFileName( "BrainProtonDensitySliceShifted13x17y.png" );</p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"> </p><p class="" align="left">
registration->SetFixedImage( fixedImageReader->GetOutput() );</p><p class="" align="left"> registration->SetMovingImage( movingImageReader->GetOutput() );</p><p class="" align="left"> </p><p class="" align="left">
fixedImageReader->Update();</p><p class="" align="left"> registration->SetFixedImageRegion(</p><p class="" align="left"> fixedImageReader->GetOutput()->GetBufferedRegion() );</p><p class="" align="left">
</p><p class="" align="left"> typedef RegistrationType::ParametersType ParametersType;</p><p class="" align="left"> ParametersType initialParameters( transform->GetNumberOfParameters() );</p><p class="" align="left">
<br></p><p class="" align="left"> initialParameters[0] = 0.0; // Initial offset in mm along X</p><p class="" align="left"> initialParameters[1] = 0.0; // Initial offset in mm along Y</p><p class="" align="left"><br></p>
<p class="" align="left"> registration->SetInitialTransformParameters( initialParameters );</p><p class="" align="left"> </p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left">
optimizer->UseSeedOn();</p><p class="" align="left"> optimizer->SetSeed(8775070 + seedOffset++);</p><p class="" align="left"><br></p><p class="" align="left"> OptimizerType::ParameterBoundsType bounds;</p><p class="" align="left">
bounds.push_back( std::make_pair( 0, 1 ) );</p><p class="" align="left"> optimizer->SetParameterBounds( bounds );</p><p class="" align="left"> optimizer->SetNumberOfParticles( 10 );//numberOfParticles</p><p class="" align="left">
optimizer->SetMaximalNumberOfIterations( 100 );//maxIterations</p><p class="" align="left"> optimizer->SetParametersConvergenceTolerance( 0.1,transform->GetNumberOfParameters() );//1ˇ˘xTolerance</p><p class="" align="left">
optimizer->SetFunctionConvergenceTolerance( 0.001 );//fTolerance</p><p class="" align="left"><br></p><p class="" align="left"> </p><p class="" align="left"> CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();</p>
<p class="" align="left"> optimizer->AddObserver( itk::IterationEvent(), observer );</p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"> </p><p class="" align="left"> try</p>
<p class="" align="left"> {</p><p class="" align="left"> registration->Update();</p><p class="" align="left"><span class="" style="white-space:pre">        </span>std::cout << "Optimizer stop condition: "</p>
<p class="" align="left"> << registration->GetOptimizer()->GetStopConditionDescription()</p><p class="" align="left"> << std::endl;</p><p class="" align="left"> }</p><p class="" align="left">
catch( itk::ExceptionObject & err )</p><p class="" align="left"> {</p><p class="" align="left"> std::cerr << "ExceptionObject caught !" << std::endl;</p><p class="" align="left"> std::cerr << err << std::endl;</p>
<p class="" align="left"> return EXIT_FAILURE;</p><p class="" align="left"> }</p><p class="" align="left"> </p><p class="" align="left"> ParametersType finalParameters = registration->GetLastTransformParameters();</p>
<p class="" align="left"> </p><p class="" align="left"> const double TranslationAlongX = finalParameters[0];</p><p class="" align="left"> const double TranslationAlongY = finalParameters[1];</p><p class="" align="left">
</p><p class="" align="left"> </p><p class="" align="left"> const double bestValue = optimizer->GetValue();</p><p class="" align="left"> </p><p class="" align="left"> std::cout << "Result = " << std::endl;</p>
<p class="" align="left"> std::cout << " Translation X = " << TranslationAlongX << std::endl;</p><p class="" align="left"> std::cout << " Translation Y = " << TranslationAlongY << std::endl;</p>
<p class="" align="left"> //std::cout << " Iterations = " << numberOfIterations << std::endl;</p><p class="" align="left"> std::cout << " Metric value = " << bestValue << std::endl;</p>
<p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"> </p><p class="" align="left"> typedef itk::ResampleImageFilter<</p><p class="" align="left"> MovingImageType,</p>
<p class="" align="left"> FixedImageType > ResampleFilterType;</p><p class="" align="left"> </p><p class="" align="left"> ResampleFilterType::Pointer resampler = ResampleFilterType::New();</p>
<p class="" align="left"> resampler->SetInput( movingImageReader->GetOutput() );</p><p class="" align="left"> </p><p class="" align="left"> resampler->SetTransform( registration->GetOutput()->Get() );</p>
<p class="" align="left"> </p><p class="" align="left"> FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();</p><p class="" align="left"> resampler->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );</p>
<p class="" align="left"> resampler->SetOutputOrigin( fixedImage->GetOrigin() );</p><p class="" align="left"> resampler->SetOutputSpacing( fixedImage->GetSpacing() );</p><p class="" align="left"> resampler->SetOutputDirection( fixedImage->GetDirection() );</p>
<p class="" align="left"> resampler->SetDefaultPixelValue( 100 );</p><p class="" align="left"> </p><p class="" align="left"> typedef unsigned char OutputPixelType;</p><p class="" align="left"> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;</p>
<p class="" align="left"> typedef itk::CastImageFilter<</p><p class="" align="left"> FixedImageType,</p><p class="" align="left"> OutputImageType > CastFilterType;</p><p class="" align="left">
typedef itk::ImageFileWriter< OutputImageType > WriterType;</p><p class="" align="left"> </p><p class="" align="left"> WriterType::Pointer writer = WriterType::New();</p><p class="" align="left"> CastFilterType::Pointer caster = CastFilterType::New();</p>
<p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"> writer->SetFileName( "output.png" );</p><p class="" align="left"><br></p><p class="" align="left">
<br></p><p class="" align="left"> </p><p class="" align="left"> caster->SetInput( resampler->GetOutput() );</p><p class="" align="left"> writer->SetInput( caster->GetOutput() );</p><p class="" align="left">
writer->Update();</p><p class="" align="left"> </p><p class="" align="left"> typedef itk::SubtractImageFilter<</p><p class="" align="left"> FixedImageType,</p><p class="" align="left">
FixedImageType,</p><p class="" align="left"> FixedImageType > DifferenceFilterType;</p><p class="" align="left"><br></p><p class="" align="left"> DifferenceFilterType::Pointer difference = DifferenceFilterType::New();</p>
<p class="" align="left"><br></p><p class="" align="left"> difference->SetInput1( fixedImageReader->GetOutput() );</p><p class="" align="left"> difference->SetInput2( resampler->GetOutput() );</p><p class="" align="left">
</p><p class="" align="left"> typedef itk::RescaleIntensityImageFilter<</p><p class="" align="left"> FixedImageType,</p><p class="" align="left"> OutputImageType > RescalerType;</p>
<p class="" align="left"><br></p><p class="" align="left"> RescalerType::Pointer intensityRescaler = RescalerType::New();</p><p class="" align="left"><br></p><p class="" align="left"> intensityRescaler->SetInput( difference->GetOutput() );</p>
<p class="" align="left"> intensityRescaler->SetOutputMinimum( 0 );</p><p class="" align="left"> intensityRescaler->SetOutputMaximum( 255 );</p><p class="" align="left"><br></p><p class="" align="left"> resampler->SetDefaultPixelValue( 1 );</p>
<p class="" align="left"> </p><p class="" align="left"> WriterType::Pointer writer2 = WriterType::New();</p><p class="" align="left"> writer2->SetInput( intensityRescaler->GetOutput() );</p><p class="" align="left">
// Software Guide : EndCodeSnippet</p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"> /*if( argc > 4 )</p><p class="" align="left"> {*/</p><p class="" align="left"> writer2->SetFileName( "differenceAfter.png" );</p>
<p class="" align="left"> writer2->Update();</p><p class="" align="left"> /*}*/</p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"> </p>
<p class="" align="left"> TransformType::Pointer identityTransform = TransformType::New();</p><p class="" align="left"> identityTransform->SetIdentity();</p><p class="" align="left"> resampler->SetTransform( identityTransform );</p>
<p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"> /*if( argc > 5 )</p><p class="" align="left"> {*/</p><p class="" align="left"> writer2->SetFileName( "differenceBefor.png" );</p>
<p class="" align="left"> writer2->Update();</p><p class="" align="left"> /*}*/</p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"> </p>
<p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left"> return EXIT_SUCCESS;</p><p class="" align="left">}</p><p class="" align="left"><span lang="EN-US"></span></p><p class="" align="left">
<br></p><p class="" align="left"><br></p><p class="" align="left">But an error been caught ,when you run it.The error information is as follow:</p><p class="" align="left">Location:“void _thiscall itk::ParticleSwarmOptimizerBase::validateSettings<vod>”</p>
<p class="" align="left">File:D:\..\itkParticleSwarmOptimizerBase.cxx</p><p class="" align="left">Line:360</p><p class="" align="left">Description:itk::ERROR:ParticleSwarmOptimizer<02A022C8>:cost function and parameter bounds dimensions mismatch</p>
<p class="" align="left"><br></p><p class="" align="left"><br></p><p class="" align="left">Please give some help............................</p></div>