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
|
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt 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 otbGridResampleImageFilter_txx
#define otbGridResampleImageFilter_txx
#include "otbGridResampleImageFilter.h"
#include "otbStreamingTraits.h"
#include "itkNumericTraits.h"
#include "itkProgressReporter.h"
#include "itkImageScanlineIterator.h"
#include "itkContinuousIndex.h"
namespace otb
{
template <typename TInputImage, typename TOutputImage,
typename TInterpolatorPrecision>
GridResampleImageFilter<TInputImage, TOutputImage, TInterpolatorPrecision>
::GridResampleImageFilter()
: m_OutputStartIndex(),
m_OutputSize(),
m_OutputOrigin(),
m_OutputSpacing(),
m_EdgePaddingValue(),
m_CheckOutputBounds(true),
m_Interpolator(),
m_ReachableOutputRegion()
{
// Set linear interpolator as default
m_Interpolator = dynamic_cast<InterpolatorType *>(DefaultInterpolatorType::New().GetPointer());
// Initialize EdgePaddingValue
m_EdgePaddingValue = itk::NumericTraits<OutputPixelType>::ZeroValue(m_EdgePaddingValue);
// Initialize origin and spacing
m_OutputOrigin.Fill(0.);
m_OutputSpacing.Fill(1.);
m_OutputStartIndex.Fill(0);
m_OutputSize.Fill(0);
}
/** Import output parameters from a given image */
template <typename TInputImage, typename TOutputImage,
typename TInterpolatorPrecision>
void
GridResampleImageFilter<TInputImage, TOutputImage, TInterpolatorPrecision>
::SetOutputParametersFromImage(const ImageBaseType * image)
{
this->SetOutputOrigin ( image->GetOrigin() );
this->SetOutputSpacing ( image->GetSpacing() );
this->SetOutputStartIndex ( image->GetLargestPossibleRegion().GetIndex() );
this->SetOutputSize ( image->GetLargestPossibleRegion().GetSize() );
}
template <typename TInputImage, typename TOutputImage,
typename TInterpolatorPrecision>
void
GridResampleImageFilter<TInputImage, TOutputImage, TInterpolatorPrecision>
::GenerateOutputInformation()
{
// call the superclass' implementation of this method
Superclass::GenerateOutputInformation();
// get pointers to the input and output
OutputImageType *outputPtr = this->GetOutput();
if ( !outputPtr )
{
return;
}
// Fill output image data
typename TOutputImage::RegionType outputLargestPossibleRegion;
outputLargestPossibleRegion.SetSize(m_OutputSize);
outputLargestPossibleRegion.SetIndex(m_OutputStartIndex);
outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
outputPtr->SetSpacing(m_OutputSpacing);
outputPtr->SetOrigin(m_OutputOrigin);
// TODO: Report no data value here
}
template <typename TInputImage, typename TOutputImage,
typename TInterpolatorPrecision>
void
GridResampleImageFilter<TInputImage, TOutputImage, TInterpolatorPrecision>
::GenerateInputRequestedRegion()
{
// call the superclass's implementation of this method
Superclass::GenerateInputRequestedRegion();
// Check for input
if ( !this->GetInput() )
{
return;
}
// get pointers to the input and output
typename TInputImage::Pointer inputPtr =
const_cast< TInputImage * >( this->GetInput() );
// check for output
OutputImageType *outputPtr = this->GetOutput();
if ( !outputPtr )
{
return;
}
typename TOutputImage::RegionType outputRequestedRegion = outputPtr->GetRequestedRegion();
IndexType outULIndex,outLRIndex;
typedef itk::ContinuousIndex<double,TInputImage::ImageDimension> ContinuousIndexType;
ContinuousIndexType inULCIndex,inLRCIndex;
// Get output image requested region corners as Index
outULIndex = outputRequestedRegion.GetIndex();
outLRIndex = outULIndex+outputRequestedRegion.GetSize();
outLRIndex[0]-=1;
outLRIndex[1]-=1;
// Transform to physiscal points
PointType outULPoint,outLRPoint;
outputPtr->TransformIndexToPhysicalPoint(outULIndex,outULPoint);
outputPtr->TransformIndexToPhysicalPoint(outLRIndex,outLRPoint);
// Transform to input image Index
inputPtr->TransformPhysicalPointToContinuousIndex(outULPoint,inULCIndex);
inputPtr->TransformPhysicalPointToContinuousIndex(outLRPoint,inLRCIndex);
SizeType inSize;
IndexType inULIndex,inLRIndex;
// Reorder index properly and compute size
for(unsigned int dim = 0; dim < ImageDimension;++dim)
{
if(inULCIndex[dim] > inLRCIndex[dim])
{
// swap
typename ContinuousIndexType::ValueType tmp(inULCIndex[dim]);
inULCIndex[dim]=inLRCIndex[dim];
inLRCIndex[dim]=tmp;
}
// Ensure correct rounding of coordinates
inULIndex[dim] = vcl_floor(inULCIndex[dim]);
inLRIndex[dim] = vcl_ceil(inLRCIndex[dim]);
inSize[dim] = static_cast<typename SizeType::SizeValueType>(inLRIndex[dim]-inULIndex[dim])+1;
}
// Build the input requested region
typename TInputImage::RegionType inputRequestedRegion;
inputRequestedRegion.SetIndex(inULIndex);
inputRequestedRegion.SetSize(inSize);
// Compute the padding due to the interpolator
unsigned int interpolatorRadius =
StreamingTraits<typename Superclass::InputImageType>::CalculateNeededRadiusForInterpolator(this->GetInterpolator());
inputRequestedRegion.PadByRadius(interpolatorRadius);
// crop the input requested region at the input's largest possible region
if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
{
inputPtr->SetRequestedRegion(inputRequestedRegion);
}
else
{
// store what we tried to request (prior to trying to crop)
inputPtr->SetRequestedRegion(inputRequestedRegion);
// build an exception
itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
e.SetLocation(ITK_LOCATION);
e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
e.SetDataObject(inputPtr);
throw e;
}
}
template <typename TInputImage, typename TOutputImage,
typename TInterpolatorPrecision>
void
GridResampleImageFilter<TInputImage, TOutputImage, TInterpolatorPrecision>
::BeforeThreadedGenerateData()
{
if ( !m_Interpolator )
{
itkExceptionMacro(<< "Interpolator not set");
}
// Connect input image to interpolator
m_Interpolator->SetInputImage( this->GetInput() );
unsigned int nComponents
= itk::DefaultConvertPixelTraits<OutputPixelType>::GetNumberOfComponents(
m_EdgePaddingValue );
if (nComponents == 0)
{
// Build a default value of the correct number of components
OutputPixelComponentType zeroComponent
= itk::NumericTraits<OutputPixelComponentType>::ZeroValue( zeroComponent );
nComponents = this->GetInput()->GetNumberOfComponentsPerPixel();
itk::NumericTraits<OutputPixelType>::SetLength(m_EdgePaddingValue, nComponents );
for (unsigned int n=0; n<nComponents; n++)
{
OutputPixelConvertType::SetNthComponent( n, m_EdgePaddingValue,
zeroComponent );
}
}
// Compute ReachableOutputRegion
// InputImage buffered region corresponds to a region of the output
// image. Computing it beforehand allows saving IsInsideBuffer
// calls in the interpolation loop
// Compute the padding due to the interpolator
IndexType inUL = this->GetInput()->GetBufferedRegion().GetIndex();
IndexType inLR = this->GetInput()->GetBufferedRegion().GetIndex() + this->GetInput()->GetBufferedRegion().GetSize();
inLR[0]-=1;
inLR[1]-=1;
// We should take interpolation radius into account here, but this
// does not match the IsInsideBuffer method
// unsigned int interpolatorRadius =
// StreamingTraits<typename Superclass::InputImageType>::CalculateNeededRadiusForInterpolator(this->GetInterpolator());
// inUL[0]+=interpolatorRadius;
// inUL[1]+=interpolatorRadius;
// inLR[0]-=interpolatorRadius;
// inLR[1]-=interpolatorRadius;
PointType inULp, inLRp;
this->GetInput()->TransformIndexToPhysicalPoint(inUL,inULp);
this->GetInput()->TransformIndexToPhysicalPoint(inLR,inLRp);
inULp-=0.5*this->GetInput()->GetSpacing();
inLRp+=0.5*this->GetInput()->GetSpacing();
ContinuousInputIndexType outUL;
ContinuousInputIndexType outLR;
this->GetOutput()->TransformPhysicalPointToContinuousIndex(inULp,outUL);
this->GetOutput()->TransformPhysicalPointToContinuousIndex(inLRp,outLR);
IndexType outputIndex;
// This needs to take into account negative spacing
outputIndex[0] = vcl_ceil(std::min(outUL[0],outLR[0]));
outputIndex[1] = vcl_ceil(std::min(outUL[1],outLR[1]));
SizeType outputSize;
outputSize[0] = vcl_floor(std::max(outUL[0],outLR[0])) - outputIndex[0] + 1;
outputSize[1] = vcl_floor(std::max(outUL[1],outLR[1])) - outputIndex[1] + 1;
m_ReachableOutputRegion.SetIndex(outputIndex);
m_ReachableOutputRegion.SetSize(outputSize);
}
template <typename TInputImage, typename TOutputImage,
typename TInterpolatorPrecision>
void
GridResampleImageFilter<TInputImage, TOutputImage, TInterpolatorPrecision>
::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
{
// Get the output pointers
OutputImageType *outputPtr = this->GetOutput();
// Get this input pointers
const InputImageType *inputPtr = this->GetInput();
// Min/max values of the output pixel type AND these values
// represented as the output type of the interpolator
const OutputPixelComponentType minValue = itk::NumericTraits< OutputPixelComponentType >::NonpositiveMin();
const OutputPixelComponentType maxValue = itk::NumericTraits< OutputPixelComponentType >::max();
const InterpolatorComponentType minOutputValue = static_cast< InterpolatorComponentType >( minValue );
const InterpolatorComponentType maxOutputValue = static_cast< InterpolatorComponentType >( maxValue );
// Iterator on the output region for current thread
OutputImageRegionType regionToCompute = outputRegionForThread;
bool cropSucceed = regionToCompute.Crop(m_ReachableOutputRegion);
// Fill thread buffer
itk::ImageScanlineIterator<OutputImageType> outItFull(outputPtr,outputRegionForThread);
outItFull.GoToBegin();
while(!outItFull.IsAtEnd())
{
while(!outItFull.IsAtEndOfLine())
{
outItFull.Set(m_EdgePaddingValue);
++outItFull;
}
outItFull.NextLine();
}
if(!cropSucceed)
return;
itk::ImageScanlineIterator<OutputImageType> outIt(outputPtr, regionToCompute);
// Support for progress methods/callbacks
itk::ProgressReporter progress( this,
threadId,
regionToCompute.GetSize()[1]);
// Temporary variables for loop
PointType outPoint;
ContinuousInputIndexType inCIndex;
InterpolatorOutputType interpolatorValue; //(this->GetOutput()->GetNumberOfComponentsPerPixel());
OutputPixelType outputValue; //(this->GetOutput()->GetNumberOfComponentsPerPixel());
// TODO: assert outputPtr->GetSpacing() != 0 here
assert(outputPtr->GetSpacing()[0]!=0&&"Null spacing will cause division by zero.");
const double delta = outputPtr->GetSpacing()[0]/inputPtr->GetSpacing()[0];
// Iterate through the output region
outIt.GoToBegin();
while(!outIt.IsAtEnd())
{
// Map output index to input continuous index
outputPtr->TransformIndexToPhysicalPoint(outIt.GetIndex(),outPoint);
inputPtr->TransformPhysicalPointToContinuousIndex(outPoint,inCIndex);
while(!outIt.IsAtEndOfLine())
{
// Interpolate
interpolatorValue = m_Interpolator->EvaluateAtContinuousIndex(inCIndex);
// Cast and check bounds
this->CastPixelWithBoundsChecking(interpolatorValue,minOutputValue,maxOutputValue,outputValue);
// Set output value
outIt.Set(outputValue);
// move one pixel forward
++outIt;
// Update input position
inCIndex[0]+=delta;
}
// Report progress
progress.CompletedPixel();
// Move to next line
outIt.NextLine();
}
}
template <typename TInputImage, typename TOutputImage,
typename TInterpolatorPrecision>
void
GridResampleImageFilter<TInputImage, TOutputImage, TInterpolatorPrecision>
::AfterThreadedGenerateData()
{
// Disconnect input image from the interpolator
m_Interpolator->SetInputImage(ITK_NULLPTR);
}
template <typename TInputImage, typename TOutputImage,
typename TInterpolatorPrecision>
itk::ModifiedTimeType
GridResampleImageFilter< TInputImage, TOutputImage, TInterpolatorPrecision >
::GetMTime(void) const
{
itk::ModifiedTimeType latestTime = itk::Object::GetMTime();
if ( m_Interpolator )
{
if ( latestTime < m_Interpolator->GetMTime() )
{
latestTime = m_Interpolator->GetMTime();
}
}
return latestTime;
}
template <typename TInputImage, typename TOutputImage,
typename TInterpolatorPrecision>
void
GridResampleImageFilter<TInputImage, TOutputImage, TInterpolatorPrecision>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "EdgePaddingValue: "
<< static_cast< typename itk::NumericTraits< OutputPixelType >::PrintType >
( m_EdgePaddingValue )
<< std::endl;
os << indent << "OutputStartIndex: " << m_OutputStartIndex << std::endl;
os << indent << "OutputSize: " << m_OutputSize << std::endl;
os << indent << "OutputOrigin: " << m_OutputOrigin << std::endl;
os << indent << "OutputSpacing: " << m_OutputSpacing << std::endl;
os << indent << "Interpolator: " << m_Interpolator.GetPointer() << std::endl;
os << indent << "CheckOutputBounds: " << ( m_CheckOutputBounds ? "On" : "Off" )
<< std::endl;
}
} // namespace otb
#endif
|