Computes Smoothing With Gaussian Kernel#

Synopsis#

Computes the smoothing of an image by convolution with Gaussian kernels.

Results#

Input image

Input image#

Output image

Output image#

Code#

Python#

#!/usr/bin/env python

import itk
import argparse

parser = argparse.ArgumentParser(description="Computes Smoothing With Gaussian Kernel.")
parser.add_argument("input_image")
parser.add_argument("output_image")
parser.add_argument("sigma", type=float)
args = parser.parse_args()

PixelType = itk.UC
Dimension = 2

ImageType = itk.Image[PixelType, Dimension]

reader = itk.ImageFileReader[ImageType].New()
reader.SetFileName(args.input_image)

smoothFilter = itk.SmoothingRecursiveGaussianImageFilter[ImageType, ImageType].New()
smoothFilter.SetInput(reader.GetOutput())
smoothFilter.SetSigma(args.sigma)

writer = itk.ImageFileWriter[ImageType].New()
writer.SetFileName(args.output_image)
writer.SetInput(smoothFilter.GetOutput())

writer.Update()

C++#

#include "itkSmoothingRecursiveGaussianImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

int
main(int argc, char * argv[])
{
  if (argc != 4)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " <InputImageFile> <OutputImageFile> <sigma>" << std::endl;
    return EXIT_FAILURE;
  }

  constexpr unsigned int Dimension = 2;

  const char * inputFileName = argv[1];
  const char * outputFileName = argv[2];
  const float  sigmaValue = std::stod(argv[3]);

  using PixelType = unsigned char;
  using ImageType = itk::Image<PixelType, Dimension>;

  const auto input = itk::ReadImage<ImageType>(inputFileName);

  using FilterType = itk::SmoothingRecursiveGaussianImageFilter<ImageType, ImageType>;
  auto smoothFilter = FilterType::New();

  smoothFilter->SetSigma(sigmaValue);
  smoothFilter->SetInput(input);

  try
  {
    itk::WriteImage(smoothFilter->GetOutput(), outputFileName);
  }
  catch (const itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInputImage, typename TOutputImage = TInputImage>
class SmoothingRecursiveGaussianImageFilter : public itk::InPlaceImageFilter<TInputImage, TOutputImage>

Computes the smoothing of an image by convolution with the Gaussian kernels implemented as IIR filters.

This filter is implemented using the recursive gaussian filters. For multi-component images, the filter works on each component independently.

For this filter to be able to run in-place the input and output image types need to be the same and/or the same type as the RealImageType.

See itk::SmoothingRecursiveGaussianImageFilter for additional documentation.