1 #ifndef _clitkVectorBSplineDecompositionImageFilterWithOBD_txx
2 #define _clitkVectorBSplineDecompositionImageFilterWithOBD_txx
4 #include "clitkVectorBSplineDecompositionImageFilterWithOBD.h"
5 #include "itkImageRegionConstIteratorWithIndex.h"
6 #include "itkImageRegionIterator.h"
7 #include "itkProgressReporter.h"
16 template <class TInputImage, class TOutputImage>
17 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
18 ::VectorBSplineDecompositionImageFilterWithOBD()
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 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
36 itk::Indent indent) const
38 Superclass::PrintSelf( os, indent );
39 os << indent << "Spline Order: " << m_SplineOrder << std::endl;
44 template <class TInputImage, class TOutputImage>
46 VectorBSplineDecompositionImageFilterWithOBD<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 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
100 ::SetSplineOrder(unsigned int SplineOrder)
102 if (SplineOrder == m_SplineOrder)
106 m_SplineOrder = SplineOrder;
113 template <class TInputImage, class TOutputImage>
115 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
116 ::SetSplineOrders(SizeType SplineOrders)
118 m_SplineOrders=SplineOrders;
121 template <class TInputImage, class TOutputImage>
123 VectorBSplineDecompositionImageFilterWithOBD<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)
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 itk::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 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
171 ::SetInitialCausalCoefficient(double z)
173 /* begining InitialCausalCoefficient */
174 /* See Unser, 1999, Box 2 for explaination */
176 itk::Vector<double, VectorDimension> sum;
177 double zn, z2n, iz; //sum
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 VectorBSplineDecompositionImageFilterWithOBD<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 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
232 ::DataToCoefficientsND()
234 OutputImagePointer output = this->GetOutput();
236 itk::Size<ImageDimension> size = output->GetBufferedRegion().GetSize();
238 unsigned int count = output->GetBufferedRegion().GetNumberOfPixels() / size[0] * ImageDimension;
240 itk::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 );
263 // Perform 1D BSpline calculations
264 this->DataToCoefficients1D();
266 // Copy scratch back to coefficients.
267 // Brings us back to the end of the line we were working on.
268 CIterator.GoToBeginOfLine();
269 this->CopyScratchToCoefficients( CIterator ); // m_Scratch = m_Image;
270 CIterator.NextLine();
271 progress.CompletedPixel();
278 * Copy the input image into the output image
280 template <class TInputImage, class TOutputImage>
282 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
286 typedef itk::ImageRegionConstIteratorWithIndex< TInputImage > InputIterator;
287 typedef itk::ImageRegionIterator< TOutputImage > OutputIterator;
288 typedef typename TOutputImage::PixelType OutputPixelType;
290 InputIterator inIt( this->GetInput(), this->GetInput()->GetBufferedRegion() );
291 OutputIterator outIt( this->GetOutput(), this->GetOutput()->GetBufferedRegion() );
294 outIt = outIt.Begin();
296 while ( !outIt.IsAtEnd() )
298 for (unsigned int i=0; i< VectorDimension;i++)
300 v[i]= static_cast<typename OutputPixelType::ComponentType>( inIt.Get()[i] );
311 * Copy the scratch to one line of the output image
313 template <class TInputImage, class TOutputImage>
315 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
316 ::CopyScratchToCoefficients(OutputLinearIterator & Iter)
318 typedef typename TOutputImage::PixelType OutputPixelType;
321 while ( !Iter.IsAtEndOfLine() )
323 for(unsigned int i=0; i<VectorDimension; i++) v[i]=static_cast<typename OutputPixelType::ComponentType>( m_Scratch[j][i]);
333 * Copy one line of the output image to the scratch
335 template <class TInputImage, class TOutputImage>
337 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
338 ::CopyCoefficientsToScratch(OutputLinearIterator & Iter)
341 itk::Vector<double, VectorDimension> v;
342 while ( !Iter.IsAtEndOfLine() )
344 for(unsigned int i=0; i<VectorDimension; i++)v[i]=static_cast<double>( Iter.Get()[i] );
353 * GenerateInputRequestedRegion method.
355 template <class TInputImage, class TOutputImage>
357 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
358 ::GenerateInputRequestedRegion()
360 // this filter requires the all of the input image to be in
362 InputImagePointer inputPtr = const_cast< TInputImage * > ( this->GetInput() );
365 inputPtr->SetRequestedRegionToLargestPossibleRegion();
371 * EnlargeOutputRequestedRegion method.
373 template <class TInputImage, class TOutputImage>
375 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
376 ::EnlargeOutputRequestedRegion( itk::DataObject *output )
379 // this filter requires the all of the output image to be in
381 TOutputImage *imgData;
382 imgData = dynamic_cast<TOutputImage*>( output );
385 imgData->SetRequestedRegionToLargestPossibleRegion();
393 template <class TInputImage, class TOutputImage>
395 VectorBSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
398 DD("VectorBSplineDecompositionImageFilterWithOBD GenerateData()");
399 // Allocate scratch memory
400 InputImageConstPointer inputPtr = this->GetInput();
401 m_DataLength = inputPtr->GetBufferedRegion().GetSize();
403 unsigned long maxLength = 0;
404 for ( unsigned int n = 0; n < ImageDimension; n++ )
406 if ( m_DataLength[n] > maxLength )
408 maxLength = m_DataLength[n];
411 m_Scratch.resize( maxLength );
413 // Allocate memory for output image
414 OutputImagePointer outputPtr = this->GetOutput();
415 outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
416 outputPtr->Allocate();
418 // Calculate actual output
419 this->DataToCoefficientsND();