filter Module

Surface smoothers

Morphological Filter

The morphological filter is a technique in surface processing used to process geometrical structures within a surface.

Imagine a ball rolling over the surface. Depending on the size and shape of the ball, it modifies the surface differently: larger balls can bridge small gaps, while smaller balls can fall into minor crevices.

This filter is particularly useful in operations such as closing and opening, which can help in joining narrow breaks and eliminating small holes within objects in an image, respectively.

morpho_filter

Median Filter 1

The median filter is a non-linear digital filtering technique, often used to remove noise from surfaces.

It works by moving through each point of the surface, and considering a small neighborhood around that element. The filter then replaces the element with the median of its neighboring values. This method is particularly effective because, unlike mean filtering, it can remove noise while preserving edges in the surfaces.

The median filter maintains sharp edges in surfaces and is robust against the introduction of noise.

Median Filter 2

This median filter is a bit more complex than the classical median filter.

median_filter

An outlying observation, or “outlier”, is one that appears to deviate markedly from other members of the sample in which it occurs. Therefore no universal procedure exists to remove extra points: it depends on the kind of outliers and the surrounding data.

In the present case several procedures have been tested and the one that best suits our need is the following:

  • A 5x5 kernel median filter is applied to S1, giving S2.
  • S3 = S1 -S2 represents the S1 deviation from the median.
  • S3 is divided in 10 parts in each direction, and for each part the standard deviation is calculated. The global S3 deviation σ is defined as the median value of the 10x10 standard deviations
  • Inspired by the normal law, the procedure ends with the substitution of heights, for which abs(S1-S2) > 3σ, by median heights. The cleaned surface which is obtained will be called SA in what follows.
Eventhough the procedure is unusual, it provides satisfactory results in cleaning the primary extracted surfaces, without altering “real” points.

Smoothing Filter

The 2D smoothing kernel with a stencil of [1 2 1] represents a simple, yet effective method for smoothing a surface. This stencil typically forms a part of a larger convolution kernel applied over an image to average height values, effectively reducing noise and detail.

This kernel is often used in conjunction with a similar vertical stencil to create a two-dimensional convolution mask. In practice, the [1 2 1] stencil is one row of a matrix, with possibly a corresponding column vector [1; 2; 1], used to perform a 2D convolution operation on a surface.

When applied, each height in the resulting surface is a weighted average of its neighbors with the weights given by the kernel:

  • The central height in the neighborhood receives the highest weight, typically reflecting higher importance or influence on the resulting height value.
  • The adjacent heights horizontally (and vertically, when combined with the vertical vector) receive a lower weight than the central height but contribute significantly to the smoothing effect.
  • Corner heights, if included by expanding the kernel to a full 2x2 or 3x3 matrix using both horizontal and vertical components, would receive the lowest weights.
In terms of application, the resulting convolution matrix for the two-dimensional case is : $$ \begin{matrix} 1 & 2 & 1 \\ 2 & 4 & 2 \\ 1 & 2 & 1 \end{matrix} $$ This configuration spreads the influence of a single height to its eight neighbors in a weighted manner, with a stronger influence horizontally and vertically, rather than diagonally.

The strength of smoothing is adjusted by normalizing this kernel (dividing all entries by the sum of the entries, which is 16 in the standard case), thereby ensuring that the total influence in the area remains constant, preserving overall height.

Gaussian Filter

EN ISO 16610-61 septembre 2015 - Geometrical product specification (GPS) - Filtration - Part 61: Linear areal filters: Gaussian filters The weighting function of an areal filter has the formula of a rotationally symmetric Gaussian function with a cut-off wavelength, \(\lambda_c\) , given by : $$ S(x,y)=\frac{1}{\alpha^2 \lambda_c^2} \; \exp \left[-\frac{\pi}{\alpha^2} \; \left( \frac{x^2 + y^2}{\lambda_c^2} \right) \right] $$
  • \(x\) is the distance from the centre (maximum) of the weighting function in \(X\) direction;
  • \(y\) is the distance from the centre (maximum) of the weighting function in \(Y\) direction;
  • \(\lambda_c\) is the cut-off wavelength;
  • \(\alpha\) is the constant, to provide 50% transmission characteristic at the cut-off \(\lambda_c\). Therefore, \(\displaystyle \alpha = \sqrt{\frac{\log 2}{\pi}}\)
The Gaussian filter is employed in surface processing for reducing detail and noise, effectively smoothing the surface.

This filter utilizes a Gaussian function for calculating the transformation to apply to each point in the surface. It assigns weights to pixels with distances based on the Gaussian function's characteristics, heavily weighting pixels nearer to the central pixel and progressively less to those farther away.

This creates a blurring effect that is very effective at high-frequency noise reduction, leading to a smoother appearance in surfaces.


Uses

  • module~~filter~~UsesGraph module~filter filter data_arch data_arch module~filter->data_arch fftw3 fftw3 module~filter->fftw3 module~stat_mom stat_mom module~filter->module~stat_mom sort_arrays sort_arrays module~filter->sort_arrays surfile surfile module~filter->surfile module~stat_mom->data_arch module~stat_mom->sort_arrays

Used by

  • module~~filter~~UsedByGraph module~filter filter module~anisotropy anisotropy module~anisotropy->module~filter module~grad_curv grad_curv module~grad_curv->module~filter program~test_smooth test_smooth program~test_smooth->module~filter program~test_anisotropy test_anisotropy program~test_anisotropy->module~anisotropy program~test_grad_curv test_grad_curv program~test_grad_curv->module~grad_curv

Variables

Type Visibility Attributes Name Initial
real(kind=R8), public :: PAD_FFT_FILTER = PAD_FFT

Subroutines

public subroutine fft_filter(tab, long, larg, cutoff, bf_tab, multi_fft, pad, ext, type_apo)

Classical Gaussian filter

Arguments

Type IntentOptional Attributes Name
real(kind=R8), intent(in), dimension(1:long, 1:larg) :: tab

2D array in

integer(kind=I4), intent(in) :: long

2D array width

integer(kind=I4), intent(in) :: larg

2D array height

real(kind=R8), intent(in) :: cutoff

cut-off wavelength

real(kind=R8), intent(out), dimension(1:long, 1:larg) :: bf_tab

2D array out

logical(kind=I4), intent(in) :: multi_fft

multiple fft at once ?

real(kind=R8), intent(in), optional :: pad

fft padding

character(len=*), intent(in), optional :: ext

extension

character(len=*), intent(in), optional :: type_apo

apodization type

private subroutine gaussian_filter(long, larg, xc, gauss_filt)

Gaussian kernel

Arguments

Type IntentOptional Attributes Name
integer(kind=I4), intent(in) :: long

2D array length

integer(kind=I4), intent(in) :: larg

2D array width

real(kind=R8), intent(in) :: xc

the cut-off wavelength

real(kind=R8), intent(out), dimension(1:long, 1:larg) :: gauss_filt

2D array out

public subroutine median_filter(tab, long, larg, snb, kernel, sig, omp)

A bit more complex filter: the overall height standard deviation is taken into account

Arguments

Type IntentOptional Attributes Name
real(kind=R8), intent(inout), dimension(1:long, 1:larg) :: tab

2D array

integer(kind=I4), intent(in) :: long

2D array length

integer(kind=I4), intent(in) :: larg

2D array width

integer(kind=I4), intent(in) :: snb

patch number along a direction

integer(kind=I4), intent(in) :: kernel

kernel size

real(kind=R8), intent(in) :: sig

error std

logical(kind=I4), intent(in) :: omp

if multithreading

public subroutine median_smooth(tab, long, larg, kernel, omp)

Very classical smoothing

Arguments

Type IntentOptional Attributes Name
real(kind=R8), intent(inout), dimension(1:long, 1:larg) :: tab

2D array

integer(kind=I4), intent(in) :: long

2D array length

integer(kind=I4), intent(in) :: larg

2D array width

integer(kind=I4), intent(in) :: kernel

kernel size

logical(kind=I4), intent(in) :: omp

if multithreading

public subroutine morpho_filter(tabin, tabou, long, larg, scale_xyz, ray, omp, nb_div, mtype)

Morphological filter: uses combinations of roll_smooth to provide all kind of transformation :

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=R8), intent(in), dimension(1:long, 1:larg) :: tabin

2D array in

real(kind=R8), intent(out), dimension(1:long, 1:larg) :: tabou

2D array out

integer(kind=I4), intent(in) :: long

2D array length

integer(kind=I4), intent(in) :: larg

2D array width

real(kind=R8), intent(in), dimension(1:3) :: scale_xyz

lag along x, y and scale z

real(kind=R8), intent(in) :: ray

roll radius

logical(kind=I4), intent(in) :: omp

if multithreading

integer(kind=I4), intent(in) :: nb_div

number of macro elements along an axis

character(len=*), intent(in) :: mtype

closing, opening, dilation or erosion

private subroutine roll_smooth(tabin, tabou, long, larg, scale_xyz, sgn, ray, omp, nb_div)

A ball of radius “ray” rolls on / below the surface, hence defining a closing or an opening enveloppe.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=R8), intent(in), dimension(1:long, 1:larg) :: tabin

2D array in

real(kind=R8), intent(out), dimension(1:long, 1:larg) :: tabou

2D array out

integer(kind=I4), intent(in) :: long

2D array length

integer(kind=I4), intent(in) :: larg

2D array width

real(kind=R8), intent(in), dimension(1:3) :: scale_xyz

lag along x, y and scale z

integer(kind=I4), intent(in) :: sgn

+ 1: dilation, -1:erosion

real(kind=R8), intent(in) :: ray

roll radius

logical(kind=I4), intent(in) :: omp

if multithreading

integer(kind=I4), intent(in) :: nb_div

number of macro elements along an axis

public subroutine soften(tabin, mask, tabout, long, larg)

Function to smooth out a 2D array: each point is replaced by a weighted mean of its neighbors.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=R8), intent(in), dimension(1:long, 1:larg) :: tabin

2D array in

integer(kind=I4), intent(in), optional, dimension(1:long, 1:larg) :: mask

mask

real(kind=R8), intent(out), dimension(1:long, 1:larg) :: tabout

2D array out

integer(kind=I4), intent(in) :: long

2D array width

integer(kind=I4), intent(in) :: larg

2D array height