F_VN_CheckColorRange

F_VN_CheckColorRange 1:

Check if the pixel values of an image lie in a given range. The destination image has the same size as the source image but only one channel with a pixel element size of 8 bit. Its elements are set to 255 if the corresponding pixels of the source image are in the checked range and set to 0 otherwise.

Syntax

Definition:

FUNCTION F_VN_CheckColorRange : HRESULT
VAR_INPUT
    ipSrcImage   : ITcVnImage;
    ipDestImage  : Reference To ITcVnImage;
END_VAR
VAR_IN_OUT
    aLowerBounds : TcVnVector4_LREAL;
    aUpperBounds : TcVnVector4_LREAL;
END_VAR
VAR_INPUT
    hrPrev       : HRESULT;
END_VAR

F_VN_CheckColorRange 2: Inputs

Name

Type

Description

ipSrcImage

ITcVnImage

Source image

ipDestImage

Reference To ITcVnImage

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

hrPrev

HRESULT

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

F_VN_CheckColorRange 3: In/Outputs

Name

Type

Description

aLowerBounds

TcVnVector4_LREAL

Channel-wise lower bounds (Unused channels are ignored.)

aUpperBounds

TcVnVector4_LREAL

Channel-wise upper bounds (Unused channels are ignored.)

F_VN_CheckColorRange 4: Return value

HRESULT

Further information

The F_VN_CheckColorRange function segments the input image with regard to a specific value range. This means that it does not matter which color space is used, as the segmentation is purely numerical.

Parameter

Input image

The input image ipSrcImage can have any format.

Result image

The result image ipDestImage returns the segmented 1-channel 8-bit binary image.

Range of Values

The value range to be segmented is defined by the lower limit aLowerBounds and upper limit aUpperBounds.

For cyclic value ranges such as the Hue channel in HSV space, the lower limit value can exceed the upper limit value in order to segment color ranges across the zero crossing. A range from the lower limit [250, ...] to the upper limit [5, ...] corresponds to the combination of a function call with [250, ...] - [MAX, ...] and [0, ...] - [5, ...]. This makes it possible to directly select color tones that lie in the transition of the value range, such as the red range (+-20° around 0°) in the Hue channel, with a function call.

F_VN_CheckColorRange 5:

Initialize boundary vectors

Always be sure to initialize the boundary vectors with at least as many elements as the input image has channels. For example, if you segment a 4-channel image (e.g. with alpha channel) and initialize the color vectors with only three elements, the fourth elements of the vectors are 0 and the result image turns black.

Application

For reliable color segmentations, it is generally advisable to work in a color space other than the additive RGB space. For example, in HSV space, hue (H) is separated from lightness (V) and saturation (S), which means that color differentiation is typically less sensitive to fluctuations in lighting.

The segmentation of an HSV image (HSV_FULL) for a red color spectrum looks like this, for example:

VAR
    aLower      :   TcVnVector4_LREAL := [250, 150, 100];
    aUpper      :   TcVnVector4_LREAL := [5, 255, 255];
END_VAR

hr := F_VN_CheckColorRange(
    ipSrcImage      := ipImageIn,
    ipDestImage     := ipImageRes,
    aLowerBounds    := aLower,
    aUpperBounds    := aUpper,
    hrPrev          := hr);

Examples

Related functions

Required License

TC3 Vision Base

System Requirements

Development environment

Target platform

PLC libraries to include

TwinCAT V3.1.4024.59 or later

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

Tc3_Vision