frePointSupportedImageToImageMetric.txx

Go to the documentation of this file.
00001 /*=========================================================================
00002  
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.0.7.2
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: frePointSupportedImageToImageMetric.txx,v $
00007   Language:  C++
00008  
00009  
00010  
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016  
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020  
00021 =========================================================================*/
00022 #ifndef _frePointSupportedImageToImageMetric_txx
00023 #define _frePointSupportedImageToImageMetric_txx
00024 
00025 #include "frePointSupportedImageToImageMetric.h"
00026 #include "itkImageRegionConstIteratorWithIndex.h"
00027 
00028 namespace itk
00029 {
00030 
00031 /*
00032 * Constructor
00033 */
00034 template < class TFixedImage, class TMovingImage, class TFixedPointSet, class TMovingPointSet>
00035 PointSupportedImageToImageMetric<TFixedImage,TMovingImage,TFixedPointSet,TMovingPointSet>
00036 ::PointSupportedImageToImageMetric()
00037 {
00038     itkDebugMacro("Constructor");
00039 
00040     m_Alpha = 1.0;
00041 
00042     m_LastImageMetricValue = 0;
00043     m_LastPointMetricValue = 0;
00044 
00045     m_LastImageDerivative.Fill(0);
00046     m_LastPointDerivative.Fill(0);
00047 
00048     MovingPointSetConstPointer m_MovingPointSet;
00049     FixedPointSetConstPointer m_FixedPointSet;
00050 
00051     ImageMetricPointer m_ImageMetric;
00052 
00053     m_MovingPointSet    = 0; // has to be provided by the user.
00054     m_FixedPointSet   = 0; // has to be provided by the user.
00055     m_ImageMetric = 0; // has to be provided by the user.
00056 
00057     this->SetComputeGradient(false); // don't use the default gradient for the point calculation
00058 }
00059 
00060 template < class TFixedImage, class TMovingImage, class TFixedPointSet, class TMovingPointSet>
00061 void
00062 PointSupportedImageToImageMetric<TFixedImage,TMovingImage,TFixedPointSet,TMovingPointSet>
00063 ::Initialize(void) throw ( ExceptionObject )
00064 {
00065     if( !m_ImageMetric )
00066     {
00067         itkExceptionMacro(<<"ImageMetric is not present");
00068     }
00069 
00070     m_ImageMetric->SetMovingImage(this->GetMovingImage());
00071     m_ImageMetric->SetFixedImage(this->GetFixedImage());
00072     m_ImageMetric->SetTransform(this->m_Transform);
00073     m_ImageMetric->SetInterpolator(this->m_Interpolator);
00074     m_ImageMetric->SetFixedImageRegion(this->GetFixedImageRegion());
00075     m_ImageMetric->SetMovingImageMask(this->m_MovingImageMask);
00076     m_ImageMetric->SetFixedImageMask(this->m_FixedImageMask);
00077 
00078     Superclass::Initialize();
00079     m_ImageMetric->Initialize();
00080 }
00081 
00082 /*
00083 * Get the match Measure
00084 */
00085 template < class TFixedImage, class TMovingImage, class TFixedPointSet, class TMovingPointSet>
00086 typename PointSupportedImageToImageMetric<TFixedImage,TMovingImage,TFixedPointSet,TMovingPointSet>::MeasureType
00087 PointSupportedImageToImageMetric<TFixedImage,TMovingImage,TFixedPointSet,TMovingPointSet>
00088 ::GetValue( const TransformParametersType & parameters ) const
00089 {
00090     itkDebugMacro("GetValue( " << parameters << " ) ");
00091 
00092     if( m_ImageMetric.IsNull())
00093     {
00094         itkExceptionMacro( << "Image metric has not been assigned" );
00095     }
00096 
00097     m_LastImageMetricValue = m_ImageMetric->GetValue(parameters);
00098     m_LastPointMetricValue = this->GetPointValue(parameters);
00099 
00100     m_LastPointMetricValue *= m_Alpha;
00101     MeasureType measure = m_LastImageMetricValue + m_LastPointMetricValue;
00102 
00103     return measure;
00104 }
00105 
00106 
00107 /*
00108 * Get the Derivative Measure
00109 */
00110 template < class TFixedImage, class TMovingImage, class TFixedPointSet, class TMovingPointSet>
00111 void
00112 PointSupportedImageToImageMetric<TFixedImage,TMovingImage,TFixedPointSet,TMovingPointSet>
00113 ::GetDerivative( const TransformParametersType & parameters,
00114                  DerivativeType & derivative  ) const
00115 {
00116     MeasureType value;
00117     // call the combined version
00118     this->GetValueAndDerivative( parameters, value, derivative );
00119 }
00120 
00121 
00122 /*
00123 * Get both the match Measure and theDerivative Measure 
00124 */
00125 template < class TFixedImage, class TMovingImage, class TFixedPointSet, class TMovingPointSet>
00126 void
00127 PointSupportedImageToImageMetric<TFixedImage,TMovingImage,TFixedPointSet,TMovingPointSet>
00128 ::GetValueAndDerivative(const TransformParametersType & parameters,
00129                         MeasureType & value, DerivativeType  & derivative) const
00130 {
00131     itkDebugMacro("GetValueAndDerivative( " << parameters << " ) ");
00132 
00133     if( m_ImageMetric.IsNull())
00134     {
00135         itkExceptionMacro( << "Image metric has not been assigned" );
00136     }
00137 
00138     m_ImageMetric->GetValueAndDerivative(parameters, m_LastImageMetricValue, m_LastImageDerivative);
00139     this->GetPointValueAndDerivative(parameters, m_LastPointMetricValue, m_LastPointDerivative);
00140     
00141     m_LastPointMetricValue *= m_Alpha;
00142     value = m_LastImageMetricValue + m_LastPointMetricValue;
00143 
00144     derivative = m_LastImageDerivative;
00145 
00146     for (unsigned int i=0; i<derivative.Size(); i++)
00147     {
00148       m_LastPointDerivative[i] *= m_Alpha;
00149       derivative += m_LastPointDerivative[i];
00150     }
00151 }
00152 
00153 /*
00154 * Get the match Measure
00155 */
00156 template < class TFixedImage, class TMovingImage, class TFixedPointSet, class TMovingPointSet>
00157 typename PointSupportedImageToImageMetric<TFixedImage,TMovingImage,TFixedPointSet,TMovingPointSet>::MeasureType
00158 PointSupportedImageToImageMetric<TFixedImage,TMovingImage,TFixedPointSet,TMovingPointSet>
00159 ::GetPointValue( const TransformParametersType & parameters ) const
00160 {
00161   itkDebugMacro("GetPointValue( " << parameters << " ) ");
00162 
00163   if( m_MovingPointSet.IsNull() || m_FixedPointSet.IsNull())
00164   {
00165     return 0;
00166   }
00167 
00168   if( m_MovingPointSet->GetNumberOfPoints()!=m_FixedPointSet->GetNumberOfPoints())
00169   {
00170     itkExceptionMacro( << "Point sets have not the same size. Moving point set: "<<m_MovingPointSet->GetNumberOfPoints()<<"; fixed point set: "<< m_FixedPointSet->GetNumberOfPoints() );
00171   }
00172 
00173   typedef typename FixedPointSetType::PointsContainer::ConstIterator  FixedPointIterator;
00174   typedef typename MovingPointSetType::PointsContainer::ConstIterator  MovingPointIterator;
00175 
00176   MovingPointIterator movingPointItr = m_MovingPointSet->GetPoints()->Begin();
00177   MovingPointIterator movingPointEnd = m_MovingPointSet->GetPoints()->End();
00178 
00179   FixedPointIterator fixedPointItr = m_FixedPointSet->GetPoints()->Begin();
00180 
00181   MeasureType measure = NumericTraits< MeasureType >::Zero;
00182 
00183   this->SetTransformParameters( parameters );
00184 
00185   while( movingPointItr != movingPointEnd )
00186   {
00187     InputPointType  fixedPoint;
00188     fixedPoint.CastFrom( fixedPointItr.Value() );
00189     InputPointType  movingPoint;
00190     movingPoint.CastFrom( movingPointItr.Value() );
00191    
00192     //Because we use the same transform as the image metric the transform is
00193     //a back transform, so it maps from fixed to moving
00194     typename Superclass::OutputPointType transformedPoint = 
00195     this->m_Transform->TransformPoint( fixedPoint);
00196 
00197     typename InputPointType::VectorType distance = movingPoint - transformedPoint;
00198 
00199     measure += distance.GetSquaredNorm();
00200 
00201     ++movingPointItr;
00202     ++fixedPointItr;
00203   }
00204 
00205   if (m_MovingPointSet->GetNumberOfPoints() >0)
00206   {
00207     measure /= m_MovingPointSet->GetNumberOfPoints ();
00208   }
00209 
00210   return measure;
00211 }
00212 
00213 
00214 /*
00215 * Get the Derivative Measure
00216 */
00217 template < class TFixedImage, class TMovingImage, class TFixedPointSet, class TMovingPointSet>
00218 void
00219 PointSupportedImageToImageMetric<TFixedImage,TMovingImage,TFixedPointSet,TMovingPointSet>
00220 ::GetPointDerivative( const TransformParametersType & parameters,
00221                  DerivativeType & derivative  ) const
00222 {
00223     MeasureType value;
00224     // call the combined version
00225     this->GetPointValueAndDerivative( parameters, value, derivative );
00226 }
00227 
00228 
00229 /*
00230 * Get both the match Measure and theDerivative Measure 
00231 */
00232 template < class TFixedImage, class TMovingImage, class TFixedPointSet, class TMovingPointSet>
00233 void
00234 PointSupportedImageToImageMetric<TFixedImage,TMovingImage,TFixedPointSet,TMovingPointSet>
00235 ::GetPointValueAndDerivative(const TransformParametersType & parameters,
00236                         MeasureType & value, DerivativeType  & derivative) const
00237 {
00238   itkDebugMacro("GetPointValueAndDerivative( " << parameters << " ) ");
00239 
00240   const unsigned int ParametersDimension = this->GetNumberOfParameters();
00241   derivative = DerivativeType( ParametersDimension );
00242   derivative.Fill( NumericTraits<ITK_TYPENAME DerivativeType::ValueType>::Zero );
00243 
00244   value = NumericTraits< MeasureType >::Zero;
00245 
00246   if( m_MovingPointSet.IsNotNull() && m_FixedPointSet.IsNotNull())
00247   {
00248     if( m_MovingPointSet->GetNumberOfPoints()!=m_FixedPointSet->GetNumberOfPoints())
00249     {
00250       itkExceptionMacro( << "Point sets have not the same size. Moving point set: "<<m_MovingPointSet->GetNumberOfPoints()<<"; fixed point set: "<< m_FixedPointSet->GetNumberOfPoints() );
00251     }
00252 
00253     typedef typename FixedPointSetType::PointsContainer::ConstIterator  FixedPointIterator;
00254     typedef typename MovingPointSetType::PointsContainer::ConstIterator  MovingPointIterator;
00255 
00256     MovingPointIterator movingPointItr = m_MovingPointSet->GetPoints()->Begin();
00257     MovingPointIterator movingPointEnd = m_MovingPointSet->GetPoints()->End();
00258 
00259     FixedPointIterator fixedPointItr = m_FixedPointSet->GetPoints()->Begin();
00260 
00261     this->SetTransformParameters( parameters );
00262 
00263     while( movingPointItr != movingPointEnd )
00264     {
00265       InputPointType  fixedPoint;
00266       fixedPoint.CastFrom( fixedPointItr.Value() );
00267       InputPointType  movingPoint;
00268       movingPoint.CastFrom( movingPointItr.Value() );
00269      
00270       //Because we use the same transform as the image metric the transform is
00271       //a back transform, so it maps from fixed to moving
00272       typename Superclass::OutputPointType transformedPoint = 
00273       this->m_Transform->TransformPoint( fixedPoint);
00274 
00275       typename InputPointType::VectorType distance = movingPoint - transformedPoint;
00276 
00277       const TransformJacobianType & jacobian =
00278                   this->m_Transform->GetJacobian( fixedPoint );
00279 
00280       for(unsigned int par=0; par<ParametersDimension; par++)
00281       {
00282           RealType sum = NumericTraits< RealType >::Zero;
00283 
00284           for(unsigned int dim=0; dim<FixedPointSetType::PointDimension; dim++)
00285           {
00286               sum += -2.0 * distance[dim] * jacobian( dim, par );
00287           }
00288 
00289           derivative[par] += sum;
00290       }
00291 
00292       value += distance.GetSquaredNorm();
00293 
00294       ++movingPointItr;
00295       ++fixedPointItr;
00296     }
00297 
00298     if (m_MovingPointSet->GetNumberOfPoints()>0)
00299     {
00300       value /= m_MovingPointSet->GetNumberOfPoints();
00301 
00302       for(unsigned int par=0; par<ParametersDimension; par++)
00303       {
00304         derivative[par] /= m_MovingPointSet->GetNumberOfPoints();
00305       }
00306     }
00307   }
00308 }
00309 
00310 } // end namespace itk
00311 
00312 
00313 #endif

Generated at Sat Oct 13 17:04:43 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000