File: itkFFTConvolutionImageFilter.hxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (531 lines) | stat: -rw-r--r-- 23,307 bytes parent folder | download
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
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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
 *
 *         https://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 "itkCastImageFilter.h"
#include "itkChangeInformationImageFilter.h"
#include "itkConstantPadImageFilter.h"
#include "itkCyclicShiftImageFilter.h"
#include "itkExtractImageFilter.h"
#include "itkFFTPadImageFilter.h"
#include "itkImageBase.h"
#include "itkMultiplyImageFilter.h"
#include "itkNormalizeToConstantImageFilter.h"
#include "itkMath.h"
#include "itkRegionOfInterestImageFilter.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()
{
  // Pad the input image with the radius of the kernel.
  if (this->GetInput())
  {
    InputRegionType inputRegion = this->GetOutput()->GetRequestedRegion();

    // Pad the output request region by the kernel radius.
    // Note that FFT padding will always be generated by a boundary condition
    // as an implementation detail, while pixels for kernel radius padding may be taken
    // from the original image if they lies inside the image bounds.
    inputRegion.PadByRadius(this->GetKernelRadius());

    // Crop the output requested region to fit within the largest
    // possible region.
    InputImageType * inputPtr = itkDynamicCastInDebugMode<InputImageType *>(this->GetPrimaryInput());
    bool             wasPartiallyInside = inputRegion.Crop(inputPtr->GetLargestPossibleRegion());
    if (!wasPartiallyInside)
    {
      itkExceptionMacro("Requested region is outside the largest possible region.");
    }

    // Input is an image, cast away the constness so we can set
    // the requested region.
    inputPtr->SetRequestedRegion(inputRegion);
  }

  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
  auto progress = ProgressAccumulator::New();
  progress->SetMiniPipelineFilter(this);

  auto localInput = InputImageType::New();
  localInput->Graft(this->GetInput());

  const KernelImageType * kernelImage = this->GetKernelImage();

  InternalComplexImagePointerType input = nullptr;
  InternalComplexImagePointerType kernel = nullptr;
  this->PrepareInputs(localInput, kernelImage, input, kernel, progress, 0.7f);

  // Convolve
  using MultiplyFilterType =
    MultiplyImageFilter<InternalComplexImageType, InternalComplexImageType, InternalComplexImageType>;
  auto multiplyFilter = MultiplyFilterType::New();
  multiplyFilter->SetInput1(input);
  multiplyFilter->SetInput2(kernel);
  multiplyFilter->ReleaseDataFlagOn();
  progress->RegisterInternalFilter(multiplyFilter, 0.1);
  multiplyFilter->Update();

  // Free up the memory for the prepared inputs
  input = nullptr;
  kernel = 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)
{
  float remainingProgress = 1.0f;

  InputRegionType  inputLargestRegion = input->GetLargestPossibleRegion();
  InputSizeType    inputLargestSize = inputLargestRegion.GetSize();
  InputIndexType   inputLargestIndex = inputLargestRegion.GetIndex();
  InputRegionType  inputRequestedRegion = input->GetRequestedRegion();
  InputSizeType    inputRequestedSize = inputRequestedRegion.GetSize();
  InputIndexType   inputRequestedIndex = inputRequestedRegion.GetIndex();
  OutputRegionType outputRequestedRegion = this->GetOutput()->GetRequestedRegion();
  OutputSizeType   outputRequestedSize = outputRequestedRegion.GetSize();
  OutputIndexType  outputRequestedIndex = outputRequestedRegion.GetIndex();

  // Pad the input image such that the requested region, expanded by
  // twice the kernel radius, lies entirely within the buffered region.
  // If the requested region is at least the kernel radius away from
  // all boundaries of the buffered input region then padding is skipped.

  using InputPadFilterType = PadImageFilter<InputImageType, InputImageType>;
  using PadSizeType = typename InputPadFilterType::SizeType;

  PadSizeType    lowerPad, upperPad;
  bool           needsKernelPadding = false;
  KernelSizeType kernelRadius = this->GetKernelRadius();
  for (unsigned int dim = 0; dim < ImageDimension; ++dim)
  {
    // Use signed int for arithmetic
    int largestUpperCorner = inputLargestIndex[dim] + inputLargestSize[dim];
    int requestedUpperCorner = inputRequestedIndex[dim] + inputRequestedSize[dim];

    int largestLowerCorner = inputLargestIndex[dim];
    int requestedLowerCorner = inputRequestedIndex[dim];

    // Pad for difference between lower corner of largest vs requested region
    int lower = static_cast<int>(kernelRadius[dim]) - (requestedLowerCorner - largestLowerCorner);
    // Pad for difference between upper corner of largest vs requested region
    int upper = static_cast<int>(kernelRadius[dim]) - (largestUpperCorner - requestedUpperCorner);
    lowerPad[dim] = (lower > 0 ? lower : 0);
    upperPad[dim] = (upper > 0 ? upper : 0);

    needsKernelPadding = needsKernelPadding || lower > 0 || upper > 0;
  }

  const InputImageType * kernelPaddedInput;
  if (needsKernelPadding)
  {
    auto inputPadder = InputPadFilterType::New();
    inputPadder->SetBoundaryCondition(this->GetBoundaryCondition());
    inputPadder->SetPadLowerBound(lowerPad);
    inputPadder->SetPadUpperBound(upperPad);
    inputPadder->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
    inputPadder->SetInput(input);
    inputPadder->ReleaseDataFlagOn();
    progress->RegisterInternalFilter(inputPadder, 0.2f * progressWeight);
    remainingProgress -= 0.2f;
    inputPadder->Update();
    kernelPaddedInput = inputPadder->GetOutput();
  }
  else
  {
    kernelPaddedInput = input;
  }

  // Crop to region of interest to minimize FFT requested region size.
  // This blocks forward FFT from requesting the entire input image
  // when only a subregion (output requested region + kernel radius padding)
  // is required for convolution to succeed.
  // TODO: Improve readability by keeping the output index as-is rather than resetting to 0, if the filter allows it

  const InputImageType * regionOfInterestImage;
  InputRegionType        regionOfInterest;
  InputSizeType          regionOfInterestSize;
  InputIndexType         regionOfInterestIndex;

  for (unsigned int dim = 0; dim < ImageDimension; ++dim)
  {
    regionOfInterestSize[dim] = outputRequestedSize[dim] + 2 * kernelRadius[dim];
    regionOfInterestIndex[dim] = outputRequestedIndex[dim] - kernelRadius[dim];
  }
  regionOfInterest = InputRegionType(regionOfInterestIndex, regionOfInterestSize);

  if (outputRequestedRegion != inputLargestRegion)
  {

    using CropFilterType = RegionOfInterestImageFilter<InputImageType, InputImageType>;
    auto cropFilter = CropFilterType::New();
    cropFilter->SetInput(kernelPaddedInput);
    cropFilter->SetRegionOfInterest(regionOfInterest);
    cropFilter->ReleaseDataFlagOn();
    progress->RegisterInternalFilter(cropFilter, 0.1f * progressWeight);
    remainingProgress -= 0.1f;

    // Shift region of interest back to original index
    // so that filter output is physically correct
    using InfoFilterType = ChangeInformationImageFilter<InputImageType>;
    using InfoOffsetValueType = typename InfoFilterType::OutputImageOffsetValueType;
    auto inputInfoFilter = InfoFilterType::New();
    inputInfoFilter->SetInput(cropFilter->GetOutput());
    inputInfoFilter->ChangeRegionOn();
    InfoOffsetValueType offset[ImageDimension];
    for (unsigned int dim = 0; dim < ImageDimension; ++dim)
    {
      offset[dim] = regionOfInterestIndex[dim];
    }
    inputInfoFilter->SetOutputOffset(offset);
    inputInfoFilter->ReleaseDataFlagOn();
    inputInfoFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
    progress->RegisterInternalFilter(inputInfoFilter, 0.001f * progressWeight);
    remainingProgress -= 0.001f;
    inputInfoFilter->Update();
    regionOfInterestImage = inputInfoFilter->GetOutput();
  }
  else
  {
    regionOfInterestImage = kernelPaddedInput;
  }

  // Pad for FFT so that each image side is factorable by at most
  // the specified greatest prime factor.
  // Note that FFT padding is always taken from the boundary condition
  // to avoid introducing extra information to FFT convolution vs spatial convolution
  using FFTPadFilterType = itk::FFTPadImageFilter<InputImageType, InputImageType>;
  auto fftPadder = FFTPadFilterType::New();
  fftPadder->SetInput(regionOfInterestImage);
  fftPadder->SetSizeGreatestPrimeFactor(m_SizeGreatestPrimeFactor);
  fftPadder->SetBoundaryCondition(this->GetBoundaryCondition());
  fftPadder->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
  fftPadder->ReleaseDataFlagOn();
  progress->RegisterInternalFilter(fftPadder, 0.199f * progressWeight);
  remainingProgress -= 0.199f;
  fftPadder->Update();

  // Save FFT padding size to assist in output cropping
  InputSizeType fftPaddedSize = fftPadder->GetOutput()->GetLargestPossibleRegion().GetSize();
  for (unsigned int dim = 0; dim < ImageDimension; ++dim)
  {
    m_FFTPadSize[dim] = fftPaddedSize[dim] - regionOfInterestSize[dim];
  }

  // 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.
  // TODO: Casting has been observed to take the bulk of image prep time, up to 75%.
  //       Optimize to avoid unnecessary casting and copying of memory.
  using InputCastFilterType = CastImageFilter<InputImageType, InternalImageType>;
  auto inputCaster = InputCastFilterType::New();
  inputCaster->InPlaceOn();
  inputCaster->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
  inputCaster->SetInput(fftPadder->GetOutput());
  inputCaster->ReleaseDataFlagOn();
  progress->RegisterInternalFilter(inputCaster, remainingProgress * progressWeight);
  inputCaster->Update();

  // Cache padded input size to use in kernel preparation and output cropping
  m_PaddedInputRegion = inputCaster->GetOutput()->GetLargestPossibleRegion();
  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.
  auto imageFFTFilter = FFTFilterType::New();
  imageFFTFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
  imageFFTFilter->SetInput(paddedInput);
  imageFFTFilter->ReleaseDataFlagOn();
  progress->RegisterInternalFilter(imageFFTFilter, progressWeight);
  imageFFTFilter->Update();

  transformedInput = imageFFTFilter->GetOutput();
  transformedInput->DisconnectPipeline();

  imageFFTFilter->SetInput(nullptr);
  imageFFTFilter = 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                      inputPadSize = m_PaddedInputRegion.GetSize();
  typename KernelImageType::SizeType kernelUpperBound;
  for (unsigned int i = 0; i < ImageDimension; ++i)
  {
    kernelUpperBound[i] = inputPadSize[i] - kernelSize[i];
  }

  InternalImagePointerType paddedKernelImage = nullptr;

  float paddingWeight = 0.2f;
  if (this->GetNormalize())
  {
    using NormalizeFilterType = NormalizeToConstantImageFilter<KernelImageType, InternalImageType>;
    auto normalizeFilter = NormalizeFilterType::New();
    normalizeFilter->SetConstant(NumericTraits<TInternalPrecision>::OneValue());
    normalizeFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
    normalizeFilter->SetInput(kernel);
    normalizeFilter->ReleaseDataFlagOn();
    progress->RegisterInternalFilter(normalizeFilter, 0.2f * paddingWeight * progressWeight);

    // Pad the kernel image with zeros.
    using KernelPadType = ConstantPadImageFilter<InternalImageType, InternalImageType>;
    using KernelPadPointer = typename KernelPadType::Pointer;
    KernelPadPointer kernelPadder = KernelPadType::New();
    kernelPadder->SetConstant(TInternalPrecision{});
    kernelPadder->SetPadUpperBound(kernelUpperBound);
    kernelPadder->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
    kernelPadder->SetInput(normalizeFilter->GetOutput());
    kernelPadder->ReleaseDataFlagOn();
    progress->RegisterInternalFilter(kernelPadder, 0.8f * paddingWeight * progressWeight);
    kernelPadder->Update();
    paddedKernelImage = kernelPadder->GetOutput();
  }
  else
  {
    // Pad the kernel image with zeros.
    using KernelPadType = ConstantPadImageFilter<KernelImageType, InternalImageType>;
    using KernelPadPointer = typename KernelPadType::Pointer;
    KernelPadPointer kernelPadder = KernelPadType::New();
    kernelPadder->SetConstant(TInternalPrecision{});
    kernelPadder->SetPadUpperBound(kernelUpperBound);
    kernelPadder->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
    kernelPadder->SetInput(kernel);
    kernelPadder->ReleaseDataFlagOn();
    progress->RegisterInternalFilter(kernelPadder, paddingWeight * progressWeight);
    paddedKernelImage = kernelPadder->GetOutput();
  }

  // Shift the padded kernel image.
  using KernelShiftFilterType = CyclicShiftImageFilter<InternalImageType, InternalImageType>;
  auto                                       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->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
  kernelShifter->SetInput(paddedKernelImage);
  kernelShifter->ReleaseDataFlagOn();
  progress->RegisterInternalFilter(kernelShifter, 0.1f * progressWeight);

  // Compute the kernel complex image
  auto kernelFFTFilter = FFTFilterType::New();
  kernelFFTFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
  kernelFFTFilter->SetInput(kernelShifter->GetOutput());
  progress->RegisterInternalFilter(kernelFFTFilter, 0.699f * progressWeight);
  kernelFFTFilter->Update();

  // Shift the kernel complex image in space so that it coincides with the
  // input complex image
  using InfoFilterType = ChangeInformationImageFilter<InternalComplexImageType>;
  auto kernelInfoFilter = InfoFilterType::New();
  kernelInfoFilter->ChangeRegionOn();

  using InfoOffsetValueType = typename InfoFilterType::OutputImageOffsetValueType;
  const InputIndexType &  inputIndex = m_PaddedInputRegion.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] - kernelIndex[i]);
  }
  kernelInfoFilter->SetOutputOffset(kernelOffset);
  kernelInfoFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
  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)
{
  // Transform the convolution result back into physical space
  auto ifftFilter = IFFTFilterType::New();
  ifftFilter->SetActualXDimensionIsOdd(this->GetXDimensionIsOdd());
  ifftFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
  ifftFilter->SetInput(paddedOutput);
  ifftFilter->ReleaseDataFlagOn();
  progress->RegisterInternalFilter(ifftFilter, 0.6f * progressWeight);
  ifftFilter->Update();

  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)
{
  InternalIndexType paddedOutputIndex = paddedOutput->GetLargestPossibleRegion().GetIndex();

  // Allocate the output
  this->AllocateOutputs();

  // Now crop the output to the desired size.
  using ExtractFilterType = ExtractImageFilter<InternalImageType, OutputImageType>;

  auto extractFilter = ExtractFilterType::New();
  extractFilter->InPlaceOn();
  extractFilter->GraftOutput(this->GetOutput());

  // Set up the crop sizes.
  InternalIndexType extractionIndex;
  for (unsigned int dim = 0; dim < ImageDimension; ++dim)
  {
    extractionIndex[dim] = paddedOutputIndex[dim] + m_FFTPadSize[dim] / 2 + GetKernelRadius()[dim];
  }
  auto               requestedRegionSize = this->GetOutput()->GetRequestedRegion().GetSize();
  InternalRegionType extractionRegion(extractionIndex, requestedRegionSize);
  extractFilter->SetExtractionRegion(extractionRegion);

  // Graft the minipipeline output to this filter.
  extractFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
  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>
auto
FFTConvolutionImageFilter<TInputImage, TKernelImage, TOutputImage, TInternalPrecision>::GetKernelRadius() const
  -> KernelSizeType
{
  KernelSizeType kernelSize = this->GetKernelImage()->GetLargestPossibleRegion().GetSize();
  KernelSizeType kernelRadius;
  for (unsigned int i = 0; i < ImageDimension; ++i)
  {
    kernelRadius[i] = kernelSize[i] / 2;
  }
  return kernelRadius;
}

template <typename TInputImage, typename TKernelImage, typename TOutputImage, typename TInternalPrecision>
bool
FFTConvolutionImageFilter<TInputImage, TKernelImage, TOutputImage, TInternalPrecision>::GetXDimensionIsOdd() const
{
  return (m_PaddedInputRegion.GetSize()[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;
}

} // namespace itk
#endif // itkFFTConvolutionImageFilter_hxx