1 #ifndef _itkBSplineDecompositionImageFilterWithOBD_txx
2 #define _itkBSplineDecompositionImageFilterWithOBD_txx
3 #include "itkBSplineDecompositionImageFilterWithOBD.h"
4 #include "itkImageRegionConstIteratorWithIndex.h"
5 #include "itkImageRegionIterator.h"
6 #include "itkProgressReporter.h"
15 template <class TInputImage, class TOutputImage>
16 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
17 ::BSplineDecompositionImageFilterWithOBD()
21 m_Tolerance = 1e-10; // Need some guidance on this one...what is reasonable?
22 m_IteratorDirection = 0;
23 this->SetSplineOrder(SplineOrder);
28 * Standard "PrintSelf" method
30 template <class TInputImage, class TOutputImage>
32 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
37 Superclass::PrintSelf( os, indent );
38 os << indent << "Spline Order: " << m_SplineOrder << std::endl;
43 template <class TInputImage, class TOutputImage>
45 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
46 ::DataToCoefficients1D()
49 // See Unser, 1993, Part II, Equation 2.5,
50 // or Unser, 1999, Box 2. for an explaination.
54 if (m_DataLength[m_IteratorDirection] == 1) //Required by mirror boundaries
59 // Compute overall gain
60 for (int k = 0; k < m_NumberOfPoles; k++)
62 // Note for cubic splines lambda = 6
63 c0 = c0 * (1.0 - m_SplinePoles[k]) * (1.0 - 1.0 / m_SplinePoles[k]);
67 for (unsigned int n = 0; n < m_DataLength[m_IteratorDirection]; n++)
72 // loop over all poles
73 for (int k = 0; k < m_NumberOfPoles; k++)
75 // causal initialization
76 this->SetInitialCausalCoefficient(m_SplinePoles[k]);
78 for (unsigned int n = 1; n < m_DataLength[m_IteratorDirection]; n++)
80 m_Scratch[n] += m_SplinePoles[k] * m_Scratch[n - 1];
83 // anticausal initialization
84 this->SetInitialAntiCausalCoefficient(m_SplinePoles[k]);
85 // anticausal recursion
86 for ( int n = m_DataLength[m_IteratorDirection] - 2; 0 <= n; n--)
88 m_Scratch[n] = m_SplinePoles[k] * (m_Scratch[n + 1] - m_Scratch[n]);
96 template <class TInputImage, class TOutputImage>
98 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
99 ::SetSplineOrder(unsigned int SplineOrder)
101 if (SplineOrder == m_SplineOrder)
105 m_SplineOrder = SplineOrder;
112 template <class TInputImage, class TOutputImage>
114 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
115 ::SetSplineOrders(SizeType SplineOrders)
117 m_SplineOrders=SplineOrders;
120 template <class TInputImage, class TOutputImage>
122 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
125 /* See Unser, 1997. Part II, Table I for Pole values */
126 // See also, Handbook of Medical Imaging, Processing and Analysis, Ed. Isaac N. Bankman,
128 switch (m_SplineOrder)
133 m_SplinePoles[0] = vcl_sqrt(3.0) - 2.0;
143 m_SplinePoles[0] = vcl_sqrt(8.0) - 3.0;
147 m_SplinePoles[0] = vcl_sqrt(664.0 - vcl_sqrt(438976.0)) + vcl_sqrt(304.0) - 19.0;
148 m_SplinePoles[1] = vcl_sqrt(664.0 + vcl_sqrt(438976.0)) - vcl_sqrt(304.0) - 19.0;
152 m_SplinePoles[0] = vcl_sqrt(135.0 / 2.0 - vcl_sqrt(17745.0 / 4.0)) + vcl_sqrt(105.0 / 4.0)
154 m_SplinePoles[1] = vcl_sqrt(135.0 / 2.0 + vcl_sqrt(17745.0 / 4.0)) - vcl_sqrt(105.0 / 4.0)
158 // SplineOrder not implemented yet.
159 ExceptionObject err(__FILE__, __LINE__);
160 err.SetLocation( ITK_LOCATION);
161 err.SetDescription( "SplineOrder must be between 0 and 5. Requested spline order has not been implemented yet." );
168 template <class TInputImage, class TOutputImage>
170 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
171 ::SetInitialCausalCoefficient(double z)
173 /* begining InitialCausalCoefficient */
174 /* See Unser, 1999, Box 2 for explaination */
176 double sum, zn, z2n, iz;
177 unsigned long horizon;
179 /* this initialization corresponds to mirror boundaries */
180 horizon = m_DataLength[m_IteratorDirection];
182 if (m_Tolerance > 0.0)
184 horizon = (long)vcl_ceil(log(m_Tolerance) / vcl_log(fabs(z)));
186 if (horizon < m_DataLength[m_IteratorDirection])
188 /* accelerated loop */
189 sum = m_Scratch[0]; // verify this
190 for (unsigned int n = 1; n < horizon; n++)
192 sum += zn * m_Scratch[n];
200 z2n = vcl_pow(z, (double)(m_DataLength[m_IteratorDirection] - 1L));
201 sum = m_Scratch[0] + z2n * m_Scratch[m_DataLength[m_IteratorDirection] - 1L];
203 for (unsigned int n = 1; n <= (m_DataLength[m_IteratorDirection] - 2); n++)
205 sum += (zn + z2n) * m_Scratch[n];
209 m_Scratch[0] = sum / (1.0 - zn * zn);
214 template <class TInputImage, class TOutputImage>
216 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
217 ::SetInitialAntiCausalCoefficient(double z)
219 // this initialization corresponds to mirror boundaries
220 /* See Unser, 1999, Box 2 for explaination */
221 // Also see erratum at http://bigwww.epfl.ch/publications/unser9902.html
222 m_Scratch[m_DataLength[m_IteratorDirection] - 1] =
223 (z / (z * z - 1.0)) *
224 (z * m_Scratch[m_DataLength[m_IteratorDirection] - 2] + m_Scratch[m_DataLength[m_IteratorDirection] - 1]);
228 template <class TInputImage, class TOutputImage>
230 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
231 ::DataToCoefficientsND()
233 OutputImagePointer output = this->GetOutput();
235 Size<ImageDimension> size = output->GetBufferedRegion().GetSize();
237 unsigned int count = output->GetBufferedRegion().GetNumberOfPixels() / size[0] * ImageDimension;
239 ProgressReporter progress(this, 0, count, 10);
241 // Initialize coeffient array
242 this->CopyImageToImage(); // Coefficients are initialized to the input data
244 for (unsigned int n=0; n < ImageDimension; n++)
246 m_IteratorDirection = n;
247 // Loop through each dimension
249 //JV Set the correct order by dimension!
250 SetSplineOrder(m_SplineOrders[n]);
252 // Initialize iterators
253 OutputLinearIterator CIterator( output, output->GetBufferedRegion() );
254 CIterator.SetDirection( m_IteratorDirection );
255 // For each data vector
256 while ( !CIterator.IsAtEnd() )
258 // Copy coefficients to scratch
259 this->CopyCoefficientsToScratch( CIterator );
261 // Perform 1D BSpline calculations
262 this->DataToCoefficients1D();
264 // Copy scratch back to coefficients.
265 // Brings us back to the end of the line we were working on.
266 CIterator.GoToBeginOfLine();
267 this->CopyScratchToCoefficients( CIterator ); // m_Scratch = m_Image;
268 CIterator.NextLine();
269 progress.CompletedPixel();
276 * Copy the input image into the output image
278 template <class TInputImage, class TOutputImage>
280 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
284 typedef ImageRegionConstIteratorWithIndex< TInputImage > InputIterator;
285 typedef ImageRegionIterator< TOutputImage > OutputIterator;
286 typedef typename TOutputImage::PixelType OutputPixelType;
288 InputIterator inIt( this->GetInput(), this->GetInput()->GetBufferedRegion() );
289 OutputIterator outIt( this->GetOutput(), this->GetOutput()->GetBufferedRegion() );
292 outIt = outIt.Begin();
294 while ( !outIt.IsAtEnd() )
296 outIt.Set( static_cast<OutputPixelType>( inIt.Get() ) );
305 * Copy the scratch to one line of the output image
307 template <class TInputImage, class TOutputImage>
309 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
310 ::CopyScratchToCoefficients(OutputLinearIterator & Iter)
312 typedef typename TOutputImage::PixelType OutputPixelType;
314 while ( !Iter.IsAtEndOfLine() )
316 Iter.Set( static_cast<OutputPixelType>( m_Scratch[j] ) );
325 * Copy one line of the output image to the scratch
327 template <class TInputImage, class TOutputImage>
329 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
330 ::CopyCoefficientsToScratch(OutputLinearIterator & Iter)
333 while ( !Iter.IsAtEndOfLine() )
335 m_Scratch[j] = static_cast<double>( Iter.Get() ) ;
343 * GenerateInputRequestedRegion method.
345 template <class TInputImage, class TOutputImage>
347 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
348 ::GenerateInputRequestedRegion()
350 // this filter requires the all of the input image to be in
352 InputImagePointer inputPtr = const_cast< TInputImage * > ( this->GetInput() );
355 inputPtr->SetRequestedRegionToLargestPossibleRegion();
361 * EnlargeOutputRequestedRegion method.
363 template <class TInputImage, class TOutputImage>
365 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
366 ::EnlargeOutputRequestedRegion(
370 // this filter requires the all of the output image to be in
372 TOutputImage *imgData;
373 imgData = dynamic_cast<TOutputImage*>( output );
376 imgData->SetRequestedRegionToLargestPossibleRegion();
384 template <class TInputImage, class TOutputImage>
386 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
390 // Allocate scratch memory
391 InputImageConstPointer inputPtr = this->GetInput();
392 m_DataLength = inputPtr->GetBufferedRegion().GetSize();
394 unsigned long maxLength = 0;
395 for ( unsigned int n = 0; n < ImageDimension; n++ )
397 if ( m_DataLength[n] > maxLength )
399 maxLength = m_DataLength[n];
402 m_Scratch.resize( maxLength );
404 // Allocate memory for output image
405 OutputImagePointer outputPtr = this->GetOutput();
406 outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
407 outputPtr->Allocate();
409 // Calculate actual output
410 this->DataToCoefficientsND();