F_VN_Threshold

F_VN_Threshold 1:

Apply a fixed threshold or a dynamic threshold according to Otsu.
Can use available TwinCAT Job Tasks for executing parallel code regions.
Can return partial results when canceled by Watchdog.

Syntax

Definition:

FUNCTION F_VN_Threshold : HRESULT
VAR_INPUT
    ipSrcImage     : ITcVnImage;
    ipDestImage    : Reference To ITcVnImage;
    fThreshold     : LREAL;
    fMaxValue      : LREAL;
    eThresholdType : ETcVnThresholdType;
    hrPrev         : HRESULT;
END_VAR

F_VN_Threshold 2: Inputs

Name

Type

Description

ipSrcImage

ITcVnImage

Source image (For Otsu and Triangle threshold types, only 1 channel of TCVN_ET_USINT is supported)

ipDestImage

Reference To ITcVnImage

Destination image (An appropriate destination image will be created if required.)

fThreshold

LREAL

Fixed threshold (unused if dynamic thresholding is selected)

fMaxValue

LREAL

Maximum pixel value

eThresholdType

ETcVnThresholdType

Threshold type to be applied

hrPrev

HRESULT

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

F_VN_Threshold 3: Return value

HRESULT

Further information

The function F_VN_Threshold segments the input image with regard to a threshold. Depending on this threshold and the algorithm used, each pixel of the binary result image is assigned the value 0 and a specified target value.

Algorithm

A threshold transforms a monochrome image into a binary image and thus creates segments. Good segmentation is a prerequisite for many subsequent methods such as contour search. TwinCAT Vision offers different types of threshold values that are suitable for different applications.

Fixed threshold value

A fixed threshold simply creates a binary image by setting pixels with a value greater than fThreshold to white (255) and others to black (0). fThreshold is a parameter of the corresponding functions. To find a good value, you will most probably approach it via different attempts.

Dynamic threshold value

A dynamic threshold according to Otsu works perfectly for bimodal images. The histogram of a bimodal image has two main peaks and a good threshold value is most likely exactly in the middle. If you have a noisy image, you can use a Gaussian filter to shift it towards bimodal.

Adaptive threshold value

An adaptive threshold value uses variable threshold values for the whole image. See F_VN_AdaptiveThresholdExp .

Parameter

Input image

The input image ipSrcImage may have one or three channels.

Result image

The result image ipDestImage returns the segmented binary image.

Threshold value

The threshold fThreshold defines the segmentation condition. It must be adjusted depending on the properties of the input image.

Target value

The target value fMaxValue defines which value should be given to the pixels that fulfil the threshold criterion. If the result image is to consist only of black and white pixels, then the maximum value of the element type of the input image must be set here. This corresponds to (2^d)-1, where d represents the bit depth of the element type; e.g. (2^8)-1=255 with 8-bit images.

Threshold type

The threshold type eThresholdType defines the algorithm according to which the threshold fThreshold is applied to the input image. All values of the enum ETcVnThresholdType are supported.

Application

The binary segmentation of an 8-bit grayscale image in the center of the value range looks like this, for example:

hr := F_VN_Threshold(
    ipSrcImage      :=  ipImageIn,
    ipDestImage     :=  ipImageRes,
    fThreshold      :=  128, // half value range: (2^8)/2=128
    fMaxValue       :=  255, // max value of 8-bit image: (2^8)-1=255
    eThresholdType  :=  TCVN_TT_BINARY,
    hrPrev          :=  hr
);

Related functions

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