Filters

What are filters

Today we will investigate the use of blurring filters on images. This is just one of many applications of filters in image processing, and in the next session we will use the same basic techniques for feature detection.

Blurring filters do exactly what it says on the tin. They blur the image. This has the effect of smoothing out noise, so it is a useful technique for noise removal.

One of the groups using machine vision in the final year project last year had problems with the camera failing to return a value in some pixel. The result was black pixels, so-called pepper noise. A blurring filter solves this problem very effectively.

If you have read signal processing or image processing previously, all of this may be familiar to you already.

Conceptual Application

Lenna with Salt and Pepper Noise
Lenna with Salt and Pepper Noise
  • Noisy image. Some pixels are just off.
  • Recalculate pixels using a neighbourhood
  • Use a matrix of coefficients for a weighted average
  • You could implement this with a fairly simple for-loop, but it would be slow.

Convolution

\[(f*g)[x] = \sum_{k=-\infty}^{\infty} f[k]g[x-k]\]

  • Note the convolution of a sampled signal and a continuous one
  • Demo

Note OpenCV uses correlation instead of convolution. Conceptually this is different, but in practice, the only effect is to mirror the filter.

\[(f\star g)[x] = \sum_{k=-\infty}^{\infty} f[k]g(k+x)\]

Non-linear filtering

  • Convolution is a linear operation
    • Simple and fast
  • Non-linear filters may be better for denoising.
  • However, we need linear filters when we move on to edge detection.
  • Non-linear filters must be for another course.

Blurring in Python

The -1 means the same depth is used for output as for input. As the image, we can use lenna-awgn.png with Gaussian noise or lenna-snp.png with Salt and Pepper noise.

If we want to use convolution instead of correlation, we have (requiring extra imports)

Gaussian filter

Above, we used a uniform filter (f7).

This gives a different matrix which we can use as a blurring filter.

Out[39]: 
array([[6.96247819e-08, 2.80886418e-05, 2.07548550e-04, 2.80886418e-05, 6.96247819e-08],
       [2.80886418e-05, 1.13317669e-02, 8.37310610e-02, 1.13317669e-02, 2.80886418e-05],
       [2.07548550e-04, 8.37310610e-02, 6.18693507e-01, 8.37310610e-02, 2.07548550e-04],
       [2.80886418e-05, 1.13317669e-02, 8.37310610e-02, 1.13317669e-02, 2.80886418e-05],
       [6.96247819e-08, 2.80886418e-05, 2.07548550e-04, 2.80886418e-05, 6.96247819e-08]])

Note, large numbers in the middle, smaller towards the edges. Since we divided by the sum in the last line, the entries now add to one.

Generating Noisy Images for Testing