Orfeo Toolbox  3.16
itkInverseDeformationFieldImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: itkInverseDeformationFieldImageFilter.txx
5  Language: C++
6  Date: $Date$
7  Version: $Revision$
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 
18 #ifndef __itkInverseDeformationFieldImageFilter_txx
19 #define __itkInverseDeformationFieldImageFilter_txx
20 
22 #include "itkObjectFactory.h"
23 #include "itkProgressReporter.h"
27 
28 namespace itk
29 {
30 
34 template <class TInputImage, class TOutputImage>
37 {
38  m_OutputSpacing.Fill(1.0);
39  m_OutputOrigin.Fill(0.0);
40  for (unsigned int i = 0; i < ImageDimension; i++)
41  {
42  m_Size[i] = 0;
43  }
44 
46  double,
47  itkGetStaticConstMacro( ImageDimension ) > DefaultTransformType;
48 
49  m_KernelTransform = DefaultTransformType::New();
50 
51  m_SubsamplingFactor = 16;
52 }
53 
54 
60 template <class TInputImage, class TOutputImage>
61 void
63 ::PrintSelf(std::ostream& os, Indent indent) const
64 {
65  Superclass::PrintSelf(os,indent);
66 
67  os << indent << "Size: " << m_Size << std::endl;
68  os << indent << "OutputSpacing: " << m_OutputSpacing << std::endl;
69  os << indent << "OutputOrigin: " << m_OutputOrigin << std::endl;
70  os << indent << "KernelTransform: " << m_KernelTransform.GetPointer() << std::endl;
71  os << indent << "SubsamplingFactor: " << m_SubsamplingFactor << std::endl;
72 
73  return;
74 }
75 
79 template <class TInputImage, class TOutputImage>
80 void
82 ::SetOutputSpacing(const double* spacing)
83 {
84  SpacingType s(spacing);
85  this->SetOutputSpacing( s );
86 }
87 
91 template <class TInputImage, class TOutputImage>
92 void
94 ::SetOutputOrigin(const double* origin)
95 {
96  OriginPointType p(origin);
97  this->SetOutputOrigin( p );
98 }
99 
104 template <class TInputImage, class TOutputImage>
105 void
108 {
109 
110  typedef typename KernelTransformType::PointsContainer LandmarkContainer;
111  typedef typename LandmarkContainer::Pointer LandmarkContainerPointer;
112 
113  // Source contains points with physical coordinates of the
114  // destination displacement fields (the inverse field)
115  LandmarkContainerPointer source = LandmarkContainer::New();
116 
117  // Target contains vectors (stored as points) indicating
118  // displacement in the inverse direction.
119  LandmarkContainerPointer target = LandmarkContainer::New();
120 
121 
124  InputImageType > ResamplerType;
125 
126  typename ResamplerType::Pointer resampler = ResamplerType::New();
127 
128  const InputImageType * inputImage = this->GetInput();
129 
130  resampler->SetInput( inputImage );
131  resampler->SetOutputOrigin( inputImage->GetOrigin() );
132 
133  typename InputImageType::SpacingType spacing = inputImage->GetSpacing();
134 
135 
136  typedef typename InputImageType::RegionType InputRegionType;
137  typedef typename InputImageType::SizeType InputSizeType;
138  typedef typename InputImageType::IndexType InputIndexType;
139 
140  InputRegionType region;
141 
142  region = inputImage->GetLargestPossibleRegion();
143 
144  InputSizeType size = region.GetSize();
145 
146  for(unsigned int i=0; i < ImageDimension; i++)
147  {
148  size[i] = static_cast< typename InputSizeType::SizeValueType >( size[i] / m_SubsamplingFactor );
149  spacing[i] *= m_SubsamplingFactor;
150  }
151 
152  InputRegionType subsampledRegion;
153  subsampledRegion.SetSize( size );
154  subsampledRegion.SetIndex( region.GetIndex() );
155 
156  resampler->SetSize( size );
157  resampler->SetOutputStartIndex( subsampledRegion.GetIndex() );
158  resampler->SetOutputSpacing( spacing );
159 
160  resampler->Update();
161 
162 
163  // allocate a landmark pair for each
164  // pixel in the subsampled field
165  const unsigned long numberOfLandmarks = subsampledRegion.GetNumberOfPixels();
166  source->Reserve( numberOfLandmarks );
167  target->Reserve( numberOfLandmarks );
168 
169 
170  const InputImageType * sampledInput = resampler->GetOutput();
171 
173 
174  unsigned int landmarkId = 0;
175 
176  IteratorType ot( sampledInput, subsampledRegion );
177  ot.GoToBegin();
178 
179  OutputPixelType value;
180  Point<double, ImageDimension> sourcePoint;
181  Point<double, ImageDimension> targetPoint;
182 
183  while( !ot.IsAtEnd() )
184  {
185  value = ot.Get();
186  sampledInput->TransformIndexToPhysicalPoint( ot.GetIndex(), sourcePoint );
187 
188  source->InsertElement( landmarkId, sourcePoint );
189 
190  for(unsigned int i=0; i < ImageDimension; i++)
191  {
192  targetPoint[i] = -value[i];
193  }
194  target->InsertElement( landmarkId, targetPoint ); // revert direction of displacement
195 
196  ++landmarkId;
197  ++ot;
198  }
199 
200  itkDebugMacro( << "Number of Landmarks created = " << numberOfLandmarks );
201 
202  m_KernelTransform->GetTargetLandmarks()->SetPoints( target );
203  m_KernelTransform->GetSourceLandmarks()->SetPoints( source );
204 
205  itkDebugMacro( << "Before ComputeWMatrix() ");
206 
207  m_KernelTransform->ComputeWMatrix();
208 
209  itkDebugMacro( << "After ComputeWMatrix() ");
210 
211 }
212 
216 template <class TInputImage, class TOutputImage>
217 void
220 {
221 
222  // First subsample the input deformation field in order to create
223  // the KernelBased spline.
224  this->PrepareKernelBaseSpline();
225 
226  itkDebugMacro(<<"Actually executing");
227 
228  // Get the output pointers
229  OutputImageType * outputPtr = this->GetOutput();
230 
231  outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
232  outputPtr->Allocate();
233 
234  // Create an iterator that will walk the output region for this thread.
236  TOutputImage> OutputIterator;
237 
238  OutputImageRegionType region = outputPtr->GetRequestedRegion();
239 
240  OutputIterator outIt( outputPtr, region );
241 
242  // Define a few indices that will be used to translate from an input pixel
243  // to an output pixel
244  IndexType outputIndex; // Index to current output pixel
245 
246  typedef typename KernelTransformType::InputPointType InputPointType;
247  typedef typename KernelTransformType::OutputPointType OutputPointType;
248 
249  InputPointType outputPoint; // Coordinates of current output pixel
250 
251  // Support for progress methods/callbacks
252  ProgressReporter progress(this, 0, region.GetNumberOfPixels(), 10);
253 
254  outIt.GoToBegin();
255 
256  // Walk the output region
257  while ( !outIt.IsAtEnd() )
258  {
259  // Determine the index of the current output pixel
260  outputIndex = outIt.GetIndex();
261  outputPtr->TransformIndexToPhysicalPoint( outputIndex, outputPoint );
262 
263 
264  // Compute corresponding inverse displacement vector
265  OutputPointType interpolation =
266  m_KernelTransform->TransformPoint( outputPoint );
267 
268  OutputPixelType inverseDisplacement;
269 
270  for(unsigned int i=0; i < ImageDimension; i++)
271  {
272  inverseDisplacement[i] = interpolation[i];
273  }
274 
275  outIt.Set( inverseDisplacement ); // set inverse displacement.
276  ++outIt;
277  progress.CompletedPixel();
278  }
279 
280  return;
281 }
282 
290 template <class TInputImage, class TOutputImage>
291 void
294 {
295  // call the superclass's implementation of this method
296  Superclass::GenerateInputRequestedRegion();
297 
298  if ( !this->GetInput() )
299  {
300  return;
301  }
302 
303  // get pointers to the input and output
304  InputImagePointer inputPtr =
305  const_cast< InputImageType *>( this->GetInput() );
306 
307  // Request the entire input image
308  InputImageRegionType inputRegion;
309  inputRegion = inputPtr->GetLargestPossibleRegion();
310  inputPtr->SetRequestedRegion(inputRegion);
311 
312  return;
313 }
314 
318 template <class TInputImage, class TOutputImage>
319 void
322 {
323  // call the superclass' implementation of this method
324  Superclass::GenerateOutputInformation();
325 
326  // get pointers to the input and output
327  OutputImagePointer outputPtr = this->GetOutput();
328  if ( !outputPtr )
329  {
330  return;
331  }
332 
333  // Set the size of the output region
334  typename TOutputImage::RegionType outputLargestPossibleRegion;
335  outputLargestPossibleRegion.SetSize( m_Size );
336  outputPtr->SetLargestPossibleRegion( outputLargestPossibleRegion );
337 
338  // Set spacing and origin
339  outputPtr->SetSpacing( m_OutputSpacing );
340  outputPtr->SetOrigin( m_OutputOrigin );
341 
342  return;
343 }
344 
348 template <class TInputImage, class TOutputImage>
349 unsigned long
351 ::GetMTime( void ) const
352 {
353  unsigned long latestTime = Object::GetMTime();
354 
355  if( m_KernelTransform )
356  {
357  if( latestTime < m_KernelTransform->GetMTime() )
358  {
359  latestTime = m_KernelTransform->GetMTime();
360  }
361  }
362 
363  return latestTime;
364 }
365 
366 } // end namespace itk
367 
368 #endif

Generated at Sat May 18 2013 23:47:24 for Orfeo Toolbox with doxygen 1.8.3.1