F_VN_DistanceTransformation

F_VN_DistanceTransformation 1:

Calculates the distance transformation, which is the distance to the closest zero pixel in a binary image.

Syntax

Definition:

FUNCTION F_VN_DistanceTransformation : HRESULT
VAR_INPUT
    ipSrcImage    : ITcVnImage;
    ipDestImage   : Reference To ITcVnImage;
    eDistanceType : ETcVnDistanceType;
    eMaskSize     : ETcVnDistanceTransformationMask;
    hrPrev        : HRESULT;
END_VAR

F_VN_DistanceTransformation 2: Inputs

Name

Type

Description

ipSrcImage

ITcVnImage

Source image (USINT, 1 channel)

ipDestImage

Reference To ITcVnImage

Destination image (REAL, 1 channel. An appropriate destination image will be created if required.)

eDistanceType

ETcVnDistanceType

Distance computation method (supported: L1, L2, C)

eMaskSize

ETcVnDistanceTransformationMask

Size of the distance transformation mask

hrPrev

HRESULT

HRESULT indicating the result of previous operations (If SUCCEEDED(hrPrev) equals false, no operation is executed.)

F_VN_DistanceTransformation 3: Return value

HRESULT

Further information

This function performs the distance transformation of an image. In addition, there is an expert version F_VN_DistanceTransformationExp with advanced parameterization options.

Parameter

The following parameters are available in the basic version:

Original image

A 1-channel 8-bit image (element type TCVN_ET_USINT) is expected for the original image ipSrcImage. Other image formats must first be converted.

Result image

The result image ipDestImage is returned with the element type TCVN_ET_REAL. The value of each pixel describes the distance between the respective pixel and the nearest 0-value pixel in the original image.

F_VN_DistanceTransformation 4:

Display in the ADS Image Watch

In order to be able to display the result image in the ADS Image Watch, it must first be converted into a compatible image format. Depending on the use case, it may be useful to scale the pixel values.

Distance type

The distance type eDistanceType determines the equation with which the distance between a pixel and the nearest 0-value pixel is calculated. The following distance types from the enum ETcVnDistanceType are supported:

Mask sizes

The mask size eMaskSize describes the approximation of the Euclidean distance if the L2 standard is selected as the distance type.

The mask size has no effect with the distance types L1 and C. Instead, a 3x3 mask is always used internally.

Application

An application of this function can look like this, for example:

// convert to binary image
hr := F_VN_Threshold(ipImageIn, ipImageWork, 128, 255, TCVN_TT_BINARY, hr);
// apply distance transformation
hr := F_VN_DistanceTransformation(
    ipSrcImage      :=  ipImageWork,
    ipDestImage     :=  ipImageWork,
    eDistanceType   :=  TCVN_DT_C,
    eMaskSize       :=  TCVN_DTM_3,
    hr
);

// convert image to display it in ADS Image Watch
hr := F_VN_ConvertElementType(ipImageWork, ipImageWork, TCVN_ET_USINT, hr);

Original image

Binary image

Result image

F_VN_DistanceTransformation 5:

F_VN_DistanceTransformation 6:

F_VN_DistanceTransformation 7:

Required License

TC3 Vision Base

System Requirements

Development environment

Target platform

PLC libraries to include

TwinCAT V3.1.4024.54 or later

PC or CX (x64) with PL50, e.g. Intel 4-core Atom CPU

Tc3_Vision