1 #ifndef _itkBSplineDecompositionImageFilterWithOBD_txx
2 #define _itkBSplineDecompositionImageFilterWithOBD_txx
4 #include "itkBSplineDecompositionImageFilterWithOBD.h"
5 #include "itkImageRegionConstIteratorWithIndex.h"
6 #include "itkImageRegionIterator.h"
7 #include "itkProgressReporter.h"
16 template <class TInputImage, class TOutputImage>
17 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
18 ::BSplineDecompositionImageFilterWithOBD()
22 m_Tolerance = 1e-10; // Need some guidance on this one...what is reasonable?
23 m_IteratorDirection = 0;
24 this->SetSplineOrder(SplineOrder);
29 * Standard "PrintSelf" method
31 template <class TInputImage, class TOutputImage>
33 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
38 Superclass::PrintSelf( os, indent );
39 os << indent << "Spline Order: " << m_SplineOrder << std::endl;
44 template <class TInputImage, class TOutputImage>
46 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
47 ::DataToCoefficients1D()
50 // See Unser, 1993, Part II, Equation 2.5,
51 // or Unser, 1999, Box 2. for an explaination.
55 if (m_DataLength[m_IteratorDirection] == 1) //Required by mirror boundaries
60 // Compute overall gain
61 for (int k = 0; k < m_NumberOfPoles; k++)
63 // Note for cubic splines lambda = 6
64 c0 = c0 * (1.0 - m_SplinePoles[k]) * (1.0 - 1.0 / m_SplinePoles[k]);
68 for (unsigned int n = 0; n < m_DataLength[m_IteratorDirection]; n++)
73 // loop over all poles
74 for (int k = 0; k < m_NumberOfPoles; k++)
76 // causal initialization
77 this->SetInitialCausalCoefficient(m_SplinePoles[k]);
79 for (unsigned int n = 1; n < m_DataLength[m_IteratorDirection]; n++)
81 m_Scratch[n] += m_SplinePoles[k] * m_Scratch[n - 1];
84 // anticausal initialization
85 this->SetInitialAntiCausalCoefficient(m_SplinePoles[k]);
86 // anticausal recursion
87 for ( int n = m_DataLength[m_IteratorDirection] - 2; 0 <= n; n--)
89 m_Scratch[n] = m_SplinePoles[k] * (m_Scratch[n + 1] - m_Scratch[n]);
97 template <class TInputImage, class TOutputImage>
99 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
100 ::SetSplineOrder(unsigned int SplineOrder)
102 if (SplineOrder == m_SplineOrder)
106 m_SplineOrder = SplineOrder;
113 template <class TInputImage, class TOutputImage>
115 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
116 ::SetSplineOrders(SizeType SplineOrders)
118 m_SplineOrders=SplineOrders;
121 template <class TInputImage, class TOutputImage>
123 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
126 /* See Unser, 1997. Part II, Table I for Pole values */
127 // See also, Handbook of Medical Imaging, Processing and Analysis, Ed. Isaac N. Bankman,
129 switch (m_SplineOrder)
134 m_SplinePoles[0] = vcl_sqrt(3.0) - 2.0;
144 m_SplinePoles[0] = vcl_sqrt(8.0) - 3.0;
148 m_SplinePoles[0] = vcl_sqrt(664.0 - vcl_sqrt(438976.0)) + vcl_sqrt(304.0) - 19.0;
149 m_SplinePoles[1] = vcl_sqrt(664.0 + vcl_sqrt(438976.0)) - vcl_sqrt(304.0) - 19.0;
153 m_SplinePoles[0] = vcl_sqrt(135.0 / 2.0 - vcl_sqrt(17745.0 / 4.0)) + vcl_sqrt(105.0 / 4.0)
155 m_SplinePoles[1] = vcl_sqrt(135.0 / 2.0 + vcl_sqrt(17745.0 / 4.0)) - vcl_sqrt(105.0 / 4.0)
159 // SplineOrder not implemented yet.
160 ExceptionObject err(__FILE__, __LINE__);
161 err.SetLocation( ITK_LOCATION);
162 err.SetDescription( "SplineOrder must be between 0 and 5. Requested spline order has not been implemented yet." );
169 template <class TInputImage, class TOutputImage>
171 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
172 ::SetInitialCausalCoefficient(double z)
174 /* begining InitialCausalCoefficient */
175 /* See Unser, 1999, Box 2 for explaination */
177 double sum, zn, z2n, iz;
178 unsigned long horizon;
180 /* this initialization corresponds to mirror boundaries */
181 horizon = m_DataLength[m_IteratorDirection];
183 if (m_Tolerance > 0.0)
185 horizon = (long)vcl_ceil(log(m_Tolerance) / vcl_log(fabs(z)));
187 if (horizon < m_DataLength[m_IteratorDirection])
189 /* accelerated loop */
190 sum = m_Scratch[0]; // verify this
191 for (unsigned int n = 1; n < horizon; n++)
193 sum += zn * m_Scratch[n];
201 z2n = vcl_pow(z, (double)(m_DataLength[m_IteratorDirection] - 1L));
202 sum = m_Scratch[0] + z2n * m_Scratch[m_DataLength[m_IteratorDirection] - 1L];
204 for (unsigned int n = 1; n <= (m_DataLength[m_IteratorDirection] - 2); n++)
206 sum += (zn + z2n) * m_Scratch[n];
210 m_Scratch[0] = sum / (1.0 - zn * zn);
215 template <class TInputImage, class TOutputImage>
217 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
218 ::SetInitialAntiCausalCoefficient(double z)
220 // this initialization corresponds to mirror boundaries
221 /* See Unser, 1999, Box 2 for explaination */
222 // Also see erratum at http://bigwww.epfl.ch/publications/unser9902.html
223 m_Scratch[m_DataLength[m_IteratorDirection] - 1] =
224 (z / (z * z - 1.0)) *
225 (z * m_Scratch[m_DataLength[m_IteratorDirection] - 2] + m_Scratch[m_DataLength[m_IteratorDirection] - 1]);
229 template <class TInputImage, class TOutputImage>
231 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
232 ::DataToCoefficientsND()
234 OutputImagePointer output = this->GetOutput();
236 Size<ImageDimension> size = output->GetBufferedRegion().GetSize();
238 unsigned int count = output->GetBufferedRegion().GetNumberOfPixels() / size[0] * ImageDimension;
240 ProgressReporter progress(this, 0, count, 10);
242 // Initialize coeffient array
243 this->CopyImageToImage(); // Coefficients are initialized to the input data
245 for (unsigned int n=0; n < ImageDimension; n++)
247 m_IteratorDirection = n;
248 // Loop through each dimension
250 //JV Set the correct order by dimension!
251 SetSplineOrder(m_SplineOrders[n]);
253 // Initialize iterators
254 OutputLinearIterator CIterator( output, output->GetBufferedRegion() );
255 CIterator.SetDirection( m_IteratorDirection );
256 // For each data vector
257 while ( !CIterator.IsAtEnd() )
259 // Copy coefficients to scratch
260 this->CopyCoefficientsToScratch( CIterator );
262 // Perform 1D BSpline calculations
263 this->DataToCoefficients1D();
265 // Copy scratch back to coefficients.
266 // Brings us back to the end of the line we were working on.
267 CIterator.GoToBeginOfLine();
268 this->CopyScratchToCoefficients( CIterator ); // m_Scratch = m_Image;
269 CIterator.NextLine();
270 progress.CompletedPixel();
277 * Copy the input image into the output image
279 template <class TInputImage, class TOutputImage>
281 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
285 typedef ImageRegionConstIteratorWithIndex< TInputImage > InputIterator;
286 typedef ImageRegionIterator< TOutputImage > OutputIterator;
287 typedef typename TOutputImage::PixelType OutputPixelType;
289 InputIterator inIt( this->GetInput(), this->GetInput()->GetBufferedRegion() );
290 OutputIterator outIt( this->GetOutput(), this->GetOutput()->GetBufferedRegion() );
293 outIt = outIt.Begin();
295 while ( !outIt.IsAtEnd() )
297 outIt.Set( static_cast<OutputPixelType>( inIt.Get() ) );
306 * Copy the scratch to one line of the output image
308 template <class TInputImage, class TOutputImage>
310 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
311 ::CopyScratchToCoefficients(OutputLinearIterator & Iter)
313 typedef typename TOutputImage::PixelType OutputPixelType;
315 while ( !Iter.IsAtEndOfLine() )
317 Iter.Set( static_cast<OutputPixelType>( m_Scratch[j] ) );
326 * Copy one line of the output image to the scratch
328 template <class TInputImage, class TOutputImage>
330 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
331 ::CopyCoefficientsToScratch(OutputLinearIterator & Iter)
334 while ( !Iter.IsAtEndOfLine() )
336 m_Scratch[j] = static_cast<double>( Iter.Get() ) ;
344 * GenerateInputRequestedRegion method.
346 template <class TInputImage, class TOutputImage>
348 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
349 ::GenerateInputRequestedRegion()
351 // this filter requires the all of the input image to be in
353 InputImagePointer inputPtr = const_cast< TInputImage * > ( this->GetInput() );
356 inputPtr->SetRequestedRegionToLargestPossibleRegion();
362 * EnlargeOutputRequestedRegion method.
364 template <class TInputImage, class TOutputImage>
366 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
367 ::EnlargeOutputRequestedRegion(
371 // this filter requires the all of the output image to be in
373 TOutputImage *imgData;
374 imgData = dynamic_cast<TOutputImage*>( output );
377 imgData->SetRequestedRegionToLargestPossibleRegion();
385 template <class TInputImage, class TOutputImage>
387 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
391 // Allocate scratch memory
392 InputImageConstPointer inputPtr = this->GetInput();
393 m_DataLength = inputPtr->GetBufferedRegion().GetSize();
395 unsigned long maxLength = 0;
396 for ( unsigned int n = 0; n < ImageDimension; n++ )
398 if ( m_DataLength[n] > maxLength )
400 maxLength = m_DataLength[n];
403 m_Scratch.resize( maxLength );
405 // Allocate memory for output image
406 OutputImagePointer outputPtr = this->GetOutput();
407 outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
408 outputPtr->Allocate();
410 // Calculate actual output
411 this->DataToCoefficientsND();