ImageDev

Opening2d

Performs a two-dimensional opening using a structuring element matching with a square or a cross.

Access to parameter description

For an introduction: This algorithm successively runs an Erosion2d and a Dilation2d with the same kernel.
The kernelRadius parameter tunes the number of iterations of each operation, which sets the kernel size.
This algorithm uses a basic structuring element with either 8 neighbors, or 4 neighbors, according to the neighborhood parameter.

<b> Figure 1.</b> Structuring elements
Figure 1. Structuring elements

With a classic implementation, morphological opening systematically considers areas out of the image as a replication of the image borders at each step of the algorithm. Therefore, when applying an opening, some thin object parts cut by the image borders may be removed at the erosion step and not be restored after the dilation, while one would expect to keep them. The borderPolicy parameter manages this case. The default mode, LIMITED, corresponds to the classic behavior. The EXTENDED mode properly manages image borders by extending them by a size equal to the structuring element's. Therefore, this mode can be slower and more memory consuming, especially when the structuring element size is high.

<b> (a) </b>
(a)
<b> (b) </b>
(b)
<b> (c) </b>
(c)
Figure 2. Opening and border policy: (a) The binary input image,
(b) opening of size 5 with limited border policy, (c) same opening in extended mode


See also

Function Syntax

This function returns outputImage.
// Function prototype
std::shared_ptr< iolink::ImageView > opening2d( std::shared_ptr< iolink::ImageView > inputImage, uint32_t kernelRadius, Opening2d::Neighborhood neighborhood, Opening2d::BorderPolicy borderPolicy, std::shared_ptr< iolink::ImageView > outputImage = NULL );
This function returns outputImage.
// Function prototype.
opening_2d( input_image,
            kernel_radius = 3,
            neighborhood = Opening2d.Neighborhood.CONNECTIVITY_8,
            border_policy = Opening2d.BorderPolicy.LIMITED,
            output_image = None )
This function returns outputImage.
// Function prototype.
public static IOLink.ImageView
Opening2d( IOLink.ImageView inputImage,
           UInt32 kernelRadius = 3,
           Opening2d.Neighborhood neighborhood = ImageDev.Opening2d.Neighborhood.CONNECTIVITY_8,
           Opening2d.BorderPolicy borderPolicy = ImageDev.Opening2d.BorderPolicy.LIMITED,
           IOLink.ImageView outputImage = null );

Class Syntax

Parameters

Class Name Opening2d

Parameter Name Description Type Supported Values Default Value
input
inputImage
The input image. The image type can be integer or float. Image Binary, Label, Grayscale or Multispectral nullptr
input
borderPolicy
The border policy to apply.
LIMITED The limited mode is faster to compute, but can produce unexpected results for particles close to the image border.
EXTENDED The Extended mode is slower to compute, but produces the expected results for particles close to the image border.
Enumeration LIMITED
input
kernelRadius
The number of iterations (the half size of the structuring element, in pixels). A square structuring element always has an odd side length (3x3, 5x5, etc.) which is defined by twice the kernel radius + 1. UInt32 >=1 3
input
neighborhood
The 2D neighborhood configuration.
CONNECTIVITY_4 The structuring element is a cross.
CONNECTIVITY_8 The structuring element is a square.
Enumeration CONNECTIVITY_8
output
outputImage
The output image. Its dimensions and type are forced to the same values as the input image. Image nullptr

Object Examples

std::shared_ptr< iolink::ImageView > polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );

Opening2d opening2dAlgo;
opening2dAlgo.setInputImage( polystyrene );
opening2dAlgo.setKernelRadius( 3 );
opening2dAlgo.setNeighborhood( Opening2d::Neighborhood::CONNECTIVITY_8 );
opening2dAlgo.setBorderPolicy( Opening2d::BorderPolicy::EXTENDED );
opening2dAlgo.execute();

std::cout << "outputImage:" << opening2dAlgo.outputImage()->toString();
polystyrene = ioformat.read_image(imagedev_data.get_image_path("polystyrene.tif"))

opening_2d_algo = imagedev.Opening2d()
opening_2d_algo.input_image = polystyrene
opening_2d_algo.kernel_radius = 3
opening_2d_algo.neighborhood = imagedev.Opening2d.CONNECTIVITY_8
opening_2d_algo.border_policy = imagedev.Opening2d.EXTENDED
opening_2d_algo.execute()

print( "output_image:", str( opening_2d_algo.output_image ) )
ImageView polystyrene = ViewIO.ReadImage( @"Data/images/polystyrene.tif" );

Opening2d opening2dAlgo = new Opening2d
{
    inputImage = polystyrene,
    kernelRadius = 3,
    neighborhood = Opening2d.Neighborhood.CONNECTIVITY_8,
    borderPolicy = Opening2d.BorderPolicy.EXTENDED
};
opening2dAlgo.Execute();

Console.WriteLine( "outputImage:" + opening2dAlgo.outputImage.ToString() );

Function Examples

std::shared_ptr< iolink::ImageView > polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );

auto result = opening2d( polystyrene, 3, Opening2d::Neighborhood::CONNECTIVITY_8, Opening2d::BorderPolicy::EXTENDED );

std::cout << "outputImage:" << result->toString();
polystyrene = ioformat.read_image(imagedev_data.get_image_path("polystyrene.tif"))

result = imagedev.opening_2d( polystyrene, 3, imagedev.Opening2d.CONNECTIVITY_8, imagedev.Opening2d.EXTENDED )

print( "output_image:", str( result ) )
ImageView polystyrene = ViewIO.ReadImage( @"Data/images/polystyrene.tif" );

IOLink.ImageView result = Processing.Opening2d( polystyrene, 3, Opening2d.Neighborhood.CONNECTIVITY_8, Opening2d.BorderPolicy.EXTENDED );

Console.WriteLine( "outputImage:" + result.ToString() );