Image filtering#

Definition. A filter is an operation or system that selectively modifies a signal by enhancing, attenuating, or removing certain components, while leaving others relatively unchanged. Those components can be defined in terms of:

  • frequency (most common)

  • space (image filtering)

  • time (temporal filtering)

Mathematically: A filter is a function or operator \(H\) that transforms an input signal \(x\) into an output signal \(y\) as:

\[y = H(x)\]

Think of a filter as a gate:

  • lets some information pass

  • blocks or weakens the rest

Examples:

  • sunglasses → filter light intensity

  • audio equalizer → filters sound frequencies

Common types of filters in the frequency domain:

  • Low-pass filter: passes low frequencies and suppresses high frequencies Effect: smoothing / blurring

  • High-pass filter: passes high frequencies and suppresses low frequencies Effect: edge enhancement / detail

  • Band-pass filter: passes only a range of frequencies

Filters in images

  • Low frequencies → global structure, smooth regions

  • High frequencies → edges, textures, noise That’s why:

  • blurring = low-pass filtering

  • edge detection = high-pass filtering

Linear Shift Invariant Systems (LSIS)#

Linear Shift Invariant Systems (LSIS) are very important in image processing.

../_images/LSIS1.png

We consider first one dimensional signals.The extension to two dimensional signals, i.e. images, will be simple.

It leads to lots of useful image processing algorithms, beacuse many tasks in image processing can be described by LSIS.

What is a LSIS?

Property 1. Linearity:

../_images/LSIS2.png

Property 2. Shift invariance:

../_images/LSIS3.png

Why are LSIS important?

../_images/LSIS4.png

If the object moves far from the lens, the new image is obtained by applying a LSIS to the previous one.

The defocused image \(g\) is the processed version of the image \(f\).

  • linearity: brightness variation

  • shift invariance scene movement in \(f\) has the same movements in \(g\).

Convolution#

Convolutions of two 1D functions \(f(x)\) and \(h(x)\):

../_images/conv1.png

We follow the steps.

../_images/conv2.png

Fig. 1 Step 1- Draw f and h#

../_images/conv3.png

Fig. 2 Step 2 -#

../_images/conv5.png

Fig. 3 Step 3- multiply f and h and compute the integral.#

../_images/conv6.png

Fig. 4 Step 4- repeat Step 3 for all the x in the domain.#

Any LSIS implies convolution and whenever you perform a convolution you have a LSIS.

Examples:

../_images/conv7.png
../_images/conv8.png

When you perform a convolution, the result is a function that satisfy linearity and shift inversion.

Linearity $\(g_1(x)=\int f_1(\tau)h(x-\tau)d\tau, \ \ g_2(x)=\int f_2(\tau)h(x-\tau)d\tau \)$

then: $\(\int (\alpha f_1(\tau) + \beta f_2(\tau)) h(x-\tau)d\tau=\)\( \)\(= \alpha \int f_1(\tau) h(x-\tau)d\tau+ \beta \int f_2(\tau) h(x-\tau)d\tau\)\( \)\(=\alpha g_1(x)+\beta g_2(x)\)$

Shift invariance $\(g(x)=\int f(\tau)h(x-\tau)d\tau\)\( Then: \)\(\int f(\tau -a) h(x-\tau)d\tau=\int f(\mu)h(x-a-\mu)d\mu=g(x-a)\)$

The unit of the convolution operator is the unit impulse function

../_images/conv9.png

If you convolve it with any function \(f\), the result is \(f\).

Properties of Convolution

  1. Commutative: ab=ba

  2. Associative: a*(bc)=(ab)*c

2D Convolution and linear image filters#

In case of 2D functions the convolution is extended as: $\(g(x,y)=\int \int f(\tau,\mu)h(x-\tau,y-\mu) d\tau d\mu\)$

In the discrete case, for images, we have:

../_images/filt2.png

\(h\) is usually called as convolution mask or convolution kernel or convolution filter.

If we want to understand what happen when the kernel is applied to a single pixel, first of all it is flipped twice, then it is applied to the pixel:

../_images/filt3.png
../_images/filt4.png

Then you sum all the values and this is the result stored in the output pixel:

../_images/filt5.png

What about the borders? To apply the convolution to a pixel on the border is necessary to extend somehow the image.

../_images/filt6.png

Examples

  1. If we convolve with the impulse function, we obtain the sama image of the input:

../_images/filt7.png
  1. If we consider the box kernel, as a square of constant value equal to one:

../_images/filt8.png

The output image is saturated. If we normalize the values inside the box by diving by the area of the box we obtain:

../_images/filt9.png

How to improve the results of the box filter where blocky artifacts are visible? With a fuzzy filter:

../_images/filt10.png

Fig. 5 Box filter 21x21#

../_images/filt11.png

Fig. 6 fuzzy filter 21x21#

The maximum value is in the center and the the values symmetrivally decrease far from the centre. In this case the image is smoothed but there aren’t any more blocky artifacts.

The fuzzy filter can be expressd mathematically by means of the gaussian function.

../_images/filt1.png

Note that the gaussian function goes to zero only at infinity. Which is the best size of gaussian mask to use? Ususally, \(k \simeq 2 \pi \sigma\). An example:

../_images/filt12.png
../_images/filt13.png

The Gaussian filter has the property to be separable:

../_images/filt14.png

This is important for the computational cost. We can examine the cost for a single pixel. The total cost is obtained by summing the cost for all the pixels in the image.

../_images/filt15.png

Non linear filters#

Non linear filters cannot be implemented with convolutions.

Why use non linear filters: An example. We want to remove salt-and-peppers noise froma an image.

../_images/filt16.png

With a gaussian filter the noise is not exactly removed, but smeared out, but we are also loosing some details on the coins.

Median filter

The median filter is obtained by means of this algorithm, for each pixel. 1- Create a square mask around the pixel. 2- sort the values of the pixels n teh mask. 3- Consider the median of that values. 4- subsitute the medianl value to the considered pixel. This is the new value of the pixel in the filtered image.

../_images/filt17.png

but if you use a larger mask, the noise is better removed but the image is quite blurred:

../_images/filt18.png

Bilateral filter

Suppose now to go back to the gaussian filter, but not use the same filter for any pixel. In practice, we want to create a single filter for each pixel, that takes care of the context where the pixel is and of its neighbour pixels. We can, for example, eliminate the pxels that have a too different brightness value from the agussian mask:

../_images/filt19.png

How to create a filter like this? If you apply the gaussina filter to an image like a step and youi cneter the filter in the yellow pixels, the red and green pixels have the same smoothing, vene if they are at oppsite sides of the step. the final result is the smoothe image (surface) on the left, that obviously is not satisfactory.

../_images/filt20.png
../_images/filt21.png

To face this problem, we add a new term to the filter, that is called brightness gaussian that cares abut the brigtness of the pixels in the mask an by means of the coefficent large weights are assigned when the difference of brightness is small and samll weights are assigned when the difference of brightness is large.

../_images/filt22.png

so that the final filters looks like this:

../_images/filt23.png

The explicit form of the bilateral filter is the follwing:

../_images/filt24.png

Examples:

../_images/filt25.png

Increasing \( \sigma \):

../_images/filt26.png