Orfeo Toolbox  3.16
itkImageRegistrationMethod.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkImageRegistrationMethod.txx,v $
5  Language: C++
6  Date: $Date: 2009-01-24 20:02:56 $
7  Version: $Revision: 1.31 $
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 __itkImageRegistrationMethod_txx
18 #define __itkImageRegistrationMethod_txx
19 
21 
22 
23 namespace itk
24 {
25 
29 template < typename TFixedImage, typename TMovingImage >
32 {
33 
34  this->SetNumberOfRequiredOutputs( 1 ); // for the Transform
35 
36  m_FixedImage = 0; // has to be provided by the user.
37  m_MovingImage = 0; // has to be provided by the user.
38  m_Transform = 0; // has to be provided by the user.
39  m_Interpolator = 0; // has to be provided by the user.
40  m_Metric = 0; // has to be provided by the user.
41  m_Optimizer = 0; // has to be provided by the user.
42 
43 
44  m_InitialTransformParameters = ParametersType(1);
45  m_LastTransformParameters = ParametersType(1);
46 
47  m_InitialTransformParameters.Fill( 0.0f );
48  m_LastTransformParameters.Fill( 0.0f );
49 
50  m_FixedImageRegionDefined = false;
51 
52 
53  TransformOutputPointer transformDecorator =
54  static_cast< TransformOutputType * >(
55  this->MakeOutput(0).GetPointer() );
56 
57  this->ProcessObject::SetNthOutput( 0, transformDecorator.GetPointer() );
58 
59 #ifdef ITK_USE_OPTIMIZED_REGISTRATION_METHODS
60  this->SetNumberOfThreads( this->GetMultiThreader()->GetNumberOfThreads() );
61 #else
62  this->SetNumberOfThreads( 1 );
63  this->GetMultiThreader()->SetNumberOfThreads( this->GetNumberOfThreads() );
64 #endif
65 }
66 
67 
71 template < typename TFixedImage, typename TMovingImage >
72 unsigned long
74 ::GetMTime() const
75 {
76  unsigned long mtime = Superclass::GetMTime();
77  unsigned long m;
78 
79 
80  // Some of the following should be removed once ivars are put in the
81  // input and output lists
82 
83  if (m_Transform)
84  {
85  m = m_Transform->GetMTime();
86  mtime = (m > mtime ? m : mtime);
87  }
88 
89  if (m_Interpolator)
90  {
91  m = m_Interpolator->GetMTime();
92  mtime = (m > mtime ? m : mtime);
93  }
94 
95  if (m_Metric)
96  {
97  m = m_Metric->GetMTime();
98  mtime = (m > mtime ? m : mtime);
99  }
100 
101  if (m_Optimizer)
102  {
103  m = m_Optimizer->GetMTime();
104  mtime = (m > mtime ? m : mtime);
105  }
106 
107  if (m_FixedImage)
108  {
109  m = m_FixedImage->GetMTime();
110  mtime = (m > mtime ? m : mtime);
111  }
112 
113  if (m_MovingImage)
114  {
115  m = m_MovingImage->GetMTime();
116  mtime = (m > mtime ? m : mtime);
117  }
118 
119  return mtime;
120 
121 }
122 
123 /*
124  * Set the initial transform parameters
125  */
126 template < typename TFixedImage, typename TMovingImage >
127 void
130 {
131  m_InitialTransformParameters = param;
132  this->Modified();
133 }
134 
135 
140 template < typename TFixedImage, typename TMovingImage >
141 void
144 {
145  m_FixedImageRegion = region;
146  m_FixedImageRegionDefined = true;
147  this->Modified();
148 }
149 
150 
154 template < typename TFixedImage, typename TMovingImage >
155 void
158 {
159 
160  if( !m_FixedImage )
161  {
162  itkExceptionMacro(<<"FixedImage is not present");
163  }
164 
165  if( !m_MovingImage )
166  {
167  itkExceptionMacro(<<"MovingImage is not present");
168  }
169 
170  if ( !m_Metric )
171  {
172  itkExceptionMacro(<<"Metric is not present" );
173  }
174 
175  if ( !m_Optimizer )
176  {
177  itkExceptionMacro(<<"Optimizer is not present" );
178  }
179 
180  if( !m_Transform )
181  {
182  itkExceptionMacro(<<"Transform is not present");
183  }
184 
185  //
186  // Connect the transform to the Decorator.
187  //
188  TransformOutputType * transformOutput =
189  static_cast< TransformOutputType * >( this->ProcessObject::GetOutput(0) );
190 
191  transformOutput->Set( m_Transform.GetPointer() );
192 
193 
194  if( !m_Interpolator )
195  {
196  itkExceptionMacro(<<"Interpolator is not present");
197  }
198 
199  // Setup the metric
200 #ifdef ITK_USE_OPTIMIZED_REGISTRATION_METHODS
201  this->GetMultiThreader()->SetNumberOfThreads( this->GetNumberOfThreads() );
202  this->m_Metric->SetNumberOfThreads( this->GetNumberOfThreads() );
203 #endif
204  m_Metric->SetMovingImage( m_MovingImage );
205  m_Metric->SetFixedImage( m_FixedImage );
206  m_Metric->SetTransform( m_Transform );
207  m_Metric->SetInterpolator( m_Interpolator );
208 
209  if( m_FixedImageRegionDefined )
210  {
211  m_Metric->SetFixedImageRegion( m_FixedImageRegion );
212  }
213  else
214  {
215  m_Metric->SetFixedImageRegion( m_FixedImage->GetBufferedRegion() );
216  }
217 
218  m_Metric->Initialize();
219 
220  // Setup the optimizer
221  m_Optimizer->SetCostFunction( m_Metric );
222 
223  // Validate initial transform parameters
224  if ( m_InitialTransformParameters.Size() !=
225  m_Transform->GetNumberOfParameters() )
226  {
227  itkExceptionMacro(<<"Size mismatch between initial parameters and transform." <<
228  "Expected " << m_Transform->GetNumberOfParameters() << " parameters and received "
229  << m_InitialTransformParameters.Size() << " parameters");
230  }
231 
232  m_Optimizer->SetInitialPosition( m_InitialTransformParameters );
233 
234 }
235 
236 
240 template < typename TFixedImage, typename TMovingImage >
241 void
244 {
245 
246  // StartRegistration is an old API from before
247  // ImageRegistrationMethod was a subclass of ProcessObject.
248  // Historically, one could call StartRegistration() instead of
249  // calling Update(). However, when called directly by the user, the
250  // inputs to ImageRegistrationMethod may not be up to date. This
251  // may cause an unexpected behavior.
252  //
253  // Since we cannot eliminate StartRegistration for backward
254  // compability reasons, we check whether StartRegistration was
255  // called directly or whether Update() (which in turn called
256  // StartRegistration()).
257  if (!m_Updating)
258  {
259  this->Update();
260  }
261  else
262  {
263  ParametersType empty(1);
264  empty.Fill( 0.0 );
265  try
266  {
267  // initialize the interconnects between components
268  this->Initialize();
269  }
270  catch( ExceptionObject& err )
271  {
272  m_LastTransformParameters = empty;
273 
274  // pass exception to caller
275  throw err;
276  }
277 
278  this->StartOptimization();
279  }
280 }
281 
282 
286 template < typename TFixedImage, typename TMovingImage >
287 void
290 {
291 
292  try
293  {
294  // do the optimization
295  m_Optimizer->StartOptimization();
296  }
297  catch( ExceptionObject& err )
298  {
299  // An error has occurred in the optimization.
300  // Update the parameters
301  m_LastTransformParameters = m_Optimizer->GetCurrentPosition();
302 
303  // Pass exception to caller
304  throw err;
305  }
306 
307  // get the results
308  m_LastTransformParameters = m_Optimizer->GetCurrentPosition();
309  m_Transform->SetParameters( m_LastTransformParameters );
310 }
311 
312 
316 template < typename TFixedImage, typename TMovingImage >
317 void
319 ::PrintSelf(std::ostream& os, Indent indent) const
320 {
321  Superclass::PrintSelf( os, indent );
322  os << indent << "Metric: " << m_Metric.GetPointer() << std::endl;
323  os << indent << "Optimizer: " << m_Optimizer.GetPointer() << std::endl;
324  os << indent << "Transform: " << m_Transform.GetPointer() << std::endl;
325  os << indent << "Interpolator: " << m_Interpolator.GetPointer() << std::endl;
326  os << indent << "Fixed Image: " << m_FixedImage.GetPointer() << std::endl;
327  os << indent << "Moving Image: " << m_MovingImage.GetPointer() << std::endl;
328  os << indent << "Fixed Image Region Defined: " << m_FixedImageRegionDefined << std::endl;
329  os << indent << "Fixed Image Region: " << m_FixedImageRegion << std::endl;
330  os << indent << "Initial Transform Parameters: " << m_InitialTransformParameters << std::endl;
331  os << indent << "Last Transform Parameters: " << m_LastTransformParameters << std::endl;
332 }
333 
334 /*
335  * Generate Data
336  */
337 template < typename TFixedImage, typename TMovingImage >
338 void
341 {
342  this->StartRegistration();
343 }
344 
345 
349 template < typename TFixedImage, typename TMovingImage >
352 ::GetOutput() const
353 {
354  return static_cast< const TransformOutputType * >( this->ProcessObject::GetOutput(0) );
355 }
356 
357 template < typename TFixedImage, typename TMovingImage >
360 ::MakeOutput(unsigned int output)
361 {
362  switch (output)
363  {
364  case 0:
365  return static_cast<DataObject*>(TransformOutputType::New().GetPointer());
366  break;
367  default:
368  itkExceptionMacro("MakeOutput request for an output number larger than the expected number of outputs");
369  return 0;
370  }
371 }
372 
373 
374 template < typename TFixedImage, typename TMovingImage >
375 void
377 ::SetFixedImage( const FixedImageType * fixedImage )
378 {
379  itkDebugMacro("setting Fixed Image to " << fixedImage );
380 
381  if (this->m_FixedImage.GetPointer() != fixedImage )
382  {
383  this->m_FixedImage = fixedImage;
384 
385  // Process object is not const-correct so the const_cast is required here
387  const_cast< FixedImageType *>( fixedImage ) );
388 
389  this->Modified();
390  }
391 }
392 
393 template < typename TFixedImage, typename TMovingImage >
394 void
396 ::SetMovingImage( const MovingImageType * movingImage )
397 {
398  itkDebugMacro("setting Moving Image to " << movingImage );
399 
400  if (this->m_MovingImage.GetPointer() != movingImage )
401  {
402  this->m_MovingImage = movingImage;
403 
404  // Process object is not const-correct so the const_cast is required here
406  const_cast< MovingImageType *>( movingImage ) );
407 
408  this->Modified();
409  }
410 }
411 
412 } // end namespace itk
413 
414 
415 #endif

Generated at Sat Feb 2 2013 23:44:29 for Orfeo Toolbox with doxygen 1.8.1.1