<div>Hello,</div>
<div> </div>
<div>PROBLEM:</div>
<div> </div>
<div>I am trying to perform registration of two labelled volumes using ITK. </div>
<div> </div>
<div>STUDY:</div>
<div> </div>
<div>I went through the Image Registration Section of the ITK software guide and I came to know that the appropriate metric for my problem is the </div>
<div>itk::<font color="#010001" size="2"><font color="#010001" size="2">MatchCardinalityImageToImageMetric. </font></font><font color="#010001" size="2"><font color="#010001" size="2">I also went through the corresponding example in itk --- &quot;Examples\Registration\ImageRegistration10.cxx&quot;. </font></font></div>

<div><font color="#010001"></font> </div>
<div><font color="#010001">I found that the MatchCardinalityImageToImageMetric does not provide analytical derivatives and simply counts the number of matches/mismatches. Hence the regular optimizers wouldnt work, and hence i tried to use the AmoebaOptimizer as shown in the example.</font></div>

<div><font color="#010001"></font> </div>
<div><font color="#010001">However i want to perform registration in the following order:  <font color="#010001" size="2"><font color="#010001" size="2">Similarity Transform</font></font>, Affine, Deformable</font></div>

<div><font color="#010001"></font> </div>
<div><font color="#010001">To start with, i am just trying it with the Similarity3DTransform. </font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">itk::Similarity3DTransform derives from the itk::VersorRigid3DTransform and i read in the ITK software guide that the most appropriate optimizer for this transform is itk::VersorTransformOptimizer as it computes the versor derivatives as defined by hamilton. </font></div>

<div><font color="#010001"></font> </div>
<div><font color="#010001">QUESTION:</font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">Now Im confused as to which optimizer to use for my problem </font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">1. AmoebaOptimizer ( that is appropriate for MatchCardinalityImageToImageMetric)</font></div>
<div><font color="#010001">2. VersorTransformOptimizer (that is appropriate for Similarity3DTransform)</font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">Also, does the VersorTransformOptimizer require analytical derivates of the metric (which the MatchCardinalityImageToImageMetric does not provide).</font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">MY EXPERIMENTS:</font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">I tried to use the AmoebaOptimizer with the MatchCardinalityImageToImageMetric and Similarity3DTransform. </font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">I was not clear how to assign the Initial Simplex for my transform parameters particularly those of the quaternion. So i let the optimizer determine it automatically.</font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">However, the registration method fails with an exception. Below is the command line output:</font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">****************************************************************************************</font></div>
<div><font color="#010001"></font> </div>
<div>Iter 1 : 0.1122 --- [0, 0, 0, 0, 0, 0, 1] 
<div>Iter 2 : 0.117374 --- [0.00025, 0, 0, 0, 0, 0, 1]</div>
<div>Iter 3 : 0.114343 --- [0, 0.00025, 0, 0, 0, 0, 1]</div>
<div>Iter 4 : 0.114466 --- [0, 0, 0.00025, 0, 0, 0, 1]</div>
<div>Iter 5 : 0.113333 --- [0, 0, 0, 0.00025, 0, 0, 1]</div>
<div>Iter 6 : 0.113333 --- [0, 0, 0, 0, 0.00025, 0, 1]</div>
<div>Iter 7 : 0.1122 --- [0, 0, 0, 0, 0, 0.00025, 1]</div></div>
<div>ERROR-REGISTRATION : 
<div>itk::ExceptionObject (013FFA90)</div>
<div>Location: &quot;double __thiscall itk::MatchCardinalityImageToImageMetric&lt;class itk::Image&lt;unsigned char,3&gt;,class itk::Image&lt;unsigned char,3&gt; &gt;::GetNonconstValue(const class itk::Array&lt;double&gt; &amp;)&quot;</div>

<div>File: c:\itk\insighttoolkit-3.10.2\code\algorithms\itkMatchCardinalityImageToImageMetric.txx</div>
<div>Line: 130</div>
<div>Description: itk::ERROR: MatchCardinalityImageToImageMetric(018DCEE0): All the points mapped to outside of the moving image</div>
<div> </div></div>
<div>
<div>***************************************************************************************************</div></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">At the end of this message is a section of the code that i wrote ... </font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">Any help would be greatly appreciated.</font></div>
<div><font color="#010001"></font> </div>
<div><font color="#010001">Thanks in advance.</font></div>
<div><font color="#010001"></font> </div>
<div>Regards,</div>
<div> </div>
<div>Deepak</div>
<div> </div>
<div> </div>
<div>****************************** CODE ******************************</div>
<div> </div>
<div>// Perform registration with Similarity3DTransform first 
<div>typedef itk::ImageRegistrationMethod&lt;FixedImageType, MovingImageType&gt; RegistrationType;</div>
<div>RegistrationType::Pointer pRegistrationMethod = RegistrationType::New();     </div></div>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<div> // Metric</div>
<div> typedef itk::MatchCardinalityImageToImageMetric&lt;FixedImageType,MovingImageType&gt; MetricType;</div>
<div> MetricType::Pointer pMetric = MetricType::New();</div>
<div> pMetric-&gt;MeasureMatchesOff();</div>
<div> pRegistrationMethod-&gt;SetMetric( pMetric );</div>
<div> </div>
<div> // Interpolator 
<div> typedef itk::NearestNeighborInterpolateImageFunction&lt; MovingImageType, double&gt; InterpolatorType;</div>
<div> InterpolatorType::Pointer pInterpolator = InterpolatorType::New();</div></div>
<div> pRegistrationMethod-&gt;SetInterpolator( pInterpolator );</div>
<div> </div>
<div> // transform 
<div> typedef itk::Similarity3DTransform&lt;double&gt; SimilarityTransformType;</div>
<div> typedef itk::CenteredTransformInitializer&lt;SimilarityTransformType,FixedImageType,MovingImageType&gt; TransformInitializerType;</div></div>
<div> </div>
<div> SimilarityTransformType::Pointer pTransform = SimilarityTransformType::New();  
<div> TransformInitializerType::Pointer pTransformInitializer = TransformInitializerType::New();</div></div>
<div> </div>
<div> pTransformInitializer-&gt;SetFixedImage( pFixedImage ); 
<div> pTransformInitializer-&gt;SetMovingImage( pMovingImage );</div>
<div> pTransformInitializer-&gt;SetTransform( pTransform );</div>
<div> pTransformInitializer-&gt;MomentsOn();</div>
<div> pTransformInitializer-&gt;InitializeTransform(); </div></div>
<div> </div>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<div>  // rotation</div>
<div>  typedef SimilarityTransformType::VersorType VersorType;</div>
<div>  typedef VersorType::VectorType VectorType;</div>
<div>  VectorType rot_axis;</div>
<div>  rot_axis[0] = 0.0; 
<div>  rot_axis[1] = 0.0;</div>
<div>  rot_axis[2] = 1.0;</div></div>
<div>  pTransform-&gt;SetRotation( rot_axis , 0.0 );</div>
<div> </div>
<div> // scaling 
<div> pTransform-&gt;SetScale( 1.0 );</div></div>
<div> pRegistrationMethod-&gt;SetInitialTransformParameters( pTransform-&gt;GetParameters() ); 
<div> pRegistrationMethod-&gt;SetTransform( pTransform );  </div></div></blockquote>
<div> // Optimizer 
<div> typedef itk::AmoebaOptimizer OptimizerType;</div>
<div> OptimizerType::Pointer pOptimizer = OptimizerType::New();</div>
<div> OptimizerType::ParametersType simplexDelta( pTransform-&gt;GetNumberOfParameters() );</div></div>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<div>//   simplexDelta.Fill( 5.0 );</div>
<div>//   pOptimizer-&gt;AutomaticInitialSimplexOff();</div>
<div>//   pOptimizer-&gt;SetInitialSimplexDelta( simplexDelta );</div></blockquote>
<div> pOptimizer-&gt;SetParametersConvergenceTolerance( 0.25 ); // quarter pixel 
<div> pOptimizer-&gt;SetFunctionConvergenceTolerance( 0.001 ); // 0.1%</div>
<div> pOptimizer-&gt;SetMaximumNumberOfIterations( 10 );</div></div>
<div> pRegistrationMethod-&gt;SetOptimizer( pOptimizer );</div>
<div> </div>
<div> // Observer 
<div> typedef CommandIterationUpdate ObserverType;</div>
<div> ObserverType::Pointer pObserver = ObserverType::New();</div></div>
<div> pOptimizer-&gt;AddObserver( itk::IterationEvent() , pObserver);</div></blockquote>
<div> </div>
<div>pRegistrationMethod-&gt;SetFixedImage( pFixedImage ); 
<div>pRegistrationMethod-&gt;SetMovingImage( pMovingImage );</div>
<div>pRegistrationMethod-&gt;SetFixedImageRegion( pFixedImage-&gt;GetLargestPossibleRegion() );</div>
<div> </div></div>
<div>try 
<div>{</div>
<div> pRegistrationMethod-&gt;Initialize();</div>
<div> pRegistrationMethod-&gt;StartRegistration();</div>
<div>}</div>
<div>catch(itk::ExceptionObject &amp; err)</div>
<div>{</div>
<div> std::cout &lt;&lt; &quot;\nERROR-REGISTRATION : &quot; &lt;&lt; err &lt;&lt; std::endl;</div></div>
<div> return EXIT_FAILURE; 
<div>}</div>
<div> </div></div>
<div>RegistrationType::ParametersType finalParameters;</div>
<div>finalParameters = pRegistrationMethod-&gt;GetLastTransformParameters();</div>
<div> </div>
<div>std::cout &lt;&lt; &quot;Final: &quot; &lt;&lt; pMetric-&gt;GetValue( finalParameters ) &lt;&lt; &quot; --- &quot; &lt;&lt; finalParameters &lt;&lt; std::endl;</div>
<div> </div>
<div>// Resampling -- Transform the moving image using the obtained transform parameters 
<div>typedef itk::ResampleImageFilter&lt;MovingImageType,FixedImageType&gt; ResampleFilterType;</div>
<div>ResampleFilterType::Pointer pResampleImageFilter = ResampleFilterType::New();</div></div>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<div> // transform</div>
<div> SimilarityTransformType::Pointer pFinalTransform = SimilarityTransformType::New();</div>
<div> pFinalTransform-&gt;SetParameters( pRegistrationMethod-&gt;GetLastTransformParameters() );</div></blockquote>
<div>pResampleImageFilter-&gt;SetTransform( pFinalTransform );</div>
<div>pResampleImageFilter-&gt;SetInput( pMovingImage ); 
<div>pResampleImageFilter-&gt;SetSize( pFixedImage-&gt;GetLargestPossibleRegion().GetSize() );</div>
<div>pResampleImageFilter-&gt;SetOutputOrigin(  pFixedImage-&gt;GetOrigin() );</div>
<div>pResampleImageFilter-&gt;SetOutputSpacing( pFixedImage-&gt;GetSpacing() );</div>
<div>pResampleImageFilter-&gt;SetOutputDirection( pFixedImage-&gt;GetDirection() );</div>
<div>pResampleImageFilter-&gt;SetDefaultPixelValue( 0 );</div>
<div>pResampleImageFilter-&gt;SetInterpolator( pInterpolator );</div></div>
<div>pResampleImageFilter-&gt;Update();</div>
<div> </div>
<div>************************************************************************************************</div>