1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkBSplineDecompositionImageFilter.txx,v $
Language: C++
Date: $Date: 2006-08-01 19:16:16 $
Version: $Revision: 1.10 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
Portions of this code are covered under the VTK copyright.
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef _itkBSplineDecompositionImageFilter_txx
#define _itkBSplineDecompositionImageFilter_txx
#include "itkBSplineDecompositionImageFilter.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkImageRegionIterator.h"
#include "itkProgressReporter.h"
#include "itkVector.h"
namespace itk
{
/**
* Constructor
*/
template <class TInputImage, class TOutputImage>
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::BSplineDecompositionImageFilter()
{
m_SplineOrder = 0;
int SplineOrder = 3;
m_Tolerance = 1e-10; // Need some guidance on this one...what is reasonable?
m_IteratorDirection = 0;
this->SetSplineOrder(SplineOrder);
}
/**
* Standard "PrintSelf" method
*/
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::PrintSelf(
std::ostream& os,
Indent indent) const
{
Superclass::PrintSelf( os, indent );
os << indent << "Spline Order: " << m_SplineOrder << std::endl;
}
template <class TInputImage, class TOutputImage>
bool
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::DataToCoefficients1D()
{
// See Unser, 1993, Part II, Equation 2.5,
// or Unser, 1999, Box 2. for an explaination.
double c0 = 1.0;
if (m_DataLength[m_IteratorDirection] == 1) //Required by mirror boundaries
{
return false;
}
// Compute overall gain
for (int k = 0; k < m_NumberOfPoles; k++)
{
// Note for cubic splines lambda = 6
c0 = c0 * (1.0 - m_SplinePoles[k]) * (1.0 - 1.0 / m_SplinePoles[k]);
}
// apply the gain
for (unsigned int n = 0; n < m_DataLength[m_IteratorDirection]; n++)
{
m_Scratch[n] *= c0;
}
// loop over all poles
for (int k = 0; k < m_NumberOfPoles; k++)
{
// causal initialization
this->SetInitialCausalCoefficient(m_SplinePoles[k]);
// causal recursion
for (unsigned int n = 1; n < m_DataLength[m_IteratorDirection]; n++)
{
m_Scratch[n] += m_SplinePoles[k] * m_Scratch[n - 1];
}
// anticausal initialization
this->SetInitialAntiCausalCoefficient(m_SplinePoles[k]);
// anticausal recursion
for ( int n = m_DataLength[m_IteratorDirection] - 2; 0 <= n; n--)
{
m_Scratch[n] = m_SplinePoles[k] * (m_Scratch[n + 1] - m_Scratch[n]);
}
}
return true;
}
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::SetSplineOrder(unsigned int SplineOrder)
{
if (SplineOrder == m_SplineOrder)
{
return;
}
m_SplineOrder = SplineOrder;
this->SetPoles();
this->Modified();
}
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::SetPoles()
{
/* See Unser, 1997. Part II, Table I for Pole values */
// See also, Handbook of Medical Imaging, Processing and Analysis, Ed. Isaac N. Bankman,
// 2000, pg. 416.
switch (m_SplineOrder)
{
case 3:
m_NumberOfPoles = 1;
m_SplinePoles[0] = vcl_sqrt(3.0) - 2.0;
break;
case 0:
m_NumberOfPoles = 0;
break;
case 1:
m_NumberOfPoles = 0;
break;
case 2:
m_NumberOfPoles = 1;
m_SplinePoles[0] = vcl_sqrt(8.0) - 3.0;
break;
case 4:
m_NumberOfPoles = 2;
m_SplinePoles[0] = vcl_sqrt(664.0 - vcl_sqrt(438976.0)) + vcl_sqrt(304.0) - 19.0;
m_SplinePoles[1] = vcl_sqrt(664.0 + vcl_sqrt(438976.0)) - vcl_sqrt(304.0) - 19.0;
break;
case 5:
m_NumberOfPoles = 2;
m_SplinePoles[0] = vcl_sqrt(135.0 / 2.0 - vcl_sqrt(17745.0 / 4.0)) + vcl_sqrt(105.0 / 4.0)
- 13.0 / 2.0;
m_SplinePoles[1] = vcl_sqrt(135.0 / 2.0 + vcl_sqrt(17745.0 / 4.0)) - vcl_sqrt(105.0 / 4.0)
- 13.0 / 2.0;
break;
default:
// SplineOrder not implemented yet.
ExceptionObject err(__FILE__, __LINE__);
err.SetLocation( ITK_LOCATION);
err.SetDescription( "SplineOrder must be between 0 and 5. Requested spline order has not been implemented yet." );
throw err;
break;
}
}
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::SetInitialCausalCoefficient(double z)
{
/* begining InitialCausalCoefficient */
/* See Unser, 1999, Box 2 for explaination */
double sum, zn, z2n, iz;
unsigned long horizon;
/* this initialization corresponds to mirror boundaries */
horizon = m_DataLength[m_IteratorDirection];
zn = z;
if (m_Tolerance > 0.0)
{
horizon = (long)vcl_ceil(log(m_Tolerance) / vcl_log(fabs(z)));
}
if (horizon < m_DataLength[m_IteratorDirection])
{
/* accelerated loop */
sum = m_Scratch[0]; // verify this
for (unsigned int n = 1; n < horizon; n++)
{
sum += zn * m_Scratch[n];
zn *= z;
}
m_Scratch[0] = sum;
}
else {
/* full loop */
iz = 1.0 / z;
z2n = vcl_pow(z, (double)(m_DataLength[m_IteratorDirection] - 1L));
sum = m_Scratch[0] + z2n * m_Scratch[m_DataLength[m_IteratorDirection] - 1L];
z2n *= z2n * iz;
for (unsigned int n = 1; n <= (m_DataLength[m_IteratorDirection] - 2); n++)
{
sum += (zn + z2n) * m_Scratch[n];
zn *= z;
z2n *= iz;
}
m_Scratch[0] = sum / (1.0 - zn * zn);
}
}
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::SetInitialAntiCausalCoefficient(double z)
{
// this initialization corresponds to mirror boundaries
/* See Unser, 1999, Box 2 for explaination */
// Also see erratum at http://bigwww.epfl.ch/publications/unser9902.html
m_Scratch[m_DataLength[m_IteratorDirection] - 1] =
(z / (z * z - 1.0)) *
(z * m_Scratch[m_DataLength[m_IteratorDirection] - 2] + m_Scratch[m_DataLength[m_IteratorDirection] - 1]);
}
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::DataToCoefficientsND()
{
OutputImagePointer output = this->GetOutput();
Size<ImageDimension> size = output->GetBufferedRegion().GetSize();
unsigned int count = output->GetBufferedRegion().GetNumberOfPixels() / size[0] * ImageDimension;
ProgressReporter progress(this, 0, count, 10);
// Initialize coeffient array
this->CopyImageToImage(); // Coefficients are initialized to the input data
for (unsigned int n=0; n < ImageDimension; n++)
{
m_IteratorDirection = n;
// Loop through each dimension
// Initialize iterators
OutputLinearIterator CIterator( output, output->GetBufferedRegion() );
CIterator.SetDirection( m_IteratorDirection );
// For each data vector
while ( !CIterator.IsAtEnd() )
{
// Copy coefficients to scratch
this->CopyCoefficientsToScratch( CIterator );
// Perform 1D BSpline calculations
this->DataToCoefficients1D();
// Copy scratch back to coefficients.
// Brings us back to the end of the line we were working on.
CIterator.GoToBeginOfLine();
this->CopyScratchToCoefficients( CIterator ); // m_Scratch = m_Image;
CIterator.NextLine();
progress.CompletedPixel();
}
}
}
/**
* Copy the input image into the output image
*/
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::CopyImageToImage()
{
typedef ImageRegionConstIteratorWithIndex< TInputImage > InputIterator;
typedef ImageRegionIterator< TOutputImage > OutputIterator;
typedef typename TOutputImage::PixelType OutputPixelType;
InputIterator inIt( this->GetInput(), this->GetInput()->GetBufferedRegion() );
OutputIterator outIt( this->GetOutput(), this->GetOutput()->GetBufferedRegion() );
inIt = inIt.Begin();
outIt = outIt.Begin();
while ( !outIt.IsAtEnd() )
{
outIt.Set( static_cast<OutputPixelType>( inIt.Get() ) );
++inIt;
++outIt;
}
}
/**
* Copy the scratch to one line of the output image
*/
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::CopyScratchToCoefficients(OutputLinearIterator & Iter)
{
typedef typename TOutputImage::PixelType OutputPixelType;
unsigned long j = 0;
while ( !Iter.IsAtEndOfLine() )
{
Iter.Set( static_cast<OutputPixelType>( m_Scratch[j] ) );
++Iter;
++j;
}
}
/**
* Copy one line of the output image to the scratch
*/
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::CopyCoefficientsToScratch(OutputLinearIterator & Iter)
{
unsigned long j = 0;
while ( !Iter.IsAtEndOfLine() )
{
m_Scratch[j] = static_cast<double>( Iter.Get() ) ;
++Iter;
++j;
}
}
/**
* GenerateInputRequestedRegion method.
*/
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::GenerateInputRequestedRegion()
{
// this filter requires the all of the input image to be in
// the buffer
InputImagePointer inputPtr = const_cast< TInputImage * > ( this->GetInput() );
if( inputPtr )
{
inputPtr->SetRequestedRegionToLargestPossibleRegion();
}
}
/**
* EnlargeOutputRequestedRegion method.
*/
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::EnlargeOutputRequestedRegion(
DataObject *output )
{
// this filter requires the all of the output image to be in
// the buffer
TOutputImage *imgData;
imgData = dynamic_cast<TOutputImage*>( output );
if( imgData )
{
imgData->SetRequestedRegionToLargestPossibleRegion();
}
}
/**
* Generate data
*/
template <class TInputImage, class TOutputImage>
void
BSplineDecompositionImageFilter<TInputImage, TOutputImage>
::GenerateData()
{
// Allocate scratch memory
InputImageConstPointer inputPtr = this->GetInput();
m_DataLength = inputPtr->GetBufferedRegion().GetSize();
unsigned long maxLength = 0;
for ( unsigned int n = 0; n < ImageDimension; n++ )
{
if ( m_DataLength[n] > maxLength )
{
maxLength = m_DataLength[n];
}
}
m_Scratch.resize( maxLength );
// Allocate memory for output image
OutputImagePointer outputPtr = this->GetOutput();
outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
outputPtr->Allocate();
// Calculate actual output
this->DataToCoefficientsND();
// Clean up
m_Scratch.clear();
}
} // namespace itk
#endif
|