ImageDev

DistanceMap2d

Computes the distance map of objects in a two-dimensional binary image.

Access to parameter description

A distance map, also known as distance transform, is a representation of an image where each pixel value corresponds to its distance to the nearest boundary pixel in a given metric.

Two metrics are commonly used: In the output image $O$, a pixel $x$ belonging to a particle $X$, takes an intensity $I$ equal to the distance to the boundary.

In the case of the Chessboard metric, the distance is given by: $$ I=d_ C(x,\delta(X)) $$ Where: $$ d_C((i,j),(h,k))=max(|h-i|,|k-j|) $$ The weight used for each neighbor configuration can be user defined. Some optimizations are automatically performed when the weights set correspond to a Chamfer or a Chessboard distance map.

Distance maps are computed on binary images either on object pixels (foreground) or on the background.
The output image can have either a 16-bit signed integer or a 32-bit float type.

<b> Figure 1.</b> Output image for chessboard distance transform in inside mode
Figure 1. Output image for chessboard distance transform in inside mode


<b> Figure 2.</b> Output image for chamfer distance transform in inside mode
Figure 2. Output image for chamfer distance transform in inside mode


Note: This algorithm does not take into account the input image calibration. The output values are systematically indicating distance in pixel units.

See also

Function Syntax

This function returns outputMapImage.
// Function prototype
std::shared_ptr< iolink::ImageView > distanceMap2d( std::shared_ptr< iolink::ImageView > inputBinaryImage, DistanceMap2d::MappingMode mappingMode, DistanceMap2d::BorderCondition borderCondition, double edgeDistance, double cornerDistance, DistanceMap2d::OutputType outputType, std::shared_ptr< iolink::ImageView > outputMapImage = NULL );
This function returns outputMapImage.
// Function prototype.
distance_map_2d( input_binary_image,
                 mapping_mode = DistanceMap2d.MappingMode.INSIDE,
                 border_condition = DistanceMap2d.BorderCondition.MIRROR,
                 edge_distance = 1,
                 corner_distance = 1.41421,
                 output_type = DistanceMap2d.OutputType.SIGNED_INTEGER_16_BIT,
                 output_map_image = None )
This function returns outputMapImage.
// Function prototype.
public static IOLink.ImageView
DistanceMap2d( IOLink.ImageView inputBinaryImage,
               DistanceMap2d.MappingMode mappingMode = ImageDev.DistanceMap2d.MappingMode.INSIDE,
               DistanceMap2d.BorderCondition borderCondition = ImageDev.DistanceMap2d.BorderCondition.MIRROR,
               double edgeDistance = 1,
               double cornerDistance = 1.41421,
               DistanceMap2d.OutputType outputType = ImageDev.DistanceMap2d.OutputType.SIGNED_INTEGER_16_BIT,
               IOLink.ImageView outputMapImage = null );

Class Syntax

Parameters

Class Name DistanceMap2d

Parameter Name Description Type Supported Values Default Value
input
inputBinaryImage
The binary input image.
Background is represented by zero values while objects are represented by one values.
Image Binary nullptr
input
mappingMode
The mode defining where the distance field is computed.
INSIDE Distances are computed for pixels inside the objects (having an intensity equal to 1). Outside pixels remain to zero.
OUTSIDE Distances are computed for pixels outside of objects (having an intensity equal to 0). Inside pixels are set to zero.
Enumeration INSIDE
input
borderCondition
The mode defining the border conditions.
ZERO Pixels outside of the image are considered to have the value 0.
MIRROR Pixels outside of the image are considered as an extrapolation of the image pixels with a mirror border condition.
Enumeration MIRROR
input
edgeDistance
The distance weight between 2 pixels having an edge connection.
The value must be strictly positive.
Float64 >0 1
input
cornerDistance
The distance weight between 2 pixels having a corner connection.
The value must be greater than or equal to edgeDistance.
Float64 Any value 1.41421
input
outputType
The output image type to provide.
SIGNED_INTEGER_16_BIT The output image type is 16-bit signed integer.
FLOAT_32_BIT The output image type is 32-bit float.
Enumeration SIGNED_INTEGER_16_BIT
output
outputMapImage
The output distance map image.
The distance map of the shape represented by the input image. Each voxel is assigned to a value corresponding to the distance to the nearest object boundary. Its dimensions are forced to the same values as the input. Its data type is defined by the outputType parameter.
Image nullptr

Object Examples

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

DistanceMap2d distanceMap2dAlgo;
distanceMap2dAlgo.setInputBinaryImage( polystyrene_sep );
distanceMap2dAlgo.setMappingMode( DistanceMap2d::MappingMode::INSIDE );
distanceMap2dAlgo.setBorderCondition( DistanceMap2d::BorderCondition::MIRROR );
distanceMap2dAlgo.setEdgeDistance( 1 );
distanceMap2dAlgo.setCornerDistance( 1.4142135623730951 );
distanceMap2dAlgo.setOutputType( DistanceMap2d::OutputType::SIGNED_INTEGER_16_BIT );
distanceMap2dAlgo.execute();

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

distance_map_2d_algo = imagedev.DistanceMap2d()
distance_map_2d_algo.input_binary_image = polystyrene_sep
distance_map_2d_algo.mapping_mode = imagedev.DistanceMap2d.INSIDE
distance_map_2d_algo.border_condition = imagedev.DistanceMap2d.MIRROR
distance_map_2d_algo.edge_distance = 1
distance_map_2d_algo.corner_distance = 1.4142135623730951
distance_map_2d_algo.output_type = imagedev.DistanceMap2d.SIGNED_INTEGER_16_BIT
distance_map_2d_algo.execute()

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

DistanceMap2d distanceMap2dAlgo = new DistanceMap2d
{
    inputBinaryImage = polystyrene_sep,
    mappingMode = DistanceMap2d.MappingMode.INSIDE,
    borderCondition = DistanceMap2d.BorderCondition.MIRROR,
    edgeDistance = 1,
    cornerDistance = 1.4142135623730951,
    outputType = DistanceMap2d.OutputType.SIGNED_INTEGER_16_BIT
};
distanceMap2dAlgo.Execute();

Console.WriteLine( "outputMapImage:" + distanceMap2dAlgo.outputMapImage.ToString() );

Function Examples

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

auto result = distanceMap2d( polystyrene_sep, DistanceMap2d::MappingMode::INSIDE, DistanceMap2d::BorderCondition::MIRROR, 1, 1.4142135623730951, DistanceMap2d::OutputType::SIGNED_INTEGER_16_BIT );

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

result = imagedev.distance_map_2d( polystyrene_sep, imagedev.DistanceMap2d.INSIDE, imagedev.DistanceMap2d.MIRROR, 1, 1.4142135623730951, imagedev.DistanceMap2d.SIGNED_INTEGER_16_BIT )

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

IOLink.ImageView result = Processing.DistanceMap2d( polystyrene_sep, DistanceMap2d.MappingMode.INSIDE, DistanceMap2d.BorderCondition.MIRROR, 1, 1.4142135623730951, DistanceMap2d.OutputType.SIGNED_INTEGER_16_BIT );

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