Blurring an Image Using a Binomial Kernel#

Synopsis#

The BinomialBlurImageFilter computes a nearest neighbor average along each dimension. The process is repeated a number of times, as specified by the user. In principle, after a large number of iterations the result will approach the convolution with a Gaussian.

Results#

Code#

Python#

#!/usr/bin/env python

import itk
import argparse

parser = argparse.ArgumentParser(
    description="Blurring An Image Using A Binomial Kernel."
)
parser.add_argument("input_image")
parser.add_argument("output_image")
parser.add_argument("number_of_repetitions", type=int)
args = parser.parse_args()

InputPixelType = itk.F
OutputPixelType = itk.UC
Dimension = 2

InputImageType = itk.Image[InputPixelType, Dimension]
OutputImageType = itk.Image[OutputPixelType, Dimension]

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

binomialFilter = itk.BinomialBlurImageFilter.New(reader)
binomialFilter.SetRepetitions(args.number_of_repetitions)

rescaler = itk.RescaleIntensityImageFilter[InputImageType, OutputImageType].New()
rescaler.SetInput(binomialFilter.GetOutput())
rescaler.SetOutputMinimum(0)
rescaler.SetOutputMaximum(255)

writer = itk.ImageFileWriter[OutputImageType].New()
writer.SetFileName(args.output_image)
writer.SetInput(rescaler.GetOutput())

writer.Update()

C++#

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkBinomialBlurImageFilter.h"

int
main(int argc, char * argv[])
{
  if (argc < 4)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << "  inputImageFile  outputImageFile  numberOfRepetitions" << std::endl;
    return EXIT_FAILURE;
  }

  using InputPixelType = float;
  using OutputPixelType = float;
  using InputImageType = itk::Image<InputPixelType, 2>;
  using OutputImageType = itk::Image<OutputPixelType, 2>;

  const auto input = itk::ReadImage<InputImageType>(argv[1]);

  const unsigned int repetitions = std::stoi(argv[3]);

  using FilterType = itk::BinomialBlurImageFilter<InputImageType, OutputImageType>;
  auto filter = FilterType::New();
  filter->SetInput(input);
  filter->SetRepetitions(repetitions);
  filter->Update();

  using WritePixelType = unsigned char;
  using WriteImageType = itk::Image<WritePixelType, 2>;
  using RescaleFilterType = itk::RescaleIntensityImageFilter<OutputImageType, WriteImageType>;

  auto rescaler = RescaleFilterType::New();
  rescaler->SetOutputMinimum(0);
  rescaler->SetOutputMaximum(255);
  rescaler->SetInput(filter->GetOutput());

  itk::WriteImage(rescaler->GetOutput(), argv[2]);

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInputImage, typename TOutputImage>
class BinomialBlurImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>

Performs a separable blur on each dimension of an image.

The binomial blur consists of a nearest neighbor average along each image dimension. The net result after n-iterations approaches convolution with a gaussian.

ITK Sphinx Examples:

See itk::BinomialBlurImageFilter for additional documentation.