ITK  5.1.0
Insight Toolkit
SphinxExamples/src/Filtering/FFT/FilterImageInFourierDomain/Code.cxx
/*=========================================================================
*
* 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.
*
*=========================================================================*/
int
main(int argc, char * argv[])
{
if (argc < 3)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <OutputFileName>";
std::cerr << " [Sigma]";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
double sigmaValue = 0.5;
if (argc > 3)
{
sigmaValue = std::stod(argv[3]);
}
constexpr unsigned int Dimension = 3;
using PixelType = float;
using RealImageType = itk::Image<PixelType, Dimension>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
// Some FFT filter implementations, like VNL's, need the image size to be a
// multiple of small prime numbers.
PadFilterType::Pointer padFilter = PadFilterType::New();
padFilter->SetInput(reader->GetOutput());
// Input size is [48, 62, 42]. Pad to [48, 64, 48].
padding[0] = 0;
padding[1] = 2;
padding[2] = 6;
padFilter->SetPadUpperBound(padding);
using ForwardFFTFilterType = itk::ForwardFFTImageFilter<RealImageType>;
using ComplexImageType = ForwardFFTFilterType::OutputImageType;
ForwardFFTFilterType::Pointer forwardFFTFilter = ForwardFFTFilterType::New();
forwardFFTFilter->SetInput(padFilter->GetOutput());
try
{
forwardFFTFilter->UpdateOutputInformation();
}
catch (itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
// A Gaussian is used here to create a low-pass filter.
using GaussianSourceType = itk::GaussianImageSource<RealImageType>;
GaussianSourceType::Pointer gaussianSource = GaussianSourceType::New();
gaussianSource->SetNormalized(true);
ComplexImageType::ConstPointer transformedInput = forwardFFTFilter->GetOutput();
const ComplexImageType::RegionType inputRegion(transformedInput->GetLargestPossibleRegion());
const ComplexImageType::SizeType inputSize = inputRegion.GetSize();
const ComplexImageType::SpacingType inputSpacing = transformedInput->GetSpacing();
const ComplexImageType::PointType inputOrigin = transformedInput->GetOrigin();
const ComplexImageType::DirectionType inputDirection = transformedInput->GetDirection();
gaussianSource->SetSize(inputSize);
gaussianSource->SetSpacing(inputSpacing);
gaussianSource->SetOrigin(inputOrigin);
gaussianSource->SetDirection(inputDirection);
GaussianSourceType::ArrayType sigma;
sigma.Fill(sigmaValue);
for (unsigned int ii = 0; ii < Dimension; ++ii)
{
const double halfLength = inputSize[ii] * inputSpacing[ii] / 2.0;
sigma[ii] *= halfLength;
mean[ii] = inputOrigin[ii] + halfLength;
}
mean = inputDirection * mean;
gaussianSource->SetSigma(sigma);
gaussianSource->SetMean(mean);
FFTShiftFilterType::Pointer fftShiftFilter = FFTShiftFilterType::New();
fftShiftFilter->SetInput(gaussianSource->GetOutput());
MultiplyFilterType::Pointer multiplyFilter = MultiplyFilterType::New();
multiplyFilter->SetInput1(forwardFFTFilter->GetOutput());
multiplyFilter->SetInput2(fftShiftFilter->GetOutput());
InverseFilterType::Pointer inverseFFTFilter = InverseFilterType::New();
inverseFFTFilter->SetInput(multiplyFilter->GetOutput());
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFileName);
writer->SetInput(inverseFFTFilter->GetOutput());
try
{
writer->Update();
}
catch (itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
itk::GTest::TypedefsAndConstructors::Dimension2::DirectionType
ImageBaseType::DirectionType DirectionType
Definition: itkGTestTypedefsAndConstructors.h:52
itkInverseFFTImageFilter.h
itk::GTest::TypedefsAndConstructors::Dimension2::PointType
ImageBaseType::PointType PointType
Definition: itkGTestTypedefsAndConstructors.h:51
itkImageFileReader.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::ForwardFFTImageFilter
Base class for forward Fast Fourier Transform.
Definition: itkForwardFFTImageFilter.h:61
itk::WrapPadImageFilter
Increase the image size by padding with replicants of the input image value.
Definition: itkWrapPadImageFilter.h:53
itkWrapPadImageFilter.h
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itkForwardFFTImageFilter.h
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:87
itkFFTShiftImageFilter.h
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itkMultiplyImageFilter.h
itk::InverseFFTImageFilter
Base class for inverse Fast Fourier Transform.
Definition: itkInverseFFTImageFilter.h:49
itk::GaussianImageSource
Generate an n-dimensional image of a Gaussian.
Definition: itkGaussianImageSource.h:44
itkImageFileWriter.h
itk::FFTShiftImageFilter
Shift the zero-frequency components of a Fourier transform to the center of the image.
Definition: itkFFTShiftImageFilter.h:48
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itkGaussianImageSource.h
itk::MultiplyImageFilter
Pixel-wise multiplication of two images.
Definition: itkMultiplyImageFilter.h:43
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::FixedArray::Fill
void Fill(const ValueType &)
itk::Size::GetSize
const SizeValueType * GetSize() const
Definition: itkSize.h:169