Erode a Binary Image#

See also

erosion; dilation


Erode regions by using a specified kernel, also known as a structuring element. In this example, the white regions shrink.


Input yin-yang image.

Input binary image.

Eroded output.

Eroded output image.



#!/usr/bin/env python

import itk
import argparse

parser = argparse.ArgumentParser(description="Erode A Binary Image.")
parser.add_argument("radius", type=int)
args = parser.parse_args()

PixelType = itk.UC
Dimension = 2

ImageType = itk.Image[PixelType, Dimension]

ReaderType = itk.ImageFileReader[ImageType]
reader = ReaderType.New()

StructuringElementType = itk.FlatStructuringElement[Dimension]
structuringElement = StructuringElementType.Ball(args.radius)

ErodeFilterType = itk.BinaryErodeImageFilter[
    ImageType, ImageType, StructuringElementType
erodeFilter = ErodeFilterType.New()
erodeFilter.SetForegroundValue(255)  # Intensity value to erode
erodeFilter.SetBackgroundValue(0)  # Replacement value for eroded voxels

WriterType = itk.ImageFileWriter[ImageType]
writer = WriterType.New()



#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkBinaryErodeImageFilter.h"
#include "itkFlatStructuringElement.h"

main(int argc, char * argv[])
  if (argc < 4)
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " <inputImage> <outputImage> <radius>";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  const char *       inputImage = argv[1];
  const char *       outputImage = argv[2];
  const unsigned int radiusValue = std::stoi(argv[3]);

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

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

  using StructuringElementType = itk::FlatStructuringElement<Dimension>;
  StructuringElementType::RadiusType radius;
  StructuringElementType structuringElement = StructuringElementType::Ball(radius);

  using BinaryErodeImageFilterType = itk::BinaryErodeImageFilter<ImageType, ImageType, StructuringElementType>;

  auto erodeFilter = BinaryErodeImageFilterType::New();
  erodeFilter->SetForegroundValue(255); // Intensity value to erode
  erodeFilter->SetBackgroundValue(0);   // Replacement value for eroded voxels

    itk::WriteImage(erodeFilter->GetOutput(), outputImage);
  catch (const itk::ExceptionObject & e)
    std::cerr << "Error: " << e << std::endl;
    return EXIT_FAILURE;

  return EXIT_SUCCESS;

Classes demonstrated#

template<typename TInputImage, typename TOutputImage, typename TKernel>
class BinaryErodeImageFilter : public itk::BinaryMorphologyImageFilter<TInputImage, TOutputImage, TKernel>

Fast binary erosion of a single intensity value in the image.

BinaryErodeImageFilter is a binary erosion morphologic operation on the foreground of an image. Only the value designated by the intensity value “SetForegroundValue()” (alias as SetErodeValue()) is considered as foreground, and other intensity values are considered background.

Grayscale images can be processed as binary images by selecting a “ForegroundValue” (alias “ErodeValue”). Pixel values matching the erode value are considered the “foreground” and all other pixels are “background”. This is useful in processing segmented images where all pixels in segment #1 have value 1 and pixels in segment #2 have value 2, etc. A particular “segment number” can be processed. ForegroundValue defaults to the maximum possible value of the PixelType. The eroded pixels will receive the BackgroundValue (defaults to NumericTraits::NonpositiveMin() ).

The structuring element is assumed to be composed of binary values (zero or one). Only elements of the structuring element having values > 0 are candidates for affecting the center pixel. A reasonable choice of structuring element is itk::BinaryBallStructuringElement.

This implementation is based on the papers:

L.Vincent “Morphological transformations of binary images with

arbitrary structuring elements”, and

N.Nikopoulos et al. “An efficient algorithm for 3d binary morphological transformations with 3d structuring elements for arbitrary size and shape”. IEEE Transactions on Image Processing. Vol. 9. No. 3. 2000. pp. 283-286.


ImageToImageFilter BinaryDilateImageFilter BinaryMorphologyImageFilter

ITK Sphinx Examples:

See itk::BinaryErodeImageFilter for additional documentation.
template<typename TPixel, unsigned int VDimension = 2, typename TAllocator = NeighborhoodAllocator<TPixel>>
class BinaryBallStructuringElement : public itk::Neighborhood<TPixel, VDimension, TAllocator>

A Neighborhood that represents a ball structuring element (ellipsoid) with binary elements.

This class defines a Neighborhood whose elements are either off or on depending on whether they are outside or inside an ellipsoid whose radii match the radii of the Neighborhood. This class can be used as a structuring element for the Morphology image filters.

A BinaryBallStructuringElement has an N-dimensional radius. The radius is defined separately for each dimension as the number of pixels that the neighborhood extends outward from the center pixel. For example, a 2D BinaryBallStructuringElement object with a radius of 2x3 has sides of length 5x7.

BinaryBallStructuringElement objects always have an unambiguous center because their side lengths are always odd.

Internally, this class carries out all of its computations using the FlatStructuringElement. It is preferable to use that class instead of this one because FlatStructuringElement is more flexible.









ITK Sphinx Examples:

See itk::BinaryBallStructuringElement for additional documentation.