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
75 // Compute overall gain
76 for (int k = 0; k < m_NumberOfPoles; k++) {
77 // Note for cubic splines lambda = 6
78 c0 = c0 * (1.0 - m_SplinePoles[k]) * (1.0 - 1.0 / m_SplinePoles[k]);
82 for (unsigned int n = 0; n < m_DataLength[m_IteratorDirection]; n++) {
86 // loop over all poles
87 for (int k = 0; k < m_NumberOfPoles; k++) {
88 // causal initialization
89 this->SetInitialCausalCoefficient(m_SplinePoles[k]);
91 for (unsigned int n = 1; n < m_DataLength[m_IteratorDirection]; n++) {
92 m_Scratch[n] += m_SplinePoles[k] * m_Scratch[n - 1];
95 // anticausal initialization
96 this->SetInitialAntiCausalCoefficient(m_SplinePoles[k]);
97 // anticausal recursion
98 for ( int n = m_DataLength[m_IteratorDirection] - 2; 0 <= n; n--) {
99 m_Scratch[n] = m_SplinePoles[k] * (m_Scratch[n + 1] - m_Scratch[n]);
107 template <class TInputImage, class TOutputImage>
109 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
110 ::SetSplineOrder(unsigned int SplineOrder)
112 if (SplineOrder == m_SplineOrder) {
115 m_SplineOrder = SplineOrder;
122 template <class TInputImage, class TOutputImage>
124 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
125 ::SetSplineOrders(SizeType SplineOrders)
127 m_SplineOrders=SplineOrders;
130 template <class TInputImage, class TOutputImage>
132 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
135 /* See Unser, 1997. Part II, Table I for Pole values */
136 // See also, Handbook of Medical Imaging, Processing and Analysis, Ed. Isaac N. Bankman,
138 switch (m_SplineOrder) {
142 m_SplinePoles[0] = vcl_sqrt(3.0) - 2.0;
152 m_SplinePoles[0] = vcl_sqrt(8.0) - 3.0;
156 m_SplinePoles[0] = vcl_sqrt(664.0 - vcl_sqrt(438976.0)) + vcl_sqrt(304.0) - 19.0;
157 m_SplinePoles[1] = vcl_sqrt(664.0 + vcl_sqrt(438976.0)) - vcl_sqrt(304.0) - 19.0;
161 m_SplinePoles[0] = vcl_sqrt(135.0 / 2.0 - vcl_sqrt(17745.0 / 4.0)) + vcl_sqrt(105.0 / 4.0)
163 m_SplinePoles[1] = vcl_sqrt(135.0 / 2.0 + vcl_sqrt(17745.0 / 4.0)) - vcl_sqrt(105.0 / 4.0)
167 // SplineOrder not implemented yet.
168 ExceptionObject err(__FILE__, __LINE__);
169 err.SetLocation( ITK_LOCATION);
170 err.SetDescription( "SplineOrder must be between 0 and 5. Requested spline order has not been implemented yet." );
177 template <class TInputImage, class TOutputImage>
179 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
180 ::SetInitialCausalCoefficient(double z)
182 /* begining InitialCausalCoefficient */
183 /* See Unser, 1999, Box 2 for explaination */
185 double sum, zn, z2n, iz;
186 unsigned long horizon;
188 /* this initialization corresponds to mirror boundaries */
189 horizon = m_DataLength[m_IteratorDirection];
191 if (m_Tolerance > 0.0) {
192 horizon = (long)vcl_ceil(log(m_Tolerance) / vcl_log(fabs(z)));
194 if (horizon < m_DataLength[m_IteratorDirection]) {
195 /* accelerated loop */
196 sum = m_Scratch[0]; // verify this
197 for (unsigned int n = 1; n < horizon; n++) {
198 sum += zn * m_Scratch[n];
205 z2n = vcl_pow(z, (double)(m_DataLength[m_IteratorDirection] - 1L));
206 sum = m_Scratch[0] + z2n * m_Scratch[m_DataLength[m_IteratorDirection] - 1L];
208 for (unsigned int n = 1; n <= (m_DataLength[m_IteratorDirection] - 2); n++) {
209 sum += (zn + z2n) * m_Scratch[n];
213 m_Scratch[0] = sum / (1.0 - zn * zn);
218 template <class TInputImage, class TOutputImage>
220 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
221 ::SetInitialAntiCausalCoefficient(double z)
223 // this initialization corresponds to mirror boundaries
224 /* See Unser, 1999, Box 2 for explaination */
225 // Also see erratum at http://bigwww.epfl.ch/publications/unser9902.html
226 m_Scratch[m_DataLength[m_IteratorDirection] - 1] =
227 (z / (z * z - 1.0)) *
228 (z * m_Scratch[m_DataLength[m_IteratorDirection] - 2] + m_Scratch[m_DataLength[m_IteratorDirection] - 1]);
232 template <class TInputImage, class TOutputImage>
234 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
235 ::DataToCoefficientsND()
237 OutputImagePointer output = this->GetOutput();
239 Size<ImageDimension> size = output->GetBufferedRegion().GetSize();
241 unsigned int count = output->GetBufferedRegion().GetNumberOfPixels() / size[0] * ImageDimension;
243 ProgressReporter progress(this, 0, count, 10);
245 // Initialize coeffient array
246 this->CopyImageToImage(); // Coefficients are initialized to the input data
248 for (unsigned int n=0; n < ImageDimension; n++) {
249 m_IteratorDirection = n;
250 // Loop through each dimension
252 //JV Set the correct order by dimension!
253 SetSplineOrder(m_SplineOrders[n]);
255 // Initialize iterators
256 OutputLinearIterator CIterator( output, output->GetBufferedRegion() );
257 CIterator.SetDirection( m_IteratorDirection );
258 // For each data vector
259 while ( !CIterator.IsAtEnd() ) {
260 // Copy coefficients to scratch
261 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 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
286 typedef ImageRegionConstIteratorWithIndex< TInputImage > InputIterator;
287 typedef 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() ) {
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() ) {
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() ) {
334 m_Scratch[j] = static_cast<double>( Iter.Get() ) ;
342 * GenerateInputRequestedRegion method.
344 template <class TInputImage, class TOutputImage>
346 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
347 ::GenerateInputRequestedRegion()
349 // this filter requires the all of the input image to be in
351 InputImagePointer inputPtr = const_cast< TInputImage * > ( this->GetInput() );
353 inputPtr->SetRequestedRegionToLargestPossibleRegion();
359 * EnlargeOutputRequestedRegion method.
361 template <class TInputImage, class TOutputImage>
363 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
364 ::EnlargeOutputRequestedRegion(
368 // this filter requires the all of the output image to be in
370 TOutputImage *imgData;
371 imgData = dynamic_cast<TOutputImage*>( output );
373 imgData->SetRequestedRegionToLargestPossibleRegion();
381 template <class TInputImage, class TOutputImage>
383 BSplineDecompositionImageFilterWithOBD<TInputImage, TOutputImage>
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++ ) {
393 if ( m_DataLength[n] > maxLength ) {
394 maxLength = m_DataLength[n];
397 m_Scratch.resize( maxLength );
399 // Allocate memory for output image
400 OutputImagePointer outputPtr = this->GetOutput();
401 outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
402 outputPtr->Allocate();
404 // Calculate actual output
405 this->DataToCoefficientsND();