Blur filter

In this sample the following blur filters are compared:

Explanation

In some cases an image is to be smoothed or freed from noise. For tasks such as Code Reading it is necessary at the same time to obtain a good contrast and good edges on the image so that the downstream algorithms can successfully work on it.

The bilateral filter is particularly suitable for the combination of image smoothing with the simultaneous retention of edges. For each pixel intensity it calculates a weighted average value of the surrounding pixels. The weighting depends not only on how far away the pixel is, but also on the difference in intensity. As a result, sharp edges are retained.

The Gaussian filter smooths images with the help of a two-dimensional discrete approximation to the Gaussian bell function. This allows the image noise to be reduced. Smaller structures are lost, but coarser structures on the other hand are retained. Edges are therefore less blurred than with the two following filters, but larger disturbances are not removed so well.

The median filter sorts all pixel values of the specified mask in ascending order and then selects the median value, to which the corresponding pixel is then set. This filter is well suited for suppressing structures (disturbances) that are smaller than the applied filter mask and is very robust towards outliers. As a result, the small salt-and-pepper disturbances, for example, are removed best, but the disadvantage of this is that the other detailed information is also lost.

The custom filter function was used in order to form a mean value. Here, the value of each pixel is replaced by the mean value of the kernel pixel, which results in blurring. Disturbances (noise) are suppressed as a result, but details and edges gradually become blurred as the number of kernels increases. As the custom filter function offers many different options for applying your own filters, the F_VN_InitMatrixStruct function is additionally required for the standardization of the filter kernel.

Variables

// Bilateral Filter
ipImageBilateral         :   ITcVnImage;
ipImageBilateralDisp     :   ITcVnDisplayableImage;
nBilateral_Diameter      :   DINT  := 7;
fBilateral_SigmaColor    :   LREAL := 100;
fBilateral_SigmaSpace    :   LREAL := 100;

// Gaussian Filter
ipImageGaussian          :   ITcVnImage;
ipImageGaussianDisp      :   ITcVnDisplayableImage;
nFilterWidth             :   UDINT := 7;
nFilterHeight            :   UDINT := 7;

// Median Filter
ipImageMedian            :   ITcVnImage;
ipImageMedianDisp        :   ITcVnDisplayableImage;
nMedian_FilterSize       :   UDINT := 7;

// Custom Filter e.g. Mean
ipImageCustom            :   ITcVnImage;
ipImageCustomDisp        :   ITcVnDisplayableImage;
stKernelMatrix           :   TcVnMatrix;
// 7x7 Mean Filter Kernel with weights of 1/49 ~ 0.0204081632653
aMatrixArray7x7          :   ARRAY [0..6,0..6] OF REAL := [49(0.0204081632653)];

Code

hr := F_VN_BilateralFilter(
        ipSrcImage      := ipImageIn,
        ipDestImage     := ipImageBilateral,
        nDiameter       := nBilateral_Diameter,
        fSigmaColor     := fBilateral_SigmaColor,
        fSigmaSpace     := fBilateral_SigmaSpace,
        hrPrev          := hr);

hr := F_VN_GaussianFilter(
        ipSrcImage      := ipImageIn,
        ipDestImage     := ipImageGaussian,
        nFilterWidth    := nFilterWidth,
        nFilterHeight   := nFilterHeight,
        hrPrev          := hr);

hr := F_VN_MedianFilter(
        ipSrcImage      := ipImageIn,
        ipDestImage     := ipImageMedian,
        nFilterSize     := nMedian_FilterSize,
        hrPrev          := hr);

// Mean-Filter with Custom Filter Function
hr := F_VN_InitMatrixStruct(
        pSrcBuffer      := ADR(aMatrixArray7x7),
        stDestMatrix    := stKernelMatrix,
        nRows           := 7,
        nCols           := 7,
        eElementType    := TCVN_ET_REAL,
        hrPrev          := hr);

hr := F_VN_CustomFilter(
        ipSrcImage      := ipImageIn,
        ipDestImage     := ipImageCustom,
        eDestDepth      := TCVN_ET_USINT,
        stKernel        := stKernelMatrix,
        hrPrev          := hr);

Result

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).

So that the effects of the different filters are as comparable as possible, a kernel size of 7 x 7 is used with all of them. Using other parameter values or kernel sizes, as well as further setting options for the EXP variants of the functions, other effects can be achieved in detail. As the filter effect very much depends on the content of the input image, it is recommended to test several filters with different parameter settings in order to compare them directly and find the best filter for the use case. The table below contains an overview of the result images:

Original images

Bilateral filter

Gaussian filter

Median filter

Mean filter

Blur filter 1:

Blur filter 2:

Blur filter 3:

Blur filter 4:

Blur filter 5:

Blur filter 6:

Blur filter 7:

Blur filter 8:

Blur filter 9:

Blur filter 10:

Blur filter 11:

Blur filter 12:

Blur filter 13:

Blur filter 14:

Blur filter 15:

Notice

Long or fluctuating execution times

The duration of the execution of a filter essentially depends on the type of filter as well as the image and kernel size. Therefore, please note that a filter operation can take considerably longer than other functions, so you should adapt the cycle time of your task accordingly.

If the filter parameters are changed during the runtime, in particular the kernel size, it is recommended to use watchdogs. In the case of the median filter, the runtime can always fluctuate somewhat due to the principle.

Furthermore, job tasks can lead to a reduction in the total computing time, which should be tested according to the system.