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
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkFFTConvolutionImageFilter_hxx
#define itkFFTConvolutionImageFilter_hxx
#include "itkFFTConvolutionImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkChangeInformationImageFilter.h"
#include "itkConstantPadImageFilter.h"
#include "itkCyclicShiftImageFilter.h"
#include "itkExtractImageFilter.h"
#include "itkImageBase.h"
#include "itkMultiplyImageFilter.h"
#include "itkNormalizeToConstantImageFilter.h"
#include "itkMath.h"
namespace itk
{
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::FFTConvolutionImageFilter()
{
m_SizeGreatestPrimeFactor = FFTFilterType::New()->GetSizeGreatestPrimeFactor();
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
void
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::GenerateInputRequestedRegion()
{
// Request the largest possible region for both input images.
if ( this->GetInput() )
{
typename InputImageType::Pointer imagePtr =
const_cast< InputImageType * >( this->GetInput() );
imagePtr->SetRequestedRegionToLargestPossibleRegion();
}
if ( this->GetKernelImage() )
{
// Input kernel is an image, cast away the constness so we can set
// the requested region.
typename KernelImageType::Pointer kernelPtr =
const_cast< KernelImageType * >( this->GetKernelImage() );
kernelPtr->SetRequestedRegionToLargestPossibleRegion();
}
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
void
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::GenerateData()
{
// Create a process accumulator for tracking the progress of this minipipeline
ProgressAccumulator::Pointer progress = ProgressAccumulator::New();
progress->SetMiniPipelineFilter( this );
typename InputImageType::Pointer localInput = InputImageType::New();
localInput->Graft( this->GetInput() );
const KernelImageType* kernelImage = this->GetKernelImage();
InternalComplexImagePointerType input = ITK_NULLPTR;
InternalComplexImagePointerType kernel = ITK_NULLPTR;
this->PrepareInputs( localInput, kernelImage, input, kernel, progress, 0.7f );
typedef MultiplyImageFilter< InternalComplexImageType,
InternalComplexImageType,
InternalComplexImageType > MultiplyFilterType;
typename MultiplyFilterType::Pointer multiplyFilter = MultiplyFilterType::New();
multiplyFilter->SetInput1( input );
multiplyFilter->SetInput2( kernel );
multiplyFilter->ReleaseDataFlagOn();
progress->RegisterInternalFilter( multiplyFilter, 0.1 );
// Free up the memory for the prepared inputs
input = ITK_NULLPTR;
kernel = ITK_NULLPTR;
this->ProduceOutput( multiplyFilter->GetOutput(), progress, 0.2 );
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
void
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::PrepareInputs(const InputImageType * input,
const KernelImageType * kernel,
InternalComplexImagePointerType & preparedInput,
InternalComplexImagePointerType & preparedKernel,
ProgressAccumulator * progress, float progressWeight)
{
this->PrepareInput( input, preparedInput, progress, 0.5f * progressWeight );
this->PrepareKernel( kernel, preparedKernel, progress, 0.5f * progressWeight );
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
void
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::PrepareInput(const InputImageType * input,
InternalComplexImagePointerType & preparedInput,
ProgressAccumulator * progress,
float progressWeight)
{
InternalImagePointerType paddedInput;
this->PadInput( input, paddedInput, progress, 0.3f * progressWeight );
this->TransformPaddedInput( paddedInput, preparedInput, progress,
0.7f * progressWeight );
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
void
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::PadInput(const InputImageType * input,
InternalImagePointerType & paddedInput,
ProgressAccumulator * progress, float progressWeight)
{
// Pad the image
InputSizeType padSize = this->GetPadSize();
InputRegionType inputRegion = input->GetLargestPossibleRegion();
InputSizeType inputSize = inputRegion.GetSize();
typedef PadImageFilter< InputImageType, InputImageType > InputPadFilterType;
typename InputPadFilterType::Pointer inputPadder = InputPadFilterType::New();
inputPadder->SetBoundaryCondition( this->GetBoundaryCondition() );
InputSizeType inputLowerBound = this->GetPadLowerBound();
inputPadder->SetPadLowerBound( inputLowerBound );
InputSizeType inputUpperBound;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
inputUpperBound[i] = (padSize[i] - inputSize[i]) / 2;
if ( ( padSize[i] - inputSize[i] ) % 2 == 1 )
{
inputUpperBound[i]++;
}
}
inputPadder->SetPadUpperBound( inputUpperBound );
inputPadder->SetNumberOfThreads( this->GetNumberOfThreads() );
inputPadder->SetInput( input );
inputPadder->ReleaseDataFlagOn();
progress->RegisterInternalFilter( inputPadder, 0.5f * progressWeight );
// We could avoid a separate cast here by setting the output type of
// the padder to the InternalImageType, but doing so complicates the
// definition of the boundary condition passed into this class and
// requires the InternalImageType to be exposed publicly.
typedef CastImageFilter< InputImageType, InternalImageType > InputCastFilterType;
typename InputCastFilterType::Pointer inputCaster = InputCastFilterType::New();
// See if we can avoid unnecessary casting and copying of memory
inputCaster->InPlaceOn();
inputCaster->SetNumberOfThreads( this->GetNumberOfThreads() );
inputCaster->SetInput( inputPadder->GetOutput() );
inputCaster->ReleaseDataFlagOn();
progress->RegisterInternalFilter( inputCaster, 0.5f * progressWeight );
inputCaster->Update();
paddedInput = inputCaster->GetOutput();
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
void
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::TransformPaddedInput(const InternalImageType * paddedInput,
InternalComplexImagePointerType & transformedInput,
ProgressAccumulator * progress, float progressWeight)
{
// Take the Fourier transform of the padded image.
typename FFTFilterType::Pointer imageFFTFilter = FFTFilterType::New();
imageFFTFilter->SetNumberOfThreads( this->GetNumberOfThreads() );
imageFFTFilter->SetInput( paddedInput );
imageFFTFilter->ReleaseDataFlagOn();
progress->RegisterInternalFilter( imageFFTFilter, progressWeight );
imageFFTFilter->Update();
transformedInput = imageFFTFilter->GetOutput();
transformedInput->DisconnectPipeline();
imageFFTFilter->SetInput( ITK_NULLPTR );
imageFFTFilter = ITK_NULLPTR;
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
void
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::PrepareKernel(const KernelImageType * kernel,
InternalComplexImagePointerType & preparedKernel,
ProgressAccumulator * progress, float progressWeight)
{
KernelRegionType kernelRegion = kernel->GetLargestPossibleRegion();
KernelSizeType kernelSize = kernelRegion.GetSize();
InputSizeType padSize = this->GetPadSize();
typename KernelImageType::SizeType kernelUpperBound;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
kernelUpperBound[i] = padSize[i] - kernelSize[i];
}
InternalImagePointerType paddedKernelImage = ITK_NULLPTR;
float paddingWeight = 0.2f;
if ( this->GetNormalize() )
{
typedef NormalizeToConstantImageFilter< KernelImageType, InternalImageType >
NormalizeFilterType;
typename NormalizeFilterType::Pointer normalizeFilter = NormalizeFilterType::New();
normalizeFilter->SetConstant( NumericTraits< TInternalPrecision >::OneValue() );
normalizeFilter->SetNumberOfThreads( this->GetNumberOfThreads() );
normalizeFilter->SetInput( kernel );
normalizeFilter->ReleaseDataFlagOn();
progress->RegisterInternalFilter( normalizeFilter,
0.2f * paddingWeight * progressWeight );
// Pad the kernel image with zeros.
typedef ConstantPadImageFilter< InternalImageType, InternalImageType > KernelPadType;
typedef typename KernelPadType::Pointer KernelPadPointer;
KernelPadPointer kernelPadder = KernelPadType::New();
kernelPadder->SetConstant( NumericTraits< TInternalPrecision >::ZeroValue() );
kernelPadder->SetPadUpperBound( kernelUpperBound );
kernelPadder->SetNumberOfThreads( this->GetNumberOfThreads() );
kernelPadder->SetInput( normalizeFilter->GetOutput() );
kernelPadder->ReleaseDataFlagOn();
progress->RegisterInternalFilter( kernelPadder,
0.8f * paddingWeight * progressWeight );
paddedKernelImage = kernelPadder->GetOutput();
}
else
{
// Pad the kernel image with zeros.
typedef ConstantPadImageFilter< KernelImageType, InternalImageType > KernelPadType;
typedef typename KernelPadType::Pointer KernelPadPointer;
KernelPadPointer kernelPadder = KernelPadType::New();
kernelPadder->SetConstant( NumericTraits< TInternalPrecision >::ZeroValue() );
kernelPadder->SetPadUpperBound( kernelUpperBound );
kernelPadder->SetNumberOfThreads( this->GetNumberOfThreads() );
kernelPadder->SetInput( kernel );
kernelPadder->ReleaseDataFlagOn();
progress->RegisterInternalFilter( kernelPadder,
paddingWeight * progressWeight );
paddedKernelImage = kernelPadder->GetOutput();
}
// Shift the padded kernel image.
typedef CyclicShiftImageFilter< InternalImageType, InternalImageType > KernelShiftFilterType;
typename KernelShiftFilterType::Pointer kernelShifter = KernelShiftFilterType::New();
typename KernelShiftFilterType::OffsetType kernelShift;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
kernelShift[i] = -(static_cast<typename KernelShiftFilterType::OffsetType::OffsetValueType>(kernelSize[i]/2));
}
kernelShifter->SetShift( kernelShift );
kernelShifter->SetNumberOfThreads( this->GetNumberOfThreads() );
kernelShifter->SetInput( paddedKernelImage );
kernelShifter->ReleaseDataFlagOn();
progress->RegisterInternalFilter( kernelShifter, 0.1f * progressWeight );
typename FFTFilterType::Pointer kernelFFTFilter = FFTFilterType::New();
kernelFFTFilter->SetNumberOfThreads( this->GetNumberOfThreads() );
kernelFFTFilter->SetInput( kernelShifter->GetOutput() );
progress->RegisterInternalFilter( kernelFFTFilter, 0.699f * progressWeight );
kernelFFTFilter->Update();
typedef ChangeInformationImageFilter< InternalComplexImageType > InfoFilterType;
typename InfoFilterType::Pointer kernelInfoFilter = InfoFilterType::New();
kernelInfoFilter->ChangeRegionOn();
typedef typename InfoFilterType::OutputImageOffsetValueType InfoOffsetValueType;
const InputSizeType & inputLowerBound = this->GetPadLowerBound();
const InputIndexType & inputIndex = this->GetInput()->GetLargestPossibleRegion().GetIndex();
const KernelIndexType & kernelIndex = kernel->GetLargestPossibleRegion().GetIndex();
InfoOffsetValueType kernelOffset[ImageDimension];
for (unsigned int i = 0; i < ImageDimension; ++i)
{
kernelOffset[i] = static_cast< InfoOffsetValueType >( inputIndex[i] - inputLowerBound[i] - kernelIndex[i] );
}
kernelInfoFilter->SetOutputOffset( kernelOffset );
kernelInfoFilter->SetNumberOfThreads( this->GetNumberOfThreads() );
kernelInfoFilter->SetInput( kernelFFTFilter->GetOutput() );
progress->RegisterInternalFilter( kernelInfoFilter, 0.001f * progressWeight );
kernelInfoFilter->Update();
preparedKernel = kernelInfoFilter->GetOutput();
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
void
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::ProduceOutput(InternalComplexImageType * paddedOutput,
ProgressAccumulator * progress,
float progressWeight)
{
typename IFFTFilterType::Pointer ifftFilter = IFFTFilterType::New();
ifftFilter->SetActualXDimensionIsOdd( this->GetXDimensionIsOdd() );
ifftFilter->SetNumberOfThreads( this->GetNumberOfThreads() );
ifftFilter->SetInput( paddedOutput );
ifftFilter->ReleaseDataFlagOn();
progress->RegisterInternalFilter( ifftFilter, 0.6f * progressWeight );
this->CropOutput( ifftFilter->GetOutput(), progress, 0.4f * progressWeight );
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
void
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::CropOutput(InternalImageType * paddedOutput,
ProgressAccumulator * progress,
float progressWeight)
{
// Allocate the output
this->AllocateOutputs();
// Now crop the output to the desired size.
typedef ExtractImageFilter< InternalImageType, OutputImageType > ExtractFilterType;
typename ExtractFilterType::Pointer extractFilter = ExtractFilterType::New();
extractFilter->InPlaceOn();
extractFilter->GraftOutput( this->GetOutput() );
// Set up the crop sizes.
if ( this->GetOutputRegionMode() == Self::SAME )
{
InputRegionType sameRegion = this->GetInput()->GetLargestPossibleRegion();
extractFilter->SetExtractionRegion( sameRegion );
}
else // OutputRegionMode == Self::VALID
{
extractFilter->SetExtractionRegion( this->GetValidRegion() );
}
// Graft the minipipeline output to this filter.
extractFilter->SetNumberOfThreads( this->GetNumberOfThreads() );
extractFilter->SetInput( paddedOutput );
extractFilter->GetOutput()->SetRequestedRegion( this->GetOutput()->GetRequestedRegion() );
progress->RegisterInternalFilter( extractFilter, progressWeight );
extractFilter->Update();
OutputImageType *extractedImage = extractFilter->GetOutput();
OutputImageType *output = this->GetOutput();
// Only manually copy the buffer via the pixel container.
// The output meta-data of the extract filter is not correct and
// different that the GenerateOutputInformation method. So just copy
// the buffer.
output->SetBufferedRegion(extractedImage->GetBufferedRegion());
output->SetPixelContainer(extractedImage->GetPixelContainer());
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
typename FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >::InputSizeType
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::GetPadLowerBound() const
{
typename InputImageType::ConstPointer inputImage = this->GetInput();
InputSizeType inputSize = inputImage->GetLargestPossibleRegion().GetSize();
InputSizeType padSize = this->GetPadSize();
InputSizeType inputLowerBound;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
inputLowerBound[i] = (padSize[i] - inputSize[i]) / 2;
}
return inputLowerBound;
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
typename FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >::InputSizeType
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::GetPadSize() const
{
typename InputImageType::ConstPointer inputImage = this->GetInput();
InputSizeType inputSize = inputImage->GetLargestPossibleRegion().GetSize();
typename KernelImageType::ConstPointer kernelImage = this->GetKernelImage();
KernelSizeType kernelSize = kernelImage->GetLargestPossibleRegion().GetSize();
InputSizeType padSize;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
padSize[i] = inputSize[i] + kernelSize[i];
if( m_SizeGreatestPrimeFactor > 1 )
{
while ( Math::GreatestPrimeFactor( padSize[i] ) > m_SizeGreatestPrimeFactor )
{
padSize[i]++;
}
}
}
return padSize;
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
bool
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::GetXDimensionIsOdd() const
{
InputSizeType padSize = this->GetPadSize();
return (padSize[0] % 2 != 0);
}
template< typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision >
void
FFTConvolutionImageFilter< TInputImage, TKernelImage, TOutputImage, TInternalPrecision >
::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "SizeGreatestPrimeFactor: " << m_SizeGreatestPrimeFactor << std::endl;
}
}
#endif
|