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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkResampleImageFilter.txx,v $
Language: C++
Date: $Date: 2008-01-04 12:54:09 $
Version: $Revision: 1.61 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.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 _itkResampleImageFilter_txx
#define _itkResampleImageFilter_txx
// First make sure that the configuration is available.
// This line can be removed once the optimized versions
// gets integrated into the main directories.
#include "itkConfigure.h"
#ifdef ITK_USE_OPTIMIZED_REGISTRATION_METHODS
#include "itkOptResampleImageFilter.txx"
#else
#include "itkResampleImageFilter.h"
#include "itkObjectFactory.h"
#include "itkIdentityTransform.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkProgressReporter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageLinearIteratorWithIndex.h"
#include "itkSpecialCoordinatesImage.h"
namespace itk
{
/**
* Initialize new instance
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
ResampleImageFilter<TInputImage, TOutputImage,TInterpolatorPrecisionType>
::ResampleImageFilter()
{
m_OutputSpacing.Fill(1.0);
m_OutputOrigin.Fill(0.0);
m_OutputDirection.SetIdentity();
m_UseReferenceImage = false;
m_Size.Fill( 0 );
m_OutputStartIndex.Fill( 0 );
m_Transform = IdentityTransform<TInterpolatorPrecisionType, ImageDimension>::New();
m_Interpolator = LinearInterpolateImageFunction<InputImageType, TInterpolatorPrecisionType>::New();
m_DefaultPixelValue = 0;
}
/**
* Print out a description of self
*
* \todo Add details about this class
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage, TOutputImage,TInterpolatorPrecisionType>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "DefaultPixelValue: "
<< static_cast<typename NumericTraits<PixelType>::PrintType>(m_DefaultPixelValue)
<< std::endl;
os << indent << "Size: " << m_Size << std::endl;
os << indent << "OutputStartIndex: " << m_OutputStartIndex << std::endl;
os << indent << "OutputSpacing: " << m_OutputSpacing << std::endl;
os << indent << "OutputOrigin: " << m_OutputOrigin << std::endl;
os << indent << "OutputDirection: " << m_OutputDirection << std::endl;
os << indent << "Transform: " << m_Transform.GetPointer() << std::endl;
os << indent << "Interpolator: " << m_Interpolator.GetPointer() << std::endl;
os << indent << "UseReferenceImage: " << (m_UseReferenceImage ? "On" : "Off") << std::endl;
return;
}
/**
* Set the output image spacing.
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::SetOutputSpacing(
const double* spacing)
{
SpacingType s(spacing);
this->SetOutputSpacing( s );
}
/**
* Set the output image origin.
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::SetOutputOrigin(
const double* origin)
{
OriginPointType p(origin);
this->SetOutputOrigin( p );
}
/**
* Set up state of filter before multi-threading.
* InterpolatorType::SetInputImage is not thread-safe and hence
* has to be set up before ThreadedGenerateData
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::BeforeThreadedGenerateData()
{
if( !m_Transform )
{
itkExceptionMacro(<< "Transform not set");
}
if( !m_Interpolator )
{
itkExceptionMacro(<< "Interpolator not set");
}
// Connect input image to interpolator
m_Interpolator->SetInputImage( this->GetInput() );
}
/**
* Set up state of filter after multi-threading.
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::AfterThreadedGenerateData()
{
// Disconnect input image from the interpolator
m_Interpolator->SetInputImage( NULL );
}
/**
* ThreadedGenerateData
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::ThreadedGenerateData(
const OutputImageRegionType& outputRegionForThread,
int threadId)
{
// Check whether the input or the output is a
// SpecialCoordinatesImage. If either are, then we cannot use the
// fast path since index mapping will definately not be linear.
typedef SpecialCoordinatesImage<PixelType, ImageDimension> OutputSpecialCoordinatesImageType;
typedef SpecialCoordinatesImage<InputPixelType, InputImageDimension> InputSpecialCoordinatesImageType;
// Our friend the SGI needs these declarations to avoid unresolved
// linker errors.
#ifdef __sgi
InputSpecialCoordinatesImageType::Pointer foo =
InputSpecialCoordinatesImageType::New();
OutputSpecialCoordinatesImageType::Pointer bar =
OutputSpecialCoordinatesImageType::New();
#endif
if (dynamic_cast<const InputSpecialCoordinatesImageType *>(this->GetInput())
|| dynamic_cast<const OutputSpecialCoordinatesImageType *>(this->GetOutput()))
{
this->NonlinearThreadedGenerateData(outputRegionForThread, threadId);
return;
}
// Check whether we can use a fast path for resampling. Fast path
// can be used if the transformation is linear. Transform respond
// to the IsLinear() call.
if( m_Transform->IsLinear() )
{
this->LinearThreadedGenerateData(outputRegionForThread, threadId);
return;
}
// Otherwise, we use the normal method where the transform is called
// for computing the transformation of every point.
this->NonlinearThreadedGenerateData(outputRegionForThread, threadId);
}
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::NonlinearThreadedGenerateData(
const OutputImageRegionType& outputRegionForThread,
int threadId)
{
// Get the output pointers
OutputImagePointer outputPtr = this->GetOutput();
// Get ths input pointers
InputImageConstPointer inputPtr=this->GetInput();
// Create an iterator that will walk the output region for this thread.
typedef ImageRegionIteratorWithIndex<TOutputImage> OutputIterator;
OutputIterator outIt(outputPtr, outputRegionForThread);
// Define a few indices that will be used to translate from an input pixel
// to an output pixel
PointType outputPoint; // Coordinates of current output pixel
PointType inputPoint; // Coordinates of current input pixel
typedef ContinuousIndex<TInterpolatorPrecisionType, ImageDimension> ContinuousIndexType;
ContinuousIndexType inputIndex;
// Support for progress methods/callbacks
ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
typedef typename InterpolatorType::OutputType OutputType;
// Min/max values of the output pixel type AND these values
// represented as the output type of the interpolator
const PixelType minValue = NumericTraits<PixelType >::NonpositiveMin();
const PixelType maxValue = NumericTraits<PixelType >::max();
const OutputType minOutputValue = static_cast<OutputType>(minValue);
const OutputType maxOutputValue = static_cast<OutputType>(maxValue);
// Walk the output region
outIt.GoToBegin();
// This fix works for images up to approximately 2^25 pixels in
// any dimension. If the image is larger than this, this constant
// needs to be made lower.
double precisionConstant = 1<<(NumericTraits<double>::digits>>1);
while ( !outIt.IsAtEnd() )
{
// Determine the index of the current output pixel
outputPtr->TransformIndexToPhysicalPoint( outIt.GetIndex(), outputPoint );
// Compute corresponding input pixel position
inputPoint = m_Transform->TransformPoint(outputPoint);
inputPtr->TransformPhysicalPointToContinuousIndex(inputPoint, inputIndex);
// The inputIndex is precise to many decimal points, but this precision
// involves some error in the last bits.
// Sometimes, when an index should be inside of the image, the
// index will be slightly
// greater than the largest index in the image, like 255.00000000002
// for a image of size 256. This can cause an empty row to show up
// at the bottom of the image.
// Therefore, the following routine uses a
// precisionConstant that specifies the number of relevant bits,
// and the value is truncated to this precision.
for (int i=0; i < ImageDimension; ++i)
{
double roundedInputIndex = vcl_floor(inputIndex[i]);
double inputIndexFrac = inputIndex[i] - roundedInputIndex;
double newInputIndexFrac = vcl_floor(precisionConstant * inputIndexFrac)/precisionConstant;
inputIndex[i] = roundedInputIndex + newInputIndexFrac;
}
// Evaluate input at right position and copy to the output
if( m_Interpolator->IsInsideBuffer(inputIndex) )
{
PixelType pixval;
const OutputType value
= m_Interpolator->EvaluateAtContinuousIndex(inputIndex);
if( value < minOutputValue )
{
pixval = minValue;
}
else if( value > maxOutputValue )
{
pixval = maxValue;
}
else
{
pixval = static_cast<PixelType>( value );
}
outIt.Set( pixval );
}
else
{
outIt.Set(m_DefaultPixelValue); // default background value
}
progress.CompletedPixel();
++outIt;
}
return;
}
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::LinearThreadedGenerateData(
const OutputImageRegionType& outputRegionForThread,
int threadId)
{
// Get the output pointers
OutputImagePointer outputPtr = this->GetOutput();
// Get ths input pointers
InputImageConstPointer inputPtr=this->GetInput();
// Create an iterator that will walk the output region for this thread.
typedef ImageLinearIteratorWithIndex<TOutputImage> OutputIterator;
OutputIterator outIt(outputPtr, outputRegionForThread);
outIt.SetDirection( 0 );
// Define a few indices that will be used to translate from an input pixel
// to an output pixel
PointType outputPoint; // Coordinates of current output pixel
PointType inputPoint; // Coordinates of current input pixel
PointType tmpOutputPoint;
PointType tmpInputPoint;
typedef ContinuousIndex<TInterpolatorPrecisionType, ImageDimension> ContinuousIndexType;
ContinuousIndexType inputIndex;
ContinuousIndexType tmpInputIndex;
typedef typename PointType::VectorType VectorType;
VectorType delta; // delta in input continuous index coordinate frame
IndexType index;
// Support for progress methods/callbacks
ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
typedef typename InterpolatorType::OutputType OutputType;
// Cache information from the superclass
PixelType defaultValue = this->GetDefaultPixelValue();
// Min/max values of the output pixel type AND these values
// represented as the output type of the interpolator
const PixelType minValue = NumericTraits<PixelType >::NonpositiveMin();
const PixelType maxValue = NumericTraits<PixelType >::max();
const OutputType minOutputValue = static_cast<OutputType>(minValue);
const OutputType maxOutputValue = static_cast<OutputType>(maxValue);
// Determine the position of the first pixel in the scanline
index = outIt.GetIndex();
outputPtr->TransformIndexToPhysicalPoint( index, outputPoint );
// Compute corresponding input pixel position
inputPoint = m_Transform->TransformPoint(outputPoint);
inputPtr->TransformPhysicalPointToContinuousIndex(inputPoint, inputIndex);
// As we walk across a scan line in the output image, we trace
// an oriented/scaled/translated line in the input image. Cache
// the delta along this line in continuous index space of the input
// image. This allows us to use vector addition to model the
// transformation.
//
// To find delta, we take two pixels adjacent in a scanline
// and determine the continuous indices of these pixels when
// mapped to the input coordinate frame. We use the difference
// between these two continuous indices as the delta to apply
// to an index to trace line in the input image as we move
// across the scanline of the output image.
//
// We determine delta in this manner so that Images and
// OrientedImages are both handled properly (with the delta for
// OrientedImages taking into account the direction cosines).
//
++index[0];
outputPtr->TransformIndexToPhysicalPoint( index, tmpOutputPoint );
tmpInputPoint = m_Transform->TransformPoint( tmpOutputPoint );
inputPtr->TransformPhysicalPointToContinuousIndex(tmpInputPoint,
tmpInputIndex);
delta = tmpInputIndex - inputIndex;
// This fix works for images up to approximately 2^25 pixels in
// any dimension. If the image is larger than this, this constant
// needs to be made lower.
double precisionConstant = 1<<(NumericTraits<double>::digits>>1);
// Delta is precise to many decimal points, but this precision
// involves some error in the last bits. This error can accumulate
// as the delta values are added.
// Sometimes, when the accumulated delta should be inside of the
// image, it will be slightly
// greater than the largest index in the image, like 255.00000000002
// for a image of size 256. This can cause an empty column to show up
// at the right side of the image. If we instead
// truncate this delta value to some precision, this solves the problem.
// Therefore, the following routine uses a
// precisionConstant that specifies the number of relevant bits,
// and the value is truncated to this precision.
for (int i=0; i < ImageDimension; ++i)
{
double roundedDelta = vcl_floor(delta[i]);
double deltaFrac = delta[i] - roundedDelta;
double newDeltaFrac = vcl_floor(precisionConstant * deltaFrac)/precisionConstant;
delta[i] = roundedDelta + newDeltaFrac;
}
while ( !outIt.IsAtEnd() )
{
// Determine the continuous index of the first pixel of output
// scanline when mapped to the input coordinate frame.
//
// First get the position of the pixel in the output coordinate frame
index = outIt.GetIndex();
outputPtr->TransformIndexToPhysicalPoint( index, outputPoint );
// Compute corresponding input pixel continuous index, this index
// will incremented in the scanline loop
inputPoint = m_Transform->TransformPoint(outputPoint);
inputPtr->TransformPhysicalPointToContinuousIndex(inputPoint, inputIndex);
// The inputIndex is precise to many decimal points, but this precision
// involves some error in the last bits.
// Sometimes, when an index should be inside of the image, the
// index will be slightly
// greater than the largest index in the image, like 255.00000000002
// for a image of size 256. This can cause an empty row to show up
// at the bottom of the image.
// Therefore, the following routine uses a
// precisionConstant that specifies the number of relevant bits,
// and the value is truncated to this precision.
for (int i=0; i < ImageDimension; ++i)
{
double roundedInputIndex = vcl_floor(inputIndex[i]);
double inputIndexFrac = inputIndex[i] - roundedInputIndex;
double newInputIndexFrac = vcl_floor(precisionConstant * inputIndexFrac)/precisionConstant;
inputIndex[i] = roundedInputIndex + newInputIndexFrac;
}
while( !outIt.IsAtEndOfLine() )
{
// Evaluate input at right position and copy to the output
if( m_Interpolator->IsInsideBuffer(inputIndex) )
{
PixelType pixval;
const OutputType value
= m_Interpolator->EvaluateAtContinuousIndex(inputIndex);
if( value < minOutputValue )
{
pixval = minValue;
}
else if( value > maxOutputValue )
{
pixval = maxValue;
}
else
{
pixval = static_cast<PixelType>( value );
}
outIt.Set( pixval );
}
else
{
outIt.Set(defaultValue); // default background value
}
progress.CompletedPixel();
++outIt;
inputIndex += delta;
}
outIt.NextLine();
}
return;
}
/**
* Inform pipeline of necessary input image region
*
* Determining the actual input region is non-trivial, especially
* when we cannot assume anything about the transform being used.
* So we do the easy thing and request the entire input image.
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::GenerateInputRequestedRegion()
{
// call the superclass's implementation of this method
Superclass::GenerateInputRequestedRegion();
if ( !this->GetInput() )
{
return;
}
// get pointers to the input and output
InputImagePointer inputPtr =
const_cast< TInputImage *>( this->GetInput() );
// Request the entire input image
InputImageRegionType inputRegion;
inputRegion = inputPtr->GetLargestPossibleRegion();
inputPtr->SetRequestedRegion(inputRegion);
return;
}
/**
* Set the smart pointer to the reference image that will provide
* the grid parameters for the output image.
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
const typename ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>::OutputImageType *
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::GetReferenceImage() const
{
Self * surrogate = const_cast< Self * >( this );
const OutputImageType * referenceImage =
static_cast<const OutputImageType *>(surrogate->ProcessObject::GetInput(1));
return referenceImage;
}
/**
* Set the smart pointer to the reference image that will provide
* the grid parameters for the output image.
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::SetReferenceImage( const TOutputImage *image )
{
itkDebugMacro("setting input ReferenceImage to " << image);
if( image != static_cast<const TOutputImage *>(this->GetInput( 1 )) )
{
this->ProcessObject::SetNthInput(1, const_cast< TOutputImage *>( image ) );
this->Modified();
}
}
/**
* Inform pipeline of required output region
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
void
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::GenerateOutputInformation()
{
// call the superclass' implementation of this method
Superclass::GenerateOutputInformation();
// get pointers to the input and output
OutputImagePointer outputPtr = this->GetOutput();
if ( !outputPtr )
{
return;
}
const OutputImageType * referenceImage = this->GetReferenceImage();
// Set the size of the output region
if( m_UseReferenceImage && referenceImage )
{
outputPtr->SetLargestPossibleRegion( referenceImage->GetLargestPossibleRegion() );
}
else
{
typename TOutputImage::RegionType outputLargestPossibleRegion;
outputLargestPossibleRegion.SetSize( m_Size );
outputLargestPossibleRegion.SetIndex( m_OutputStartIndex );
outputPtr->SetLargestPossibleRegion( outputLargestPossibleRegion );
}
// Set spacing and origin
if (m_UseReferenceImage && referenceImage)
{
outputPtr->SetSpacing( referenceImage->GetSpacing() );
outputPtr->SetOrigin( referenceImage->GetOrigin() );
outputPtr->SetDirection( referenceImage->GetDirection() );
}
else
{
outputPtr->SetSpacing( m_OutputSpacing );
outputPtr->SetOrigin( m_OutputOrigin );
outputPtr->SetDirection( m_OutputDirection );
}
return;
}
/**
* Verify if any of the components has been modified.
*/
template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
unsigned long
ResampleImageFilter<TInputImage,TOutputImage,TInterpolatorPrecisionType>
::GetMTime( void ) const
{
unsigned long latestTime = Object::GetMTime();
if( m_Transform )
{
if( latestTime < m_Transform->GetMTime() )
{
latestTime = m_Transform->GetMTime();
}
}
if( m_Interpolator )
{
if( latestTime < m_Interpolator->GetMTime() )
{
latestTime = m_Interpolator->GetMTime();
}
}
return latestTime;
}
} // end namespace itk
#endif
#endif
|