ImageDev

Labeling2d

Assigns the same label to each connected component of a two-dimensional binary or label image.

Access to parameter description

This algorithm scans the input image from top to bottom and left to right.
Each pixel of the same connected component (or object) takes the same value, and each object is assigned to a different value, starting from value 1. The assigned gray level (or label) depends on the location of the object in the image. The maximum gray level value gives the total number of objects in the original binary or label image, which means that all objects have consecutive indices.

A label image may be 8, 16 or 32 bits per pixel.
By default the output image type is defined by the labelType parameter, but if the number of labels exceeds its capacity, then the output image is automatically converted to the upper type to be able to manage the number of labels. To optimize the computation, it is important to select a relevant type for the output. Selecting an 8-bit type for an image containing more than 65535 objects will produce the expected result, but will compute it in much more time than by selecting a 32-bit output type directly.

The type of connectivity to define the connected components is managed by the neighborhood parameter.

See also
See related examples

Function Syntax

This function returns the outputLabelImage output parameter.
// Function prototype.
std::shared_ptr< iolink::ImageView >
labeling2d( std::shared_ptr< iolink::ImageView > inputObjectImage,
            Labeling2d::LabelType labelType,
            Labeling2d::Neighborhood neighborhood,
            std::shared_ptr< iolink::ImageView > outputLabelImage = NULL );
This function returns the outputLabelImage output parameter.
// Function prototype.
labeling_2d( input_object_image,
             label_type = Labeling2d.LabelType.LABEL_16_BIT,
             neighborhood = Labeling2d.Neighborhood.CONNECTIVITY_8,
             output_label_image = None )
This function returns the outputLabelImage output parameter.
// Function prototype.
public static IOLink.ImageView
Labeling2d( IOLink.ImageView inputObjectImage,
            Labeling2d.LabelType labelType = ImageDev.Labeling2d.LabelType.LABEL_16_BIT,
            Labeling2d.Neighborhood neighborhood = ImageDev.Labeling2d.Neighborhood.CONNECTIVITY_8,
            IOLink.ImageView outputLabelImage = null );

Class Syntax

Parameters

Class Name Labeling2d

Parameter Name Description Type Supported Values Default Value
input
inputObjectImage
The input binary or label image. Image Binary or Label nullptr
input
labelType
The minimum output data type. Automatically changed if not sufficient to encode all labels.
LABEL_8_BIT The minimum output data type is 8-bit per pixel.
LABEL_16_BIT The minimum output data type is 16-bit per pixel.
LABEL_32_BIT The minimum output data type is 32-bit per pixel.
Enumeration LABEL_16_BIT
input
neighborhood
The 2D neighborhood configuration defining the connected components.
CONNECTIVITY_4 Connected components are defined by a 4-neighbor connectivity. The connectivity pattern is a cross.
CONNECTIVITY_8 Connected components are defined by a 8-neighbor connectivity. The connectivity pattern is a square.
Enumeration CONNECTIVITY_8
output
outputLabelImage
The output label image. Its dimensions are forced to the same values as the input image. Its type depends on the number of objects and the labelType parameter. Image nullptr

Object Examples

auto polystyrene_sep = readVipImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene_sep.vip" );

Labeling2d labeling2dAlgo;
labeling2dAlgo.setInputObjectImage( polystyrene_sep );
labeling2dAlgo.setLabelType( Labeling2d::LabelType::LABEL_8_BIT );
labeling2dAlgo.setNeighborhood( Labeling2d::Neighborhood::CONNECTIVITY_8 );
labeling2dAlgo.execute();

std::cout << "outputLabelImage:" << labeling2dAlgo.outputLabelImage()->toString();
polystyrene_sep = imagedev.read_vip_image(imagedev_data.get_image_path("polystyrene_sep.vip"))

labeling_2d_algo = imagedev.Labeling2d()
labeling_2d_algo.input_object_image = polystyrene_sep
labeling_2d_algo.label_type = imagedev.Labeling2d.LABEL_8_BIT
labeling_2d_algo.neighborhood = imagedev.Labeling2d.CONNECTIVITY_8
labeling_2d_algo.execute()

print( "output_label_image:", str( labeling_2d_algo.output_label_image ) )
ImageView polystyrene_sep = Data.ReadVipImage( @"Data/images/polystyrene_sep.vip" );

Labeling2d labeling2dAlgo = new Labeling2d
{
    inputObjectImage = polystyrene_sep,
    labelType = Labeling2d.LabelType.LABEL_8_BIT,
    neighborhood = Labeling2d.Neighborhood.CONNECTIVITY_8
};
labeling2dAlgo.Execute();

Console.WriteLine( "outputLabelImage:" + labeling2dAlgo.outputLabelImage.ToString() );

Function Examples

auto polystyrene_sep = readVipImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene_sep.vip" );

auto result = labeling2d( polystyrene_sep, Labeling2d::LabelType::LABEL_8_BIT, Labeling2d::Neighborhood::CONNECTIVITY_8 );

std::cout << "outputLabelImage:" << result->toString();
polystyrene_sep = imagedev.read_vip_image(imagedev_data.get_image_path("polystyrene_sep.vip"))

result = imagedev.labeling_2d( polystyrene_sep, imagedev.Labeling2d.LABEL_8_BIT, imagedev.Labeling2d.CONNECTIVITY_8 )

print( "output_label_image:", str( result ) )
ImageView polystyrene_sep = Data.ReadVipImage( @"Data/images/polystyrene_sep.vip" );

IOLink.ImageView result = Processing.Labeling2d( polystyrene_sep, Labeling2d.LabelType.LABEL_8_BIT, Labeling2d.Neighborhood.CONNECTIVITY_8 );

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