Compute Inverse FFT Of Image

Synopsis

Compute the inverse FFT of an image.

Results

ifft.png

ifft.png

Code

C++

#include "itkImage.h"
#include "itkForwardFFTImageFilter.h"
#include "itkInverseFFTImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkImageFileReader.h"
#include "itkCastImageFilter.h"
#include "itkImageFileWriter.h"

using FloatImageType = itk::Image<float, 2>;

static void
CreateImage(FloatImageType * const image);

int
main(int argc, char * argv[])
{
  FloatImageType::Pointer image;
  // Verify input
  if (argc < 2)
  {
    image = FloatImageType::New();
    CreateImage(image);
    // std::cerr << "Required: filename" << std::endl;
    // return EXIT_FAILURE;
  }
  else
  {
    // Read the image
    using ReaderType = itk::ImageFileReader<FloatImageType>;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName(argv[1]);
    reader->Update();

    image = reader->GetOutput();
  }

  // Define some types
  using UnsignedCharImageType = itk::Image<unsigned char, 2>;

  // Compute the FFT
  using FFTType = itk::ForwardFFTImageFilter<FloatImageType>;
  FFTType::Pointer fftFilter = FFTType::New();
  fftFilter->SetInput(image);
  fftFilter->Update();

  // Compute the IFFT
  // using IFFTType = itk::InverseFFTImageFilter<FFTType::OutputImageType, UnsignedCharImageType>; // This does not work
  // - output type seems to need to be float, but it is just an error, not a concept check error...
  using IFFTType = itk::InverseFFTImageFilter<FFTType::OutputImageType, FloatImageType>;
  IFFTType::Pointer ifftFilter = IFFTType::New();
  ifftFilter->SetInput(fftFilter->GetOutput());
  ifftFilter->Update();

  using CastFilterType = itk::CastImageFilter<FloatImageType, UnsignedCharImageType>;
  CastFilterType::Pointer castFilter = CastFilterType::New();
  castFilter->SetInput(ifftFilter->GetOutput());
  castFilter->Update();

  using WriterType = itk::ImageFileWriter<UnsignedCharImageType>;

  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName("ifft.png");
  writer->SetInput(castFilter->GetOutput());
  writer->Update();

  return EXIT_SUCCESS;
}

void
CreateImage(FloatImageType * const image)
{
  itk::Index<2> corner = { { 0, 0 } };

  itk::Size<2> size = { { 200, 200 } };

  itk::ImageRegion<2> region(corner, size);

  image->SetRegions(region);
  image->Allocate();

  // Make a square
  for (FloatImageType::IndexValueType r = 40; r < 100; r++)
  {
    for (FloatImageType::IndexValueType c = 40; c < 100; c++)
    {
      FloatImageType::IndexType pixelIndex = { { r, c } };

      image->SetPixel(pixelIndex, 100);
    }
  }
}

Classes demonstrated

template<typename TInputImage, typename TOutputImage = Image<typename TInputImage::PixelType::value_type, TInputImage::ImageDimension>>
class InverseFFTImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>

Base class for inverse Fast Fourier Transform.

This is a base class for the “inverse” or “reverse” Discrete Fourier Transform. This is an abstract base class: the actual implementation is provided by the best child available on the system when the object is created via the object factory system.

This class transforms a full complex image with Hermitian symmetry into its real spatial domain representation. If the input does not have Hermitian symmetry, the imaginary component is discarded.

See

ForwardFFTImageFilter, InverseFFTImageFilter

ITK Sphinx Examples:

Subclassed by itk::FFTWInverseFFTImageFilter< TInputImage, TOutputImage >, itk::VnlInverseFFTImageFilter< TInputImage, TOutputImage >

See itk::InverseFFTImageFilter for additional documentation.