F_VN_MedianFilter

F_VN_MedianFilter 1:

Apply a Median filter to an image.

Syntax

Definition:

FUNCTION F_VN_MedianFilter : HRESULT
VAR_INPUT
    ipSrcImage  : ITcVnImage;
    ipDestImage : Reference To ITcVnImage;
    nFilterSize : UDINT;
    hrPrev      : HRESULT;
END_VAR

F_VN_MedianFilter 2: Inputs

Name

Type

Description

ipSrcImage

ITcVnImage

Source image (for nFilterSize 3 or 5: USINT, UINT, REAL. For bigger filters, only USINT is supported.

ipDestImage

Reference To ITcVnImage

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

nFilterSize

UDINT

Size (width and height) of the filter (3, 5, 7, ...)

hrPrev

HRESULT

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

F_VN_MedianFilter 3: Return value

HRESULT

Further information

The function F_VN_MedianFilter applies a median filter to the input image.

Algorithm

The median filter is a ranking filter that is often used to suppress noise. All pixel values captured by the filter matrix are sorted by size, then the current value is replaced by the value at the center position of the sorted list. The advantage over a filter with a weighted mean value (e.g. a Gaussian filter) is that strong outliers of individual pixels (such as the 155 in the following sample image) are completely ignored and thus do not have a negative effect on the result.

The size of the filter matrix is specified by nFilterSize, only odd integers >= 3 being accepted.

F_VN_MedianFilter 4:

Parameter

Input image

The input image ipSrcImage can have any number of channels. The element types USINT; UINT and REAL are permitted with a filter size nFilterSize of 3 or 5. Only the element type USINT is supported for larger filters.

Result image

The result image ipDestImage returns the filtered image.

Filter size

The filter size nFilterSize defines the height and width of the filter area. It must be an odd number and >= 3.

Application

The application of a median filter with the size 7×7 looks like this, for example:

hr := F_VN_MedianFilter(
    ipSrcImage      :=  ipImageIn,
    ipDestImage     :=  ipImageRes,
    nFilterSize     :=  7,
    hrPrev          :=  hr
);

The unprocessed original image (1st row) already exhibits fine structures on the surface of the gear wheels in the detail. In order to illustrate the effects of the filters, additional disturbances were added to the original image by Gauss noise (2nd row) and salt-and-pepper noise (3rd row).

Original images

Result images

F_VN_MedianFilter 5:

F_VN_MedianFilter 6:

F_VN_MedianFilter 7:

F_VN_MedianFilter 8:

F_VN_MedianFilter 9:

F_VN_MedianFilter 10:

Samples

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