Orfeo Toolbox  3.16
itkMeanReciprocalSquareDifferencePointSetToImageMetric.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMeanReciprocalSquareDifferencePointSetToImageMetric.txx,v $
5  Language: C++
6  Date: $Date: 2009-04-06 16:49:21 $
7  Version: $Revision: 1.8 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkMeanReciprocalSquareDifferencePointSetToImageMetric_txx
18 #define __itkMeanReciprocalSquareDifferencePointSetToImageMetric_txx
19 
22 
23 namespace itk
24 {
25 
26 /*
27 * Constructor
28 */
29 template <class TFixedPointSet, class TMovingImage>
32 {
33  m_Lambda = 1.0;
34 }
35 
39 template <class TFixedPointSet, class TMovingImage>
42 ::GetValue( const TransformParametersType & parameters ) const
43 {
44 
45  FixedPointSetConstPointer fixedPointSet = this->GetFixedPointSet();
46 
47  if( !fixedPointSet )
48  {
49  itkExceptionMacro( << "Fixed point set has not been assigned" );
50  }
51 
52  PointIterator pointItr = fixedPointSet->GetPoints()->Begin();
53  PointIterator pointEnd = fixedPointSet->GetPoints()->End();
54 
55  PointDataIterator pointDataItr = fixedPointSet->GetPointData()->Begin();
56  PointDataIterator pointDataEnd = fixedPointSet->GetPointData()->End();
57 
58  MeasureType measure = NumericTraits< MeasureType >::Zero;
59 
60  this->m_NumberOfPixelsCounted = 0;
61  double lambdaSquared = vcl_pow(this->m_Lambda, 2);
62 
63  this->SetTransformParameters( parameters );
64 
65  typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
66 
67  while( pointItr != pointEnd && pointDataItr != pointDataEnd )
68  {
69  InputPointType inputPoint;
70  inputPoint.CastFrom( pointItr.Value() );
71  OutputPointType transformedPoint =
72  this->m_Transform->TransformPoint( inputPoint );
73 
74  if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
75  {
76  const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
77  const RealType fixedValue = pointDataItr.Value();
78  const RealType diff = movingValue - fixedValue;
79  const double diffSquared = diff * diff;
80  measure += 1.0 / ( lambdaSquared + diffSquared );
81  this->m_NumberOfPixelsCounted++;
82  }
83 
84  ++pointItr;
85  ++pointDataItr;
86  }
87 
88  if( !this->m_NumberOfPixelsCounted )
89  {
90  itkExceptionMacro(<<"All the points mapped to outside of the moving image");
91  }
92  else
93  {
94  measure *= (lambdaSquared / this->m_NumberOfPixelsCounted);
95  }
96 
97 
98  return measure;
99 
100 }
101 
102 /*
103  * Get the Derivative Measure
104  */
105 template < class TFixedPointSet, class TMovingImage>
106 void
109  DerivativeType & derivative ) const
110 {
111 
112  if( !this->GetGradientImage() )
113  {
114  itkExceptionMacro(<<"The gradient image is null, maybe you forgot to call Initialize()");
115  }
116 
117  FixedPointSetConstPointer fixedPointSet = this->GetFixedPointSet();
118 
119  if( !fixedPointSet )
120  {
121  itkExceptionMacro( << "Fixed image has not been assigned" );
122  }
123 
124  this->m_NumberOfPixelsCounted = 0;
125 
126  double lambdaSquared = vcl_pow(this->m_Lambda, 2);
127 
128  this->SetTransformParameters( parameters );
129 
130  typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
131 
132  const unsigned int ParametersDimension = this->GetNumberOfParameters();
133  derivative = DerivativeType( ParametersDimension );
134  derivative.Fill( NumericTraits<ITK_TYPENAME DerivativeType::ValueType>::Zero );
135 
136  PointIterator pointItr = fixedPointSet->GetPoints()->Begin();
137  PointIterator pointEnd = fixedPointSet->GetPoints()->End();
138 
139  PointDataIterator pointDataItr = fixedPointSet->GetPointData()->Begin();
140  PointDataIterator pointDataEnd = fixedPointSet->GetPointData()->End();
141 
142  while( pointItr != pointEnd && pointDataItr != pointDataEnd )
143  {
144  InputPointType inputPoint;
145  inputPoint.CastFrom( pointItr.Value() );
146  OutputPointType transformedPoint =
147  this->m_Transform->TransformPoint( inputPoint );
148 
149  if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
150  {
151  const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
152  const RealType fixedValue = pointDataItr.Value();
153 
154  this->m_NumberOfPixelsCounted++;
155  const RealType diff = movingValue - fixedValue;
156  const RealType diffSquared = diff * diff;
157 
158  // Now compute the derivatives
159  const TransformJacobianType & jacobian =
160  this->m_Transform->GetJacobian( inputPoint );
161 
162  // Get the gradient by NearestNeighboorInterpolation:
163  // which is equivalent to round up the point components.
164  typedef typename OutputPointType::CoordRepType CoordRepType;
166  MovingImageContinuousIndexType;
167 
168  MovingImageContinuousIndexType tempIndex;
169  this->m_MovingImage->TransformPhysicalPointToContinuousIndex( transformedPoint, tempIndex );
170 
171  typename MovingImageType::IndexType mappedIndex;
172  mappedIndex.CopyWithRound( tempIndex );
173 
174  const GradientPixelType gradient =
175  this->GetGradientImage()->GetPixel( mappedIndex );
176 
177  for(unsigned int par=0; par<ParametersDimension; par++)
178  {
179  RealType sum = NumericTraits< RealType >::Zero;
180  for(unsigned int dim=0; dim<Self::FixedPointSetDimension; dim++)
181  {
182  //Will it be computationally more efficient to instead calculate the
183  //derivative using finite differences ?
184  sum -= jacobian( dim, par ) *
185  gradient[dim] / (vcl_pow( lambdaSquared + diffSquared , 2));
186  }
187  derivative[par] += diff * sum;
188  }
189  }
190 
191  ++pointItr;
192  ++pointDataItr;
193  }
194 
195  if( !this->m_NumberOfPixelsCounted )
196  {
197  itkExceptionMacro(<<"All the points mapped to outside of the moving image");
198  }
199  else
200  {
201  for(unsigned int i=0; i<ParametersDimension; i++)
202  {
203  derivative[i] *= 2.0 * lambdaSquared / this->m_NumberOfPixelsCounted;
204  }
205  }
206 }
207 
208 /*
209  * Get both the match Measure and theDerivative Measure
210  */
211 template <class TFixedPointSet, class TMovingImage>
212 void
215  MeasureType & value, DerivativeType & derivative) const
216 {
217 
218  if( !this->GetGradientImage() )
219  {
220  itkExceptionMacro(<<"The gradient image is null, maybe you forgot to call Initialize()");
221  }
222 
223  FixedPointSetConstPointer fixedPointSet = this->GetFixedPointSet();
224 
225  if( !fixedPointSet )
226  {
227  itkExceptionMacro( << "Fixed image has not been assigned" );
228  }
229 
230  this->m_NumberOfPixelsCounted = 0;
231  MeasureType measure = NumericTraits< MeasureType >::Zero;
232 
233  this->SetTransformParameters( parameters );
234  double lambdaSquared = vcl_pow(this->m_Lambda, 2);
235 
236  typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
237 
238  const unsigned int ParametersDimension = this->GetNumberOfParameters();
239  derivative = DerivativeType( ParametersDimension );
240  derivative.Fill( NumericTraits<ITK_TYPENAME DerivativeType::ValueType>::Zero );
241 
242  PointIterator pointItr = fixedPointSet->GetPoints()->Begin();
243  PointIterator pointEnd = fixedPointSet->GetPoints()->End();
244 
245  PointDataIterator pointDataItr = fixedPointSet->GetPointData()->Begin();
246  PointDataIterator pointDataEnd = fixedPointSet->GetPointData()->End();
247 
248  while( pointItr != pointEnd && pointDataItr != pointDataEnd )
249  {
250  InputPointType inputPoint;
251  inputPoint.CastFrom( pointItr.Value() );
252  OutputPointType transformedPoint =
253  this->m_Transform->TransformPoint( inputPoint );
254 
255  if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
256  {
257  const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
258  const RealType fixedValue = pointDataItr.Value();
259 
260  this->m_NumberOfPixelsCounted++;
261 
262  // Now compute the derivatives
263  const TransformJacobianType & jacobian =
264  this->m_Transform->GetJacobian( inputPoint );
265 
266  const RealType diff = movingValue - fixedValue;
267  const RealType diffSquared = diff * diff;
268  measure += 1.0 / ( lambdaSquared + diffSquared );
269 
270  // Get the gradient by NearestNeighboorInterpolation:
271  // which is equivalent to round up the point components.
272  typedef typename OutputPointType::CoordRepType CoordRepType;
274  MovingImageContinuousIndexType;
275 
276  MovingImageContinuousIndexType tempIndex;
277  this->m_MovingImage->TransformPhysicalPointToContinuousIndex( transformedPoint, tempIndex );
278 
279  typename MovingImageType::IndexType mappedIndex;
280  mappedIndex.CopyWithRound( tempIndex );
281 
282  const GradientPixelType gradient =
283  this->GetGradientImage()->GetPixel( mappedIndex );
284 
285  for(unsigned int par=0; par<ParametersDimension; par++)
286  {
287  RealType sum = NumericTraits< RealType >::Zero;
288  for(unsigned int dim=0; dim<Self::FixedPointSetDimension; dim++)
289  {
290  sum -= jacobian( dim, par ) * gradient[dim] *
291  vcl_pow(lambdaSquared + diffSquared, 2 );
292  }
293  derivative[par] += diff * sum;
294  }
295 
296  }
297 
298  ++pointItr;
299  ++pointDataItr;
300  }
301 
302  if( !this->m_NumberOfPixelsCounted )
303  {
304  itkExceptionMacro(<<"All the points mapped to outside of the moving image");
305  }
306  else
307  {
308  for(unsigned int i=0; i<ParametersDimension; i++)
309  {
310  derivative[i] *= 2.0 * lambdaSquared / this->m_NumberOfPixelsCounted;
311  }
312  measure *= lambdaSquared / this->m_NumberOfPixelsCounted;
313  }
314 
315  value = measure;
316 }
317 
321 template <class TFixedPointSet, class TMovingImage>
322 void
324 ::PrintSelf(std::ostream& os, Indent indent) const
325 {
326  Superclass::PrintSelf( os, indent );
327  os << "Lambda factor = " << m_Lambda << std::endl;
328 }
329 
330 
331 } // end namespace itk
332 
333 
334 #endif

Generated at Sat Feb 2 2013 23:52:12 for Orfeo Toolbox with doxygen 1.8.1.1