Coming back to local filters, there's another extremely important and extremely
appropriate local filter which is called a median filter,
which we are going to describe next. And it's a great filter because it
doesn't really blur edges. And although it's a very classical
filter, it's still very useful today. So, let's observe it.
Before we actually describe it, let's see what happens with a real image.
We have here an image, that has been contaminated with what's called salt and
pepper noise. Next week, we're actually going to
describe different models of noise a bit better, but salt and pepper, it's just
noise that basically is very strong noise, individually added at different
pixels. We see it here.
This is the result of the local mean filter.
What we have seen in the previous videos, just local averaging,
in this case, a three by three. You might be able to observe there is
some reduction of noise but not really too much reduction of noise.
Here, on the other hand, is the result of a three by three median filter and we see
a great result. We don't see blurring.
we actually see that a lot of the noise has been reduced.
We could reduce much more noise with a local mean filter, a local average.
If we go beyond three by three, we might go five by five, nine by nine.
Remember, by averaging we are reducing the noise by square of the number of
pixels that we are averaging as we saw in the previous videos.
But if we enlarge the neighborhood, we also saw that we are going to start to
blur a lot of the details. On the other hand,
keeping a three by three neighborhood would still get extremely good denoising
with the median filter. So, what's the median filter?
Very simple. What we do, is we actually take, let's
say, if we are working in a three by three neighborhood,
we basically order the pixels so we basically order them from the smallest to
the largest and we pick the one in the middle,
okay? So, we start putting all the pixels in
order, we pick the one in the middle. This is an operation we actually learn in
elementary school. And it's an extremely powerful, replace
it by the one in the middle. So, for example, if you have pixels which
look like this and one of the pixels is very noisy,
when you order them, this one is going to be replaced by one
of these values that are all equal so it's going to be replaced, it's going to
go down, okay?
So, the noise is clean. Completely clean, without blurring as we
have seen for Gaussian filtering or for mean filtering.
It's a very, very simple filter. You order, and you pick the one in the
middle. You order from the smallest to the
largest, and you pick the one in the middle.
And then, you move to the next window. So, you basically take a three by three
window, then you move to the next window, the next window, the next window.
And you get this extremely simple filter, the median filter.
Before I show you additional examples of the median filter in real time, we're
going to actual run MatLab to do that. Let me just give you one interesting
property of the median filter. We saw in a couple of views ago that the
average filter is reducing this, a minus ai squared. We say the average is the
value of a that reduces, that minimizes this and I'm summing over all the pixels.
So, I'm going to ask you a question, can we represent the median filtering in a
form like this? A different function maybe.
Some of certain function of a minus ai, can we do that?
Just think for a while, and tell me yes or no, if you know the answer of that.
So, what's the answer to this question? Very interesting.
This is the mean. The value of a that minimizes the square
error is the mean. What's the median?
The median is the value that minimizes the absolute.
There's no square here. It's just the absolute difference.
It's extremely interesting that by replacing this square by basically
nothing, just absolute value, we get the median.
Once again, you can prove this. It's a bit more of a difficult proof,
because when we discuss how to prove this, we say, just take derivatives, make
the derivative equal to zero, and you're going to find that a is the average of
all the ai's. Here, you're not allowed to take
derivative, because at zero, the absolute value cannot have a, doesn't have a
derivative. So, the proof is a bit more delicate.
But I'm sure that you can do it as a bonus exercise.
Don't worry too much about that for now. You can believe me, that if you don't
want to do the proof by yourself, that the result of minimizing this is the
median filter. An extremely interesting operation.
For those that are familiar with the area of robot statistics,
this is a square error, which is not very robust, and that's why it's blurring.
This is a much more robust error. And that's why it's able to completely
eliminate noise, like salt and pepper noise, without blurring the rest of the
image. The last interesting property I want to
mention about the median is in the average, the result doesn't have to be
one of the pixels. So, for example, if you're averaging one
and two, you get 1.5. It's not one, it's not two,
okay? When you're averaging, let's say, a three
by, when you're completing the average of a
three by three and you're replacing the middle pixel by the average, you might be
replacing the pixel by a value that doesn't show up,
not in the three by three window, maybe not even in the whole image.
Once again, the average between, of one and two is 1.5,
a new value. On the other hand, when you're doing the
median filtering, you're always replacing by one of the values that already exist
in the three by three window. So, you're not producing new pixel
values. And that's part of what's happening here.
And that's a very interesting property by itself.
There is no novelty. There is no new values.
Only those that are already familiar in the neighborhood of the pixel that you
are replacing the pixel with the median. Now,
now, that we know about the median, now that we know some of the interesting
properties of the median, let's just see a few additional examples
of the median filter in action. Let's see that in the next video.
Thank you.