1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #ifndef _itkBSplineDecompositionImageFilterWithOBD_txx
19 #define _itkBSplineDecompositionImageFilterWithOBD_txx
20 #include "itkBSplineDecompositionImageFilterWithOBD.h"
21 #include "itkImageRegionConstIteratorWithIndex.h"
22 #include "itkImageRegionIterator.h"
23 #include "itkProgressReporter.h"
24 #include "itkVector.h"
32 template <class TInputImage, class TOutputImage>
33 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
34 ::BSplineDecompositionImageFilterWithOBD()
38 m_Tolerance = 1e-10; // Need some guidance on this one...what is reasonable?
39 m_IteratorDirection = 0;
40 this->SetSplineOrder(SplineOrder);
45 * Standard "PrintSelf" method
47 template <class TInputImage, class TOutputImage>
49 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
54 Superclass::PrintSelf( os, indent );
55 os << indent << "Spline Order: " << m_SplineOrder << std::endl;
60 template <class TInputImage, class TOutputImage>
62 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
63 ::DataToCoefficients1D()
66 // See Unser, 1993, Part II, Equation 2.5,
67 // or Unser, 1999, Box 2. for an explaination.
71 if (m_DataLength[m_IteratorDirection] == 1) //Required by mirror boundaries
76 // Compute overall gain
77 for (int k = 0; k < m_NumberOfPoles; k++)
79 // Note for cubic splines lambda = 6
80 c0 = c0 * (1.0 - m_SplinePoles[k]) * (1.0 - 1.0 / m_SplinePoles[k]);
84 for (unsigned int n = 0; n < m_DataLength[m_IteratorDirection]; n++)
89 // loop over all poles
90 for (int k = 0; k < m_NumberOfPoles; k++)
92 // causal initialization
93 this->SetInitialCausalCoefficient(m_SplinePoles[k]);
95 for (unsigned int n = 1; n < m_DataLength[m_IteratorDirection]; n++)
97 m_Scratch[n] += m_SplinePoles[k] * m_Scratch[n - 1];
100 // anticausal initialization
101 this->SetInitialAntiCausalCoefficient(m_SplinePoles[k]);
102 // anticausal recursion
103 for ( int n = m_DataLength[m_IteratorDirection] - 2; 0 <= n; n--)
105 m_Scratch[n] = m_SplinePoles[k] * (m_Scratch[n + 1] - m_Scratch[n]);
113 template <class TInputImage, class TOutputImage>
115 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
116 ::SetSplineOrder(unsigned int SplineOrder)
118 if (SplineOrder == m_SplineOrder)
122 m_SplineOrder = SplineOrder;
129 template <class TInputImage, class TOutputImage>
131 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
132 ::SetSplineOrders(SizeType SplineOrders)
134 m_SplineOrders=SplineOrders;
137 template <class TInputImage, class TOutputImage>
139 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
142 /* See Unser, 1997. Part II, Table I for Pole values */
143 // See also, Handbook of Medical Imaging, Processing and Analysis, Ed. Isaac N. Bankman,
145 switch (m_SplineOrder)
150 m_SplinePoles[0] = vcl_sqrt(3.0) - 2.0;
160 m_SplinePoles[0] = vcl_sqrt(8.0) - 3.0;
164 m_SplinePoles[0] = vcl_sqrt(664.0 - vcl_sqrt(438976.0)) + vcl_sqrt(304.0) - 19.0;
165 m_SplinePoles[1] = vcl_sqrt(664.0 + vcl_sqrt(438976.0)) - vcl_sqrt(304.0) - 19.0;
169 m_SplinePoles[0] = vcl_sqrt(135.0 / 2.0 - vcl_sqrt(17745.0 / 4.0)) + vcl_sqrt(105.0 / 4.0)
171 m_SplinePoles[1] = vcl_sqrt(135.0 / 2.0 + vcl_sqrt(17745.0 / 4.0)) - vcl_sqrt(105.0 / 4.0)
175 // SplineOrder not implemented yet.
176 ExceptionObject err(__FILE__, __LINE__);
177 err.SetLocation( ITK_LOCATION);
178 err.SetDescription( "SplineOrder must be between 0 and 5. Requested spline order has not been implemented yet." );
185 template <class TInputImage, class TOutputImage>
187 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
188 ::SetInitialCausalCoefficient(double z)
190 /* begining InitialCausalCoefficient */
191 /* See Unser, 1999, Box 2 for explaination */
193 double sum, zn, z2n, iz;
194 unsigned long horizon;
196 /* this initialization corresponds to mirror boundaries */
197 horizon = m_DataLength[m_IteratorDirection];
199 if (m_Tolerance > 0.0)
201 horizon = (long)vcl_ceil(log(m_Tolerance) / vcl_log(fabs(z)));
203 if (horizon < m_DataLength[m_IteratorDirection])
205 /* accelerated loop */
206 sum = m_Scratch[0]; // verify this
207 for (unsigned int n = 1; n < horizon; n++)
209 sum += zn * m_Scratch[n];
217 z2n = vcl_pow(z, (double)(m_DataLength[m_IteratorDirection] - 1L));
218 sum = m_Scratch[0] + z2n * m_Scratch[m_DataLength[m_IteratorDirection] - 1L];
220 for (unsigned int n = 1; n <= (m_DataLength[m_IteratorDirection] - 2); n++)
222 sum += (zn + z2n) * m_Scratch[n];
226 m_Scratch[0] = sum / (1.0 - zn * zn);
231 template <class TInputImage, class TOutputImage>
233 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
234 ::SetInitialAntiCausalCoefficient(double z)
236 // this initialization corresponds to mirror boundaries
237 /* See Unser, 1999, Box 2 for explaination */
238 // Also see erratum at http://bigwww.epfl.ch/publications/unser9902.html
239 m_Scratch[m_DataLength[m_IteratorDirection] - 1] =
240 (z / (z * z - 1.0)) *
241 (z * m_Scratch[m_DataLength[m_IteratorDirection] - 2] + m_Scratch[m_DataLength[m_IteratorDirection] - 1]);
245 template <class TInputImage, class TOutputImage>
247 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
248 ::DataToCoefficientsND()
250 OutputImagePointer output = this->GetOutput();
252 Size<ImageDimension> size = output->GetBufferedRegion().GetSize();
254 unsigned int count = output->GetBufferedRegion().GetNumberOfPixels() / size[0] * ImageDimension;
256 ProgressReporter progress(this, 0, count, 10);
258 // Initialize coeffient array
259 this->CopyImageToImage(); // Coefficients are initialized to the input data
261 for (unsigned int n=0; n < ImageDimension; n++)
263 m_IteratorDirection = n;
264 // Loop through each dimension
266 //JV Set the correct order by dimension!
267 SetSplineOrder(m_SplineOrders[n]);
269 // Initialize iterators
270 OutputLinearIterator CIterator( output, output->GetBufferedRegion() );
271 CIterator.SetDirection( m_IteratorDirection );
272 // For each data vector
273 while ( !CIterator.IsAtEnd() )
275 // Copy coefficients to scratch
276 this->CopyCoefficientsToScratch( CIterator );
278 // Perform 1D BSpline calculations
279 this->DataToCoefficients1D();
281 // Copy scratch back to coefficients.
282 // Brings us back to the end of the line we were working on.
283 CIterator.GoToBeginOfLine();
284 this->CopyScratchToCoefficients( CIterator ); // m_Scratch = m_Image;
285 CIterator.NextLine();
286 progress.CompletedPixel();
293 * Copy the input image into the output image
295 template <class TInputImage, class TOutputImage>
297 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
301 typedef ImageRegionConstIteratorWithIndex< TInputImage > InputIterator;
302 typedef ImageRegionIterator< TOutputImage > OutputIterator;
303 typedef typename TOutputImage::PixelType OutputPixelType;
305 InputIterator inIt( this->GetInput(), this->GetInput()->GetBufferedRegion() );
306 OutputIterator outIt( this->GetOutput(), this->GetOutput()->GetBufferedRegion() );
309 outIt = outIt.Begin();
311 while ( !outIt.IsAtEnd() )
313 outIt.Set( static_cast<OutputPixelType>( inIt.Get() ) );
322 * Copy the scratch to one line of the output image
324 template <class TInputImage, class TOutputImage>
326 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
327 ::CopyScratchToCoefficients(OutputLinearIterator & Iter)
329 typedef typename TOutputImage::PixelType OutputPixelType;
331 while ( !Iter.IsAtEndOfLine() )
333 Iter.Set( static_cast<OutputPixelType>( m_Scratch[j] ) );
342 * Copy one line of the output image to the scratch
344 template <class TInputImage, class TOutputImage>
346 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
347 ::CopyCoefficientsToScratch(OutputLinearIterator & Iter)
350 while ( !Iter.IsAtEndOfLine() )
352 m_Scratch[j] = static_cast<double>( Iter.Get() ) ;
360 * GenerateInputRequestedRegion method.
362 template <class TInputImage, class TOutputImage>
364 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
365 ::GenerateInputRequestedRegion()
367 // this filter requires the all of the input image to be in
369 InputImagePointer inputPtr = const_cast< TInputImage * > ( this->GetInput() );
372 inputPtr->SetRequestedRegionToLargestPossibleRegion();
378 * EnlargeOutputRequestedRegion method.
380 template <class TInputImage, class TOutputImage>
382 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
383 ::EnlargeOutputRequestedRegion(
387 // this filter requires the all of the output image to be in
389 TOutputImage *imgData;
390 imgData = dynamic_cast<TOutputImage*>( output );
393 imgData->SetRequestedRegionToLargestPossibleRegion();
401 template <class TInputImage, class TOutputImage>
403 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
407 // Allocate scratch memory
408 InputImageConstPointer inputPtr = this->GetInput();
409 m_DataLength = inputPtr->GetBufferedRegion().GetSize();
411 unsigned long maxLength = 0;
412 for ( unsigned int n = 0; n < ImageDimension; n++ )
414 if ( m_DataLength[n] > maxLength )
416 maxLength = m_DataLength[n];
419 m_Scratch.resize( maxLength );
421 // Allocate memory for output image
422 OutputImagePointer outputPtr = this->GetOutput();
423 outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
424 outputPtr->Allocate();
426 // Calculate actual output
427 this->DataToCoefficientsND();