1 #ifndef _clitkVectorBSplineDecompositionImageFilter_txx
2 #define _clitkVectorBSplineDecompositionImageFilter_txx
3 #include "clitkVectorBSplineDecompositionImageFilter.h"
4 #include "itkImageRegionConstIteratorWithIndex.h"
5 #include "itkImageRegionIterator.h"
6 #include "itkProgressReporter.h"
15 template <class TInputImage, class TOutputImage>
16 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
17 ::VectorBSplineDecompositionImageFilter()
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 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
35 itk::Indent indent) const
37 Superclass::PrintSelf( os, indent );
38 os << indent << "Spline Order: " << m_SplineOrder << std::endl;
43 template <class TInputImage, class TOutputImage>
45 VectorBSplineDecompositionImageFilter<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 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
99 ::SetSplineOrder(unsigned int SplineOrder)
101 if (SplineOrder == m_SplineOrder)
105 m_SplineOrder = SplineOrder;
112 template <class TInputImage, class TOutputImage>
114 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
117 /* See Unser, 1997. Part II, Table I for Pole values */
118 // See also, Handbook of Medical Imaging, Processing and Analysis, Ed. Isaac N. Bankman,
120 switch (m_SplineOrder)
124 m_SplinePoles[0] = vcl_sqrt(3.0) - 2.0;
134 m_SplinePoles[0] = vcl_sqrt(8.0) - 3.0;
138 m_SplinePoles[0] = vcl_sqrt(664.0 - vcl_sqrt(438976.0)) + vcl_sqrt(304.0) - 19.0;
139 m_SplinePoles[1] = vcl_sqrt(664.0 + vcl_sqrt(438976.0)) - vcl_sqrt(304.0) - 19.0;
143 m_SplinePoles[0] = vcl_sqrt(135.0 / 2.0 - vcl_sqrt(17745.0 / 4.0)) + vcl_sqrt(105.0 / 4.0)
145 m_SplinePoles[1] = vcl_sqrt(135.0 / 2.0 + vcl_sqrt(17745.0 / 4.0)) - vcl_sqrt(105.0 / 4.0)
149 // SplineOrder not implemented yet.
150 itk::ExceptionObject err(__FILE__, __LINE__);
151 err.SetLocation( ITK_LOCATION);
152 err.SetDescription( "SplineOrder must be between 0 and 5. Requested spline order has not been implemented yet." );
159 template <class TInputImage, class TOutputImage>
161 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
162 ::SetInitialCausalCoefficient(double z)
164 /* begining InitialCausalCoefficient */
165 /* See Unser, 1999, Box 2 for explaination */
167 itk::Vector<double, VectorDimension> sum;
168 double zn, z2n, iz; //sum
169 unsigned long horizon;
171 /* this initialization corresponds to mirror boundaries */
172 horizon = m_DataLength[m_IteratorDirection];
174 if (m_Tolerance > 0.0)
176 horizon = (long)vcl_ceil(log(m_Tolerance) / vcl_log(fabs(z)));
178 if (horizon < m_DataLength[m_IteratorDirection])
180 /* accelerated loop */
181 sum = m_Scratch[0]; // verify this
182 for (unsigned int n = 1; n < horizon; n++)
184 sum += zn * m_Scratch[n];
192 z2n = vcl_pow(z, (double)(m_DataLength[m_IteratorDirection] - 1L));
193 sum = m_Scratch[0] + z2n * m_Scratch[m_DataLength[m_IteratorDirection] - 1L];
195 for (unsigned int n = 1; n <= (m_DataLength[m_IteratorDirection] - 2); n++)
197 sum += (zn + z2n) * m_Scratch[n];
201 m_Scratch[0] = sum / (1.0 - zn * zn);
206 template <class TInputImage, class TOutputImage>
208 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
209 ::SetInitialAntiCausalCoefficient(double z)
211 // this initialization corresponds to mirror boundaries
212 /* See Unser, 1999, Box 2 for explaination */
213 // Also see erratum at http://bigwww.epfl.ch/publications/unser9902.html
214 m_Scratch[m_DataLength[m_IteratorDirection] - 1] =
215 (z / (z * z - 1.0)) *
216 (z * m_Scratch[m_DataLength[m_IteratorDirection] - 2] + m_Scratch[m_DataLength[m_IteratorDirection] - 1]);
220 template <class TInputImage, class TOutputImage>
222 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
223 ::DataToCoefficientsND()
225 OutputImagePointer output = this->GetOutput();
227 itk::Size<ImageDimension> size = output->GetBufferedRegion().GetSize();
229 unsigned int count = output->GetBufferedRegion().GetNumberOfPixels() / size[0] * ImageDimension;
231 itk::ProgressReporter progress(this, 0, count, 10);
233 // Initialize coeffient array
234 this->CopyImageToImage(); // Coefficients are initialized to the input data
236 for (unsigned int n=0; n < ImageDimension; n++)
238 m_IteratorDirection = n;
239 // Loop through each dimension
241 // Initialize iterators
242 OutputLinearIterator CIterator( output, output->GetBufferedRegion() );
243 CIterator.SetDirection( m_IteratorDirection );
244 // For each data vector
245 while ( !CIterator.IsAtEnd() )
247 // Copy coefficients to scratch
248 this->CopyCoefficientsToScratch( CIterator );
251 // Perform 1D BSpline calculations
252 this->DataToCoefficients1D();
254 // Copy scratch back to coefficients.
255 // Brings us back to the end of the line we were working on.
256 CIterator.GoToBeginOfLine();
257 this->CopyScratchToCoefficients( CIterator ); // m_Scratch = m_Image;
258 CIterator.NextLine();
259 progress.CompletedPixel();
266 * Copy the input image into the output image
268 template <class TInputImage, class TOutputImage>
270 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
274 typedef itk::ImageRegionConstIteratorWithIndex< TInputImage > InputIterator;
275 typedef itk::ImageRegionIterator< TOutputImage > OutputIterator;
276 typedef typename TOutputImage::PixelType OutputPixelType;
278 InputIterator inIt( this->GetInput(), this->GetInput()->GetBufferedRegion() );
279 OutputIterator outIt( this->GetOutput(), this->GetOutput()->GetBufferedRegion() );
282 outIt = outIt.Begin();
284 while ( !outIt.IsAtEnd() )
286 for (unsigned int i=0; i< VectorDimension;i++)
288 v[i]= static_cast<typename OutputPixelType::ComponentType>( inIt.Get()[i] );
299 * Copy the scratch to one line of the output image
301 template <class TInputImage, class TOutputImage>
303 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
304 ::CopyScratchToCoefficients(OutputLinearIterator & Iter)
306 typedef typename TOutputImage::PixelType OutputPixelType;
309 while ( !Iter.IsAtEndOfLine() )
311 for(unsigned int i=0; i<VectorDimension; i++) v[i]=static_cast<typename OutputPixelType::ComponentType>( m_Scratch[j][i]);
321 * Copy one line of the output image to the scratch
323 template <class TInputImage, class TOutputImage>
325 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
326 ::CopyCoefficientsToScratch(OutputLinearIterator & Iter)
329 itk::Vector<double, VectorDimension> v;
330 while ( !Iter.IsAtEndOfLine() )
332 for(unsigned int i=0; i<VectorDimension; i++)v[i]=static_cast<double>( Iter.Get()[i] );
341 * GenerateInputRequestedRegion method.
343 template <class TInputImage, class TOutputImage>
345 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
346 ::GenerateInputRequestedRegion()
348 // this filter requires the all of the input image to be in
350 InputImagePointer inputPtr = const_cast< TInputImage * > ( this->GetInput() );
353 inputPtr->SetRequestedRegionToLargestPossibleRegion();
359 * EnlargeOutputRequestedRegion method.
361 template <class TInputImage, class TOutputImage>
363 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
364 ::EnlargeOutputRequestedRegion( itk::DataObject *output )
367 // this filter requires the all of the output image to be in
369 TOutputImage *imgData;
370 imgData = dynamic_cast<TOutputImage*>( output );
373 imgData->SetRequestedRegionToLargestPossibleRegion();
381 template <class TInputImage, class TOutputImage>
383 VectorBSplineDecompositionImageFilter<TInputImage, TOutputImage>
386 DD("VectorBSplineDecompositionImageFilter GenerateData()");
387 // Allocate scratch memory
388 InputImageConstPointer inputPtr = this->GetInput();
389 m_DataLength = inputPtr->GetBufferedRegion().GetSize();
391 unsigned long maxLength = 0;
392 for ( unsigned int n = 0; n < ImageDimension; n++ )
394 if ( m_DataLength[n] > maxLength )
396 maxLength = m_DataLength[n];
399 m_Scratch.resize( maxLength );
401 // Allocate memory for output image
402 OutputImagePointer outputPtr = this->GetOutput();
403 outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
404 outputPtr->Allocate();
406 // Calculate actual output
407 this->DataToCoefficientsND();