LESSON

# Rank Filtering

#### Share

#### Transcript

In this section we are going to look at a different type of spatial image processing operation. To illustrate this I am going to take the Mona Lisa image and I have chosen a large number of random pixels within the image and I have set them to either 0 or 1. This is sometimes called speckle noise or salt and pepper noise. The image now is corrupted; it has got a large number of white and black spots on it.

One way to try and reduce the effect of this noise would be to compute an average. We have seen before how the averaging operation tends to blur out and reduce fine detail, so if I compute the average of this noise-corrupted image we can see that the intensity of these noise pixels has got less, but they have also tended to get a little bit bigger. They have been smeared out, they are now wider, but they are not as distinct from the background.

If I increase the size of the kernel that we use for averaging we can see that the salt and pepper noise has perhaps almost completely disappeared, but there has been some collateral damage, the image now is quite blurry. So is there a way that we can reduce this salt and pepper noise without removing the fine detail in the image that we want?

And indeed there is, the technique called rank filtering, so to look at how this works we are going to zoom in on a very small 3 by 3 patch of the original image and we see some grey level values here and they are typically around the 120 to a 130 — the grey level values that should be at this particular region — and we see that pixel has got a value of 0 … this is a noise pixel.

If we compute the average over this window, we see that it has a value of a 110. Now this value is a bit lower than the other values that are around here; we probably wouldn’t be expecting a value of a 110 here.

And what has happened is that this value of 0 is so far away from the values that should be here that it has pulled the average down, it’s influenced the average.

We want a different sort of statistic, and a statistic like the median value is actually more helpful. So the median value across this 3 by 3 median would be a 123 and that would perhaps be a much better guess at what the value should be at this location. How do we compute the median? Well the simplest way to compute the median is to take all of the values within this 3 by 3 window and stack them up from the largest through to the smallest. We are going to rank them and rank number 1 pixel is the maximum value within this window. Rank 9, is the minimum value within this window. And rank 5 is the median value and it is the median value we are interested in.

So let’s apply a median filter to the Mona Lisa image. There she is with the salt and pepper noise and here she is after we have applied a 3 by 3 median filter — we can see that the effect of the noise has been dramatically reduced, but the fine detail in the image has been mostly retained.

To compute this we use the tool box function irank. First argument is the input image. This case it would be noise corrupted Mona Lisa, the second argument is a 3 by 3 matrix of ones, which indicates the size of the region over which we want to compute the median.

And the last argument 5 is the rank that we want to choose. Because we have asked for median over a 3 by 3 window, rank 5 is the middle value of the nine elements within that window.

#### Code

So far we have taken a linear combination of pixels in the box around the input pixel, but non-linear operations like sorting and ranking the pixel values also prove to be very useful. We look at the median filter which is much better at removing salt and pepper noise from image than simple smoothing.

### Skill level

MATLAB experience

This content assumes an understanding of high school level mathematics; for example, trigonometry, algebra, calculus, physics (optics) and experience with MATLAB command line and programming, for example workspace, variables, arrays, types, functions and classes.