Compute Inverse FFT of Image#

Synopsis#

Compute the inverse FFT of an image.

Results#

ifft.png

ifft.png#

Code#

Python#

#!/usr/bin/env python

import itk
import argparse

parser = argparse.ArgumentParser(description="Compute Inverse FFT Of Image.")
parser.add_argument("input_image", nargs="?")
args = parser.parse_args()

dimension = 2
float_image_type = itk.Image[itk.F, dimension]

if not args.input_image:
    corner = itk.Index[dimension]()
    corner.Fill(0)

    size = itk.Size[dimension]()
    size.Fill(200)

    region = itk.ImageRegion[dimension]()
    region.SetIndex(corner)
    region.SetSize(size)

    image = float_image_type.New(Regions=region)
    image.Allocate()
    image.FillBuffer(0)

    # Make a square
    image[40:100, 40:100] = 100

else:
    image = itk.imread(args.input_image, pixel_type=itk.F)

# Define some types
unsigned_char_image_type = itk.Image[itk.UC, dimension]

# Compute the FFT
image = itk.forward_fft_image_filter(image)

# Compute the IFFT
image = itk.inverse_fft_image_filter(image)

image = itk.cast_image_filter(image, ttype=(float_image_type, unsigned_char_image_type))

itk.imwrite(image, "ComputeInverseFFTOfImagePython.png")

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
  {
    image = itk::ReadImage<FloatImageType>(argv[1]);
  }

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

  // Compute the FFT
  using FFTType = itk::ForwardFFTImageFilter<FloatImageType>;
  auto 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>;
  auto ifftFilter = IFFTType::New();
  ifftFilter->SetInput(fftFilter->GetOutput());
  ifftFilter->Update();

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

  itk::WriteImage(castFilter->GetOutput(), "ifft.png");

  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.