Orfeo Toolbox  3.16
itkMeanSquaresPointSetToImageMetric.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMeanSquaresPointSetToImageMetric.txx,v $
5  Language: C++
6  Date: $Date: 2009-01-24 20:03:00 $
7  Version: $Revision: 1.20 $
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 __itkMeanSquaresPointSetToImageMetric_txx
18 #define __itkMeanSquaresPointSetToImageMetric_txx
19 
22 
23 namespace itk
24 {
25 
29 template <class TFixedPointSet, class TMovingImage>
32 {
33 }
34 
38 template <class TFixedPointSet, class TMovingImage>
41 ::GetValue( const TransformParametersType & parameters ) const
42 {
43 
44  FixedPointSetConstPointer fixedPointSet = this->GetFixedPointSet();
45 
46  if( !fixedPointSet )
47  {
48  itkExceptionMacro( << "Fixed point set has not been assigned" );
49  }
50 
51  PointIterator pointItr = fixedPointSet->GetPoints()->Begin();
52  PointIterator pointEnd = fixedPointSet->GetPoints()->End();
53 
54  PointDataIterator pointDataItr = fixedPointSet->GetPointData()->Begin();
55  PointDataIterator pointDataEnd = fixedPointSet->GetPointData()->End();
56 
57  MeasureType measure = NumericTraits< MeasureType >::Zero;
58 
59  this->m_NumberOfPixelsCounted = 0;
60 
61  this->SetTransformParameters( parameters );
62 
63  typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
64 
65  while( pointItr != pointEnd && pointDataItr != pointDataEnd )
66  {
67  InputPointType inputPoint;
68  inputPoint.CastFrom( pointItr.Value() );
69  OutputPointType transformedPoint =
70  this->m_Transform->TransformPoint( inputPoint );
71 
72  if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
73  {
74  const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
75  const RealType fixedValue = pointDataItr.Value();
76  const RealType diff = movingValue - fixedValue;
77  measure += diff * diff;
78  this->m_NumberOfPixelsCounted++;
79  }
80 
81  ++pointItr;
82  ++pointDataItr;
83  }
84 
85  if( !this->m_NumberOfPixelsCounted )
86  {
87  itkExceptionMacro(<<"All the points mapped to outside of the moving image");
88  }
89  else
90  {
91  measure /= this->m_NumberOfPixelsCounted;
92  }
93 
94 
95  return measure;
96 
97 }
98 
102 template < class TFixedPointSet, class TMovingImage>
103 void
106  DerivativeType & derivative ) const
107 {
108 
109  if( !this->GetGradientImage() )
110  {
111  itkExceptionMacro(<<"The gradient image is null, maybe you forgot to call Initialize()");
112  }
113 
114  FixedPointSetConstPointer fixedPointSet = this->GetFixedPointSet();
115 
116  if( !fixedPointSet )
117  {
118  itkExceptionMacro( << "Fixed image has not been assigned" );
119  }
120 
121  this->m_NumberOfPixelsCounted = 0;
122 
123  this->SetTransformParameters( parameters );
124 
125  typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
126 
127  const unsigned int ParametersDimension = this->GetNumberOfParameters();
128  derivative = DerivativeType( ParametersDimension );
129  derivative.Fill( NumericTraits<ITK_TYPENAME DerivativeType::ValueType>::Zero );
130 
131  PointIterator pointItr = fixedPointSet->GetPoints()->Begin();
132  PointIterator pointEnd = fixedPointSet->GetPoints()->End();
133 
134  PointDataIterator pointDataItr = fixedPointSet->GetPointData()->Begin();
135  PointDataIterator pointDataEnd = fixedPointSet->GetPointData()->End();
136 
137  while( pointItr != pointEnd && pointDataItr != pointDataEnd )
138  {
139  InputPointType inputPoint;
140  inputPoint.CastFrom( pointItr.Value() );
141  OutputPointType transformedPoint =
142  this->m_Transform->TransformPoint( inputPoint );
143 
144  if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
145  {
146  const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
147  const RealType fixedValue = pointDataItr.Value();
148 
149  this->m_NumberOfPixelsCounted++;
150  const RealType diff = movingValue - fixedValue;
151 
152 
153  // Now compute the derivatives
154  const TransformJacobianType & jacobian =
155  this->m_Transform->GetJacobian( inputPoint );
156 
157  // Get the gradient by NearestNeighboorInterpolation:
158  // which is equivalent to round up the point components.
159  typedef typename OutputPointType::CoordRepType CoordRepType;
161  MovingImageContinuousIndexType;
162 
163  MovingImageContinuousIndexType tempIndex;
164  this->m_MovingImage->TransformPhysicalPointToContinuousIndex( transformedPoint, tempIndex );
165 
166  typename MovingImageType::IndexType mappedIndex;
167  mappedIndex.CopyWithRound( tempIndex );
168 
169  const GradientPixelType gradient =
170  this->GetGradientImage()->GetPixel( mappedIndex );
171 
172  for(unsigned int par=0; par<ParametersDimension; par++)
173  {
174  RealType sum = NumericTraits< RealType >::Zero;
175  for(unsigned int dim=0; dim<Self::FixedPointSetDimension; dim++)
176  {
177  sum += 2.0 * diff * jacobian( dim, par ) * gradient[dim];
178  }
179  derivative[par] += sum;
180  }
181  }
182 
183  ++pointItr;
184  ++pointDataItr;
185  }
186 
187  if( !this->m_NumberOfPixelsCounted )
188  {
189  itkExceptionMacro(<<"All the points mapped to outside of the moving image");
190  }
191  else
192  {
193  for(unsigned int i=0; i<ParametersDimension; i++)
194  {
195  derivative[i] /= this->m_NumberOfPixelsCounted;
196  }
197  }
198 }
199 
200 
201 /*
202  * Get both the match Measure and theDerivative Measure
203  */
204 template <class TFixedPointSet, class TMovingImage>
205 void
208  MeasureType & value, DerivativeType & derivative) const
209 {
210 
211  if( !this->GetGradientImage() )
212  {
213  itkExceptionMacro(<<"The gradient image is null, maybe you forgot to call Initialize()");
214  }
215 
216  FixedPointSetConstPointer fixedPointSet = this->GetFixedPointSet();
217 
218  if( !fixedPointSet )
219  {
220  itkExceptionMacro( << "Fixed image has not been assigned" );
221  }
222 
223  this->m_NumberOfPixelsCounted = 0;
224  MeasureType measure = NumericTraits< MeasureType >::Zero;
225 
226  this->SetTransformParameters( parameters );
227 
228  typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
229 
230  const unsigned int ParametersDimension = this->GetNumberOfParameters();
231  derivative = DerivativeType( ParametersDimension );
232  derivative.Fill( NumericTraits<ITK_TYPENAME DerivativeType::ValueType>::Zero );
233 
234  PointIterator pointItr = fixedPointSet->GetPoints()->Begin();
235  PointIterator pointEnd = fixedPointSet->GetPoints()->End();
236 
237  PointDataIterator pointDataItr = fixedPointSet->GetPointData()->Begin();
238  PointDataIterator pointDataEnd = fixedPointSet->GetPointData()->End();
239 
240  while( pointItr != pointEnd && pointDataItr != pointDataEnd )
241  {
242  InputPointType inputPoint;
243  inputPoint.CastFrom( pointItr.Value() );
244  OutputPointType transformedPoint =
245  this->m_Transform->TransformPoint( inputPoint );
246 
247  if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
248  {
249  const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
250  const RealType fixedValue = pointDataItr.Value();
251 
252  this->m_NumberOfPixelsCounted++;
253 
254  // Now compute the derivatives
255  const TransformJacobianType & jacobian =
256  this->m_Transform->GetJacobian( inputPoint );
257 
258  const RealType diff = movingValue - fixedValue;
259 
260  measure += diff * diff;
261 
262  // Get the gradient by NearestNeighboorInterpolation:
263  // which is equivalent to round up the point components.
264  typedef typename OutputPointType::CoordRepType CoordRepType;
266  MovingImageContinuousIndexType;
267 
268  MovingImageContinuousIndexType tempIndex;
269  this->m_MovingImage->TransformPhysicalPointToContinuousIndex( transformedPoint, tempIndex );
270 
271  typename MovingImageType::IndexType mappedIndex;
272  mappedIndex.CopyWithRound( tempIndex );
273 
274  const GradientPixelType gradient =
275  this->GetGradientImage()->GetPixel( mappedIndex );
276 
277  for(unsigned int par=0; par<ParametersDimension; par++)
278  {
279  RealType sum = NumericTraits< RealType >::Zero;
280  for(unsigned int dim=0; dim<Self::FixedPointSetDimension; dim++)
281  {
282  sum += 2.0 * diff * jacobian( dim, par ) * gradient[dim];
283  }
284  derivative[par] += sum;
285  }
286 
287  }
288 
289  ++pointItr;
290  ++pointDataItr;
291  }
292 
293  if( !this->m_NumberOfPixelsCounted )
294  {
295  itkExceptionMacro(<<"All the points mapped to outside of the moving image");
296  }
297  else
298  {
299  for(unsigned int i=0; i<ParametersDimension; i++)
300  {
301  derivative[i] /= this->m_NumberOfPixelsCounted;
302  }
303  measure /= this->m_NumberOfPixelsCounted;
304  }
305 
306  value = measure;
307 
308 
309 }
310 
311 } // end namespace itk
312 
313 
314 #endif

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