<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Hi,</div><div><br></div><div>I'm trying to use a simple metric like MeanSquaresImageToImageMetric with a BsplineInterpolatorImageFunction and RegularStepGradientDescendetOptimizer to solve a particular registration problem but the optimizer can achieve the convergence (any convergence) and the result destroy the image. I used ITK-4.2.0 and ITK-4.1 with the same problem.</div><div><br></div><div><br></div><div>In my code and in the past I've used the optimizer of LBFGSBoptimizer but I've never got a stable convergence and change this for the RegularStepGradient (any help with LBFGSoptimizer is accepted too).</div><div><br></div><div>If anyone have an example how to use MeansSquares metric, Bspline interpolation and RegularStepGradient &nbsp;with the successfully result please send me the code.</div><div><br></div><div><br></div><div>I use the code with following parameters and two image of &nbsp;200x200:</div><div><br></div><div><div>--numberTrheads 1</div><div>--numberLevels 5</div><div>--space 16</div><div>--metricType SSD</div><div>--optimizerType RSGD</div><div>--interpolatorType BSI</div><div>--maxIt 500</div><div>--relaxFact 0.8</div><div>--steplength 0.001 2</div><div><br></div></div><div><br></div><div><br></div><div>An extract of my code consern to the RESGOptimizer, SSDMetric and BsplineInterpolator &nbsp;is:</div><div><br></div><div><br></div><div><div>typedef itk::SingleValuedNonLinearOptimizer<span class="Apple-tab-span" style="white-space:pre">                </span>OptimizerType;//Abstract Optimizer</div><div>typedef itk::RegularStepGradientDescentOptimizer &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>RSGDOptimizerType;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><br></div><div>typedef itk::ImageToImageMetric&lt;FixedImageType,MovingImageType &gt; &nbsp; &nbsp;MetricType;//Abstract Metric</div><div>typedef itk::MeanSquaresImageToImageMetric&lt;FixedImageType,MovingImageType &gt; &nbsp; &nbsp;SSDMetricType;</div><div><br></div><div>typedef itk::InterpolateImageFunction&lt; MovingImageType,double&gt; InterpolatorType;</div><div>typedef itk::BSplineInterpolateImageFunction&lt;MovingImageType,double, double&gt;<span class="Apple-tab-span" style="white-space:pre">                </span> BsplineInterpolatorType;</div><div><br></div><div>typedef itk::MultiResolutionImageRegistrationMethod&lt;FixedImageType,MovingImageType &gt; &nbsp; MultiRegistrationType;</div><div><br></div><div><br></div><div>typedef itk::MultiResolutionPyramidImageFilter&lt;FixedImageType,MovingImageType &gt; &nbsp; FixedImagePyramidType;</div><div>typedef itk::MultiResolutionPyramidImageFilter&lt;FixedImageType,MovingImageType &gt; &nbsp; MovingImagePyramidType;</div><div><br></div><div>typedef itk::ResampleImageFilter&lt;FixedImageType,MovingImageType &gt; &nbsp; &nbsp;ResampleFilterType;</div><div><br></div><div><br></div><div><br></div><div>typename MetricType::Pointer metric = NULL;</div><div>typename InterpolatorType::Pointer &nbsp; interpolator = NULL;</div><div>typename OptimizerType::Pointer optimizer = NULL;</div><div><br></div><div>optimizer = RSGDOptimizerType::New();</div><div>metric = SSDMetricType::New();</div><div>interpolator = BsplineInterpolatorType::New();</div><div><br></div><div><br></div><div>typedef &nbsp; BsplineInterpolatorType * &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InterpolatorPointer;</div><div>InterpolatorPointer int_aux = dynamic_cast&lt; InterpolatorPointer &gt;(interpolator.GetPointer());</div><div>int_aux-&gt;SetSplineOrder(3);</div><div><br></div><div><br></div><div><br></div><div>typename TransformType::Pointer &nbsp;transform = TransformType::New();</div><div>typename MultiRegistrationType::Pointer &nbsp; multiregistration &nbsp;= MultiRegistrationType::New();</div><div><br></div><div>multiregistration-&gt;SetNumberOfThreads(this-&gt;m_NumberOfThreads);</div><div>typename FixedImagePyramidType::Pointer fixedImagePyramid = FixedImagePyramidType::New();</div><div>typename MovingImagePyramidType::Pointer movingImagePyramid = MovingImagePyramidType::New();</div><div><br></div><div>typename &nbsp;FixedImageType::RegionType fixedRegion = fixedImage-&gt;GetBufferedRegion();</div><div><br></div><div>multiregistration-&gt;SetMetric( &nbsp; &nbsp; &nbsp; &nbsp;metrics[i] &nbsp; &nbsp;);</div><div>multiregistration-&gt;SetOptimizer( &nbsp; &nbsp; optimizer &nbsp; &nbsp; );</div><div>multiregistration-&gt;SetInterpolator( &nbsp;interpolator &nbsp;);</div><div>multiregistration-&gt;SetTransform( transform );</div><div><br></div><div>// Setup the registration</div><div>multiregistration-&gt;SetFixedImagePyramid( fixedImagePyramid );</div><div>multiregistration-&gt;SetMovingImagePyramid( movingImagePyramid );</div><div>multiregistration-&gt;SetFixedImage( &nbsp;fixedImage &nbsp; );</div><div>multiregistration-&gt;SetMovingImage( &nbsp; movingImage);</div><div>multiregistration-&gt;SetFixedImageRegion( fixedRegion );</div><div><br></div><div><br></div><div><br></div><div>// &nbsp;Here we define the parameters of the BSplineDeformableTransform grid. &nbsp;We</div><div>// &nbsp;arbitrarily decide to use a grid with $5 \times 5$ nodes within the image.</div><div>// &nbsp;The reader should note that the BSpline computation requires a</div><div>// &nbsp;finite support region ( 1 grid node at the lower borders and 2</div><div>// &nbsp;grid nodes at upper borders). Therefore in this example, we set</div><div>// &nbsp;the grid size to be $8 \times 8$ and place the grid origin such that</div><div>// &nbsp;grid node (1,1) coincides with the first pixel in the fixed image.</div><div><br></div><div>unsigned int numberOfGridNodesInOneDimension = 1 + fixedImage-&gt;GetLargestPossibleRegion().GetSize()[0] / space;</div><div><br></div><div><br></div><div><br></div><div>#if ITK_VERSION_MAJOR &lt; 4</div><div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::RegionType bsplineRegion;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::RegionType::SizeType &nbsp; gridSizeOnImage;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::RegionType::SizeType &nbsp; gridBorderSize;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::RegionType::SizeType &nbsp; totalGridSize;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>gridSizeOnImage.Fill( numberOfGridNodesInOneDimension );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>gridBorderSize.Fill( SplineOrder ); &nbsp; &nbsp;// Border for spline order = 3 ( 1 lower, 2 upper )</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>totalGridSize = gridSizeOnImage + gridBorderSize;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>bsplineRegion.SetSize( totalGridSize );</div><div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::SpacingType spacing = fixedImage-&gt;GetSpacing();</div><div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::OriginType origin = fixedImage-&gt;GetOrigin();</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename FixedImageType::SizeType fixedImageSize = fixedRegion.GetSize();</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>for(unsigned int r=0; r&lt;Dimension; r++)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>spacing[r] *= static_cast&lt;double&gt;(fixedImageSize[r] - 1) &nbsp;/</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span> &nbsp;static_cast&lt;double&gt;(gridSizeOnImage[r] - 1);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename FixedImageType::DirectionType gridDirection = fixedImage-&gt;GetDirection();</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::SpacingType gridOriginOffset = gridDirection * spacing;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::OriginType gridOrigin = origin - gridOriginOffset;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>transform-&gt;SetGridSpacing( spacing );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>transform-&gt;SetGridOrigin( gridOrigin );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>transform-&gt;SetGridRegion( bsplineRegion );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>transform-&gt;SetGridDirection( gridDirection );</div><div><br></div><div>#else</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::PhysicalDimensionsType &nbsp; fixedPhysicalDimensions;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::MeshSizeType &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; meshSize;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::OriginType &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fixedOrigin;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>for( unsigned int k=0; k&lt; Dimension; k++ )</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>fixedOrigin = fixedImage-&gt;GetOrigin()[k];</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>fixedPhysicalDimensions[k] = fixedImage-&gt;GetSpacing()[k] *</div><div><span class="Apple-tab-span" style="white-space:pre">                </span> &nbsp;static_cast&lt;double&gt;(</div><div><span class="Apple-tab-span" style="white-space:pre">                </span> &nbsp;fixedImage-&gt;GetLargestPossibleRegion().GetSize()[k] - 1 );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>meshSize.Fill( numberOfGridNodesInOneDimension - SplineOrder );</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>transform-&gt;SetTransformDomainOrigin( fixedOrigin );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>transform-&gt;SetTransformDomainPhysicalDimensions( fixedPhysicalDimensions );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>transform-&gt;SetTransformDomainMeshSize( meshSize );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>transform-&gt;SetTransformDomainDirection( fixedImage-&gt;GetDirection() );</div><div><br></div><div>#endif</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>const unsigned int numberOfParameters = transform-&gt;GetNumberOfParameters();</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typename TransformType::ParametersType parameters( numberOfParameters );</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>parameters.Fill( 0.0 );</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>transform-&gt;SetParameters( parameters );</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// &nbsp;We now pass the parameters of the current transform as the initial</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// &nbsp;parameters to be used when the registration process starts.</div><div><br></div><div><br></div><div>multiregistration-&gt;SetInitialTransformParameters( transform-&gt;GetParameters() );</div><div><br></div><div><br></div><div>multiregistration-&gt;SetNumberOfLevels( numberLevels );</div><div><br></div><div><br></div><div>// Set the parameters of the RegularStepGradientDescentOptimizer Optimizer.</div><div>typedef &nbsp; RSGDOptimizerType * &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OptimizerPointer;</div><div><br></div><div>OptimizerPointer opt = dynamic_cast&lt; OptimizerPointer &gt;(optimizer.GetPointer());</div><div>opt-&gt;SetNumberOfIterations( maxIt );</div><div>opt-&gt;SetRelaxationFactor( relaxFact );</div><div>opt-&gt;SetMaximumStepLength( steplength[1] );</div><div>opt-&gt;SetMinimumStepLength( steplength[0] );</div><div><br></div><div><br></div><div><br></div><div>std::cout &lt;&lt; "Initial Parameters = " &lt;&lt; std::endl;</div><div>std::cout &lt;&lt; transform-&gt;GetParameters() &lt;&lt; std::endl;</div><div><br></div><div><br></div><div>CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();</div><div>optimizer-&gt;AddObserver( itk::IterationEvent(), observer );</div><div><br></div><div><br></div><div>std::string metric_name = metric-&gt;GetNameOfClass();</div><div><br></div><div>std::cout &lt;&lt; std::endl &lt;&lt; "Starting " &lt;&lt;metric_name&lt;&lt;" Registration" &lt;&lt;std::endl;</div><div>multiregistration-&gt;StartRegistration();</div><div>std::cout &lt;&lt; "Optimizer stop condition = "</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&lt;&lt; multiregistration-&gt;GetOptimizer()-&gt;GetStopConditionDescription()</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&lt;&lt; std::endl;</div><div><br></div><div>std::string prefix = "SSD";</div><div><br></div><div>// the parameters type are the same for both optimizer.</div><div>typename RSGDOptimizerType::ParametersType finalParameters;</div><div>finalParameters = multiregistration-&gt;GetLastTransformParameters();</div><div>std::cout &lt;&lt; "Last Transform Parameters" &lt;&lt; std::endl;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>std::cout &lt;&lt; finalParameters &lt;&lt; std::endl;</div><div><br></div><div>unsigned int numberOfIterations = 0;</div><div>double bestValue = 0;</div><div><br></div><div>typedef &nbsp; RSGDOptimizerType * &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OptimizerPointer;</div><div><br></div><div><br></div><div>OptimizerPointer opt = dynamic_cast&lt; OptimizerPointer &gt;(optimizer.GetPointer());</div><div>numberOfIterations = opt-&gt;GetCurrentIteration();</div><div>bestValue = opt-&gt;GetValue();</div><div><br></div><div>// Print out results</div><div>//</div><div>std::cout &lt;&lt; "Result: " &lt;&lt; std::endl;</div><div>std::cout &lt;&lt; " Iterations &nbsp; &nbsp;= " &lt;&lt; numberOfIterations &lt;&lt; std::endl;</div><div>std::cout &lt;&lt; " Metric value &nbsp;= " &lt;&lt; bestValue &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt; std::endl;</div><div><br></div><div>transform-&gt;SetParameters( finalParameters );</div><div><br></div><div><br></div><div>typename ResampleFilterType::Pointer resample = ResampleFilterType::New();</div><div><br></div><div>resample-&gt;SetTransform( transform );</div><div>resample-&gt;SetInput( movingImage );</div><div>resample-&gt;SetSize( &nbsp; &nbsp;fixedImage-&gt;GetLargestPossibleRegion().GetSize() );</div><div>resample-&gt;SetOutputOrigin( &nbsp;fixedImage-&gt;GetOrigin() );</div><div>resample-&gt;SetOutputSpacing( fixedImage-&gt;GetSpacing() );</div><div>resample-&gt;SetOutputDirection( fixedImage-&gt;GetDirection() );</div><div>resample-&gt;SetDefaultPixelValue( 100 );</div><div>resample-&gt;Update();</div><div><br></div><div>typedef itk::CastImageFilter&lt;FixedImageType,OutputImageType &gt; CastFilterType;</div><div><br></div><div><br></div><div>typename OutputWriterType::Pointer &nbsp; &nbsp; &nbsp;writer = &nbsp;OutputWriterType::New();</div><div>typename CastFilterType::Pointer &nbsp;caster = &nbsp;CastFilterType::New();</div><div><br></div><div><br></div><div><br></div><div>if(salidaResample)</div><div>{</div><div>writer-&gt;SetFileName( (prefix+"_"+filenameResample).c_str() );</div><div><br></div><div>caster-&gt;SetInput( resample-&gt;GetOutput() );</div><div>writer-&gt;SetInput( caster-&gt;GetOutput() &nbsp; );</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>try</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>writer-&gt;Update();</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>catch( itk::ExceptionObject &amp; err )</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>std::cerr &lt;&lt; "ExceptionObject caught !" &lt;&lt; std::endl;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>std::cerr &lt;&lt; err &lt;&lt; std::endl;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>return EXIT_FAILURE;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div>}</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>Please any help will be useful</div><div apple-content-edited="true"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-weight: normal; font-style: normal; "><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-weight: normal; font-style: normal; ">Thanks,</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-weight: normal; font-style: normal; ">__________________________________<br><font class="Apple-style-span" color="#b7bfc5">|</font>&nbsp;Ariel Hernán Curiale Ph.D Student<br><font class="Apple-style-span" color="#b7bfc5">|</font>&nbsp;ETSI Telecomunicación<br><font class="Apple-style-span" color="#b7bfc5">|</font>&nbsp;Universidad de Valladolid<br><font class="Apple-style-span" color="#b7bfc5">|</font>&nbsp;Campus Miguel Delibes<br><font class="Apple-style-span" color="#b7bfc5">|</font>&nbsp;47011 Valladolid, Spain<br><font class="Apple-style-span" color="#b7bfc5">|</font>&nbsp;Phone: 983-423000 ext. 5590</div><div><font class="Apple-style-span" color="#b7b7b7" style="font-family: Helvetica; font-size: medium; font-weight: normal; font-style: normal; ">|</font>&nbsp;Web:&nbsp;<a href="http://www.curiale.com.ar/"><font class="Apple-style-span" color="#084ebe">www.curiale.com.ar</font></a><br><font class="Apple-style-span" color="#b7b7b7" style="font-family: Helvetica; font-size: medium; font-weight: normal; font-style: normal; ">|</font>_________________________________</div></div></div></div></div></div></div></div></div></div></div></div></div>
</div>
<br></body></html>