00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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;
00054 m_FixedPointSet = 0;
00055 m_ImageMetric = 0;
00056
00057 this->SetComputeGradient(false);
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
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
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
00118 this->GetValueAndDerivative( parameters, value, derivative );
00119 }
00120
00121
00122
00123
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
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
00193
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
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
00225 this->GetPointValueAndDerivative( parameters, value, derivative );
00226 }
00227
00228
00229
00230
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
00271
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 }
00311
00312
00313 #endif