0% found this document useful (0 votes)
23 views86 pages

6-7noise Filtering

The document discusses noise in images, focusing on types such as salt and pepper noise, impulse noise, and Gaussian noise, along with methods for generating and visualizing these noises using MATLAB. It also covers filtering techniques, particularly moving averages and Gaussian filters, to remove noise from images, explaining the importance of kernel size and shape in smoothing effects. Additionally, it touches on linearity in filtering operations and the computational complexity involved in applying these filters.

Uploaded by

wasifa kanwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views86 pages

6-7noise Filtering

The document discusses noise in images, focusing on types such as salt and pepper noise, impulse noise, and Gaussian noise, along with methods for generating and visualizing these noises using MATLAB. It also covers filtering techniques, particularly moving averages and Gaussian filters, to remove noise from images, explaining the importance of kernel size and shape in smoothing effects. Additionally, it touches on linearity in filtering operations and the computational complexity involved in applying these filters.

Uploaded by

wasifa kanwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Adv DIP

Noise and Filtering


NOISE
Noise
Noise is simply a function that is combined
with the original function to get a new.
Noise
Noise is simply a function that is combined
with the original function to get a new.
Common Types of Noise
Salt and pepper noise: random occurrences of
black and white pixels
Common Types of Noise
Impulse noise: random occurrences of white
pixels

Original Impulse noise


Common Types of Noise
Gaussian noise: variations in intensity drawn
from a Gaussian normal distribution

Original Gaussian noise


A
Gaussian noise

>> noise = randn (size(img)).*sigma;


>> Output = img + noise;

Fig: M. Hebert
A
Gaussian noise
>> noise = randn (size(img)).*sigma;
>> Output = img + noise;

We take the size of image then randn generates a noise


signal that has a mean of zero and a standard deviation of
one, and if we scale that up by some sigma. That will spread
that out and make it bigger so essentially the noise with
mean of zero and a std of sigma.
Fig: M. Hebert
A
Gaussian noise

>> %Generate Gaussian Noise-- Pass some dimension to randn to generate a vector
or metix filled with random numbers.
>> some_num = randn([1 5]);%one row five cols.
>> disp(some_num);

-0.0571 0.2918 -0.6146 -0.0136 0.4121

>> some_num = randn([2 3]);


>> disp(some_num);

-0.9371 1.0945 0.0151


0.8352 0.3212 -1.1645
Gaussian noise

The center, or mean, for randn is zero, and


the standard deviation is one.

The standard deviation is a measure of how


spread out the distribution is.
This is a probability distribution,

which means getting back numbers that are close to zero


is highly likely,
whereas numbers far away from zero are less likely.
Gaussian noise

randn is actually sampling from a Gaussian


distribution. If we have enough samples and
distribute them among bins and we count how many
numbers landed in each bin, then we see a pattern
similar to the probability distribution function.
Effect of σ on Gaussian noise
Noise images: Images noise = randn(size(im)).*sigma
showing noise values
generated with
different sigma

σ = 2,8,32,64 sigma = 2 sigma = 8


Images shows noise
values themselves.

Grey is zero.
sigma = 32 sigma = 64
Effect of σ on Gaussian noise
Values of σ to use

• A σ of 1.0 would be tiny if the range is [0 255]


but huge if pixels went from [0.0 1.0].

• Matlab can do either and you need to be very


careful - if in doubt convert to doubles.
Displaying images in Matlab

Look at the Matlab function imshow()


imshow(im,[LOW HIGH])

will display the image im with value LOW as


black and HIGH as white.
Displaying images in Matlab

Look at the Matlab function imshow()


imshow(im,[ ])

will display the image im with the based on the

range of pixel values in im.


Matlab Code
folder: E:\DIP\training\applied
==========================================================================
scale.m
function result=scale(img,value)

result= value .* img;

end

% dolphin = imread('[Link]');

% imshow(scale(dolphin, 2));
Matlab Code
blend.m
----------------------------------------------------------------
function result=blend(img1, img2, value)
result = value * img1 +(1 - value)*img2;
End
======================
gaussianNoise.m
clc;
clear all;
noise = randn([1 10000]);
[n, x]=hist(noise, linspace(-3,3,21));
plot(x, n);
Matlab Code
bell shape
------------
>>
noise = randn([1 100]);
hist(noise, [-3 -2 -1 0 1 2 3]);
-------------------------------------
>> noise = randn([1 100]);

>> [n, x] = hist(noise, [-3 -2 -1 0 1 2 3]);%n is count and x is bin center


disp([x; n]);
plot(x, n);
Matlab Code
-----------------------------------
>> noise = randn([1 100]);
>> [n, x] = hist(noise, linspace(-3, 3, 7));
disp([x; n]);
plot(x, n);
----------------------------------
>> noise = randn([1 100]);
>> [n, x] = hist(noise, linspace(-3, 3, 21));% increase bins
disp([x; n]);
plot(x, n);
---------------------------------
>> noise = randn([1 1000]); %increasing data
>> [n, x] = hist(noise, linspace(-3, 3, 21));
disp([x; n]);
plot(x, n);
-------------
Matlab Code
>> img=imread('[Link]');
noise = randn(size(img));
noise = uint8(noise);
output = img + noise;
imshow(output);
-------------------------------
now scall up
>> img=imread('[Link]');
noise = randn(size(img)).*25; %100 ...
noise = uint8(noise);
output = img + noise;
imshow(output);
++++++++++++++++++++++++
Filtering
Gaussian noise
Removing Noise

• IF replacing each pixel with the average


of the values in the neighborhood.
1D Illustration
Replace each pixel with an average of all the values
in its neighborhood – ongoing proc…..

from: S. Marschner
1D Illustration- moving average with moving window
Replace each pixel with an average of all the values
in its neighborhood – ongoing proc…..

from: S. Marschner
1D Illustration
Replace each pixel with an average of all the values
in its neighborhood – ongoing proc…..

from: S. Marschner
1D Illustration
Replace each pixel with an average of all the values
in its neighborhood – ongoing proc…..

from: S. Marschner
Averaging ….why
• The "true" value of pixels are similar to the true value of
pixels nearby.

• The noise added to each pixel is done independently.


Moving Average (Weighted)
• Adding weights to moving average
• Weights [1, 1, 1, 1, 1] / 5 >> uniformly Dist.
Weighted Moving Average – ‘smoothness’

• The closer a pixel is to some reference pixel, the more


similar it would be.

• So the more it should contribute to an average.


Weighted Moving Average
• Non-uniform weights [1, 4, 6, 4, 1] / 16
Moving Average (Weighted) (comparison)
• Adding weights to moving average
• Weights [1, 1, 1, 1, 1] / 5 >> uniformly Dist.
Moving Average
Moving Average In 2D

F(x,y) G(x,y)

image: S. Seitz
Moving Average In 2D
F(x, y) G(x,y)
Moving Average In 2D
F(x,y) G(x,y)
Moving Average In 2D

F(x, y) G(x,y)
Moving Average In 2D

F(x, y) G(x,y)
Moving Average In 2D
F(x, y) G(x,y)
Correlation filtering - uniform weights

Say the averaging window size is 2k+1 x 2k+1:

Uniform Loop over all pixels in


weight for neighborhood around
each pixel image pixel F[i, j]
Correlation filtering - nonuniform weights

Now generalize to allow different weights depending on


neighboring pixel’s relative position:

Non-uniform weights
This is called cross-correlation, denoted G = H ⊗ F
The filter “kernel” or “mask” H[u, v] is the matrix of weights
in the linear combination.
Averaging filter
F(x,y) ⊗ H(u,v) = G(x,y)

1 1 1
1/9 1 1 1
1 1 1
“box filter”

G = H ⊗F
Smoothing by box averaging

Cause square are not smooth

original filtered
Averaging filter: Weaknesses

As squares aren't smooth and filtering an


image with a filter that is not "smooth" seems wrong if
we're trying to "blur" the image.

• Now simply think about a single spot of light viewed by an


out of focus camera the image would look something
like this:
Blurry spot as a function

D. Forsyth
Gaussian filter
Nearest neighboring pixels have the most influence

This kernel is an approximation


F(x, y) of a Gaussian function:
An Isotropic Gaussian

“Smoothing Kernel Proportional to:


“Circularly symmetric one”

D. Forsyth
Smoothing with a Gaussian (circularly symmetric)
Smoothing with not a Gaussian
Smoothing with Gaussian, and not a Gaussian
Gaussian filter: Shape vs. size

• The Gaussian we showed was isotropic


– circularly symmetric –
so it had only one parameter (sigma).

• Size of the filter (3x3, 5x5, 11x11) and then the shape of
the filter values (Gaussian distribution).
Gaussian filters

Standard deviation (𝜎) - determines extent of smoothing

𝜎 = 2 with 30 x 30 kernel 𝜎 = 5 with 30 x 30 kernel


Gaussian filters

Size of kernel or mask is not variance, larger with more smooth effect

𝜎 = 5 with 10 x 10 𝜎 = 5 with 30 x 30
kernel kernel
Gaussian filters
Gaussian filters
Now finalizing the two Gaussians

• Sigma as a width of a the Gaussian filter, as the


variance of smoothing of the blurring.
• In one case - the filter - sigma is a width in space
where as;

• With noise it's a variance in value (variance of the noise


function) (its intensity - the value).
• The bigger the noise sigma was the more likely that large
values of noise can be created.
Keeping the two Gaussians straight...

More Gaussian noise (like earlier) 𝜎

Wider Gaussian smoothing kernel 𝜎>>


Linearity and convolution
Linearity ….

• An operator H (or system) is linear if two properties


hold (f 1 and f 2 are some functions, a is a constant):

• Additivity :
H ( f 1 + f 2 ) = H ( f 1 ) + H ( f 2 ) (distributive law)

• Multiplicative scaling (Homogeneity):


H ( a • f 1) = a • H ( f 1 ) (constant scales) a times H applied to f1

Because it is sums and multiplies, the


“filtering” operation we were doing is linear.
What linearity is going to allow us to do is to build up a signal as
a function.

If we could sum up a bunch of things to make an image, then if


we apply a linear operator to that whole image, it's going to be
the same as the sum of applying that linear operator to each of the
pieces.
An impulse function...

• In the discrete world, an impulse is a very easy


signal to understand, it's just a value of 1 at a single location.
An impulse response

• If we have an unknown system and we "put in" an impulse, the


response is called the impulse response.

• So if the black box is linear you can describe H by h(x)


Filtering an impulse signal
So, let's take a look at what an impulse response looks like in an image.

What is the result of filtering the impulse signal (image) F with the
arbitrary kernel H?
Filtering an impulse signal
Filtering an impulse signal
Filtering an impulse signal
Filtering an impulse signal
Filtering an impulse signal

Assuming center
coordinate is "reference point".
Correlation vs Convolution
We had this problem, of when we put in through a correlation, we got back out
sort of this flip thing.
Convolution
Shift invariant:

• Operator behaves the same everywhere, i.e. the value of the


output depends only on the pattern in the image neighborhood,
rather than the position.
Properties of convolution convolution or correlation are built on
multiplication and
addition, these are linear operators.

Differentiation is also a linear operator.


Computational Complexity
Computational Complexity
Separability

• In some cases, filter is separable, meaning you can get the


square kernel H by convolving a single column vector by some
row vector:
r

c H
Separability
r

c H

G = H * F = (C * R) * F = C * (R * F)
• So we do two convolutions but each is W*N*N. So this is useful
if W is big enough such that 2 • W • N2 << W2 • N2
• Used to be very important. Still, if W=31, save a factor of ?.
Separability
r

c H

G = H * F = (C * R) * F = C * (R * F)
• So we do two convolutions but each is W*N*N. So this is useful
if W is big enough such that 2 • W • N2 << W2 • N2
• Used to be very important. Still, if W=31, save a factor of 15.
What different linear filters do..
Practice with linear filters
Practice with linear filters
Practice with linear filters
Filtering examples: sharpening
Different kinds of noise

• We said that Gaussian averaging was a reasonable thing


to do if the noise was independent at each pixel and
centered about zero such as if created by a Gaussian
or normal noise process.

• But there are other kinds of noise.


Different kinds of noise

Additive Gaussian noise Salt and pepper noise

You might also like