Dilate a Binary Image#
See also
dilation; erosion
Synopsis#
Dilate regions by using a specified kernel, also known as a structuring element. In this example, the white regions are enlarged.
Results#
Input binary image. |
Dilated output image. |
Code#
Python#
#!/usr/bin/env python
import itk
import argparse
parser = argparse.ArgumentParser(description="Dilate A Binary Image.")
parser.add_argument("input_image")
parser.add_argument("output_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()
reader.SetFileName(args.input_image)
StructuringElementType = itk.FlatStructuringElement[Dimension]
structuringElement = StructuringElementType.Ball(args.radius)
DilateFilterType = itk.BinaryDilateImageFilter[
ImageType, ImageType, StructuringElementType
]
dilateFilter = DilateFilterType.New()
dilateFilter.SetInput(reader.GetOutput())
dilateFilter.SetKernel(structuringElement)
dilateFilter.SetForegroundValue(255)
WriterType = itk.ImageFileWriter[ImageType]
writer = WriterType.New()
writer.SetFileName(args.output_image)
writer.SetInput(dilateFilter.GetOutput())
writer.Update()
C++#
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkFlatStructuringElement.h"
#include "itkBinaryDilateImageFilter.h"
int
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;
radius.Fill(radiusValue);
StructuringElementType structuringElement = StructuringElementType::Ball(radius);
using BinaryDilateImageFilterType = itk::BinaryDilateImageFilter<ImageType, ImageType, StructuringElementType>;
auto dilateFilter = BinaryDilateImageFilterType::New();
dilateFilter->SetInput(input);
dilateFilter->SetKernel(structuringElement);
dilateFilter->SetForegroundValue(255); // Value to dilate
try
{
itk::WriteImage(dilateFilter->GetOutput(), outputImage);
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Classes demonstrated#
-
template<typename TInputImage, typename TOutputImage, typename TKernel>
class BinaryDilateImageFilter : public itk::BinaryMorphologyImageFilter<TInputImage, TOutputImage, TKernel> Fast binary dilation of a single intensity value in the image.
BinaryDilateImageFilter is a binary dilation morphologic operation on the foreground of an image. Only the value designated by the intensity value “SetForegroundValue()” (alias as SetDilateValue()) is considered as foreground, and other intensity values are considered background.
Grayscale images can be processed as binary images by selecting a “ForegroundValue” (alias “DilateValue”). Pixel values matching the dilate 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 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.
- See
ImageToImageFilter BinaryErodeImageFilter BinaryMorphologyImageFilter
- ITK Sphinx Examples:
Subclassed by itk::FastIncrementalBinaryDilateImageFilter< TInputImage, TOutputImage, TKernel >
-
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.
- See
Neighborhood
- See
MorphologyImageFilter
- See
BinaryDilateImageFilter
- See
BinaryErodeImageFilter
- ITK Sphinx Examples: