Dilate a grayscale image using a functional kernel#
See also
dilation; erosion
Synopsis#
Dilate an image using functional grayscale morphology. Function dilation takes the maximum of all the pixels identified by the structuring element plus the structuring element value. In this example, the white regions are enlarged.
Results#
Input grayscale image.#
Dilated output.#
Code#
Python#
#!/usr/bin/env python
import itk
import argparse
itk.auto_progress(2)
parser = argparse.ArgumentParser(
description="Dilate an image using functional grayscale morphology."
)
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]
reader = itk.ImageFileReader[ImageType].New()
reader.SetFileName(args.input_image)
StructuringElementType = itk.FlatStructuringElement[Dimension]
structuringElement = StructuringElementType.Ball(args.radius)
grayscaleFilter = itk.GrayscaleFunctionDilateImageFilter[
ImageType, ImageType, structuringElement
].New()
grayscaleFilter.SetInput(reader.GetOutput())
grayscaleFilter.SetKernel(structuringElement)
writer = itk.ImageFileWriter[ImageType].New()
writer.SetFileName(args.output_image)
writer.SetInput(grayscaleFilter.GetOutput())
writer.Update()
C++#
#include "itkFlatStructuringElement.h"
#include "itkGrayscaleFunctionDilateImageFilter.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] << " <inputImage> <outputImage> <radius>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType, Dimension>;
using ReaderType = itk::ImageFileReader<ImageType>;
using WriterType = itk::ImageFileWriter<ImageType>;
auto reader = ReaderType::New();
auto writer = WriterType::New();
const char * inputImage = argv[1];
const char * outputImage = argv[2];
const unsigned int radiusValue = std::stoi(argv[3]);
reader->SetFileName(inputImage);
writer->SetFileName(outputImage);
using StructuringElementType = itk::FlatStructuringElement<Dimension>;
StructuringElementType::RadiusType radius;
radius.Fill(radiusValue);
StructuringElementType structuringElement = StructuringElementType::Ball(radius);
using FilterType = itk::GrayscaleFunctionDilateImageFilter<ImageType, ImageType, StructuringElementType>;
auto filter = FilterType::New();
filter->SetKernel(structuringElement);
filter->SetInput(reader->GetOutput());
writer->SetInput(filter->GetOutput());
try
{
writer->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Classes demonstrated#
-
template<typename TInputImage, typename TOutputImage, typename TKernel>
class GrayscaleFunctionDilateImageFilter : public itk::MorphologyImageFilter<TInputImage, TOutputImage, TKernel> Grayscale function dilation of an image.
Dilate an image using functional grayscale morphology. Function dilation takes the maximum of all the pixels identified by the structuring element plus the structuring element value.
The structuring element can be composed of arbitrary nonnegative values (not restricted to zero or one). Element values greater than zero indicate pixels that will be considered during the dilation. The function dilation operation is defined as the maximum over the element of the image value PLUS the structuring element value.
For the each input image pixel,
NeighborhoodIterator gives neighbors of the pixel.
Evaluate() member function returns the maximum value among the image neighbors plus the kernel value where the kernel has elements > 0.
Replace the original value with the max value.
See also
MorphologyImageFilter, GrayscaleDilateImageFilter, BinaryDilateImageFilter
-
template<typename TPixel, unsigned int VDimension = 2, typename TAllocator = NeighborhoodAllocator<TPixel>>
class BinaryBallStructuringElement : public itk::Neighborhood<TPixel, 2, NeighborhoodAllocator<TPixel>> 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 also
Neighborhood
See also
MorphologyImageFilter
See also
BinaryDilateImageFilter
See also
BinaryErodeImageFilter
- ITK Sphinx Examples:
