Lab Manual 5
Lab Manual 5
COMPUTER GRAPHICS
&
IMAGE PROCESSING
LAB MANUAL 5
LAB OBJECTIVE:
The objective of this lab is to understand & implement
1. Averaging Filtering
2. Median Filtering
3. Laplacian Filtering
imnoise
Add noise to an image
Syntax
J = imnoise(I,type)
J = imnoise(I,type,parameters)
Description
J = imnoise(I,type) adds noise of a given type to the intensity image I. type is a string that can have
one of these values.
J = imnoise(I,'gaussian',m,v) adds Gaussian white noise of mean m and variance v to the image I. The
default is zero mean noise with 0.01 variance.
J = imnoise(I,'localvar',V) adds zero-mean, Gaussian white noise of local variance V to the image I. V
is an array of the same size as I.
J = imnoise(I,'poisson') generates Poisson noise from the data instead of adding artificial noise to the
data. In order to respect Poisson statistics, the intensities of unit8 and uint16 images must correspond
to the number of photons (or any other quanta of information). Double-precision images are used
when the number of photons per pixel can be much larger than 65535 (but less than 10^12); the
intensity values vary between 0 and 1 and correspond to the number of photons divided by 10^12.
J = imnoise(I,'salt & pepper',d) adds salt and pepper noise to the image I, where d is the noise
density. This affects approximately d*prod(size(I)) pixels. The default is 0.05 noise density.
J = imnoise(I,'speckle',v) adds multiplicative noise to the image I, using the equation J = I+n*I, where
n is uniformly distributed random noise with mean 0 and variance v. The default for v is 0.04.
Class Support
I can be of class uint8, uint16, or double. The output image J is of the same class as I. If I has more
than two dimensions it is treated as a multidimensional intensity image and not as an RGB image.
Example
I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.02);
imshow(I)
figure, imshow(J)
fspecial
Create 2-D special filters
Syntax
h = fspecial(type)
h = fspecial(type,parameters)
Description
The following list shows the syntax for each filter type. Where applicable, additional parameters are
also shown
h = fspecial('average',hsize) returns an averaging filter h of size hsize. The argument hsize can be a
vector specifying the number of rows and columns in h, or it can be a scalar, in which case h is a
square matrix. The default value for hsize is [3 3].
h = fspecial('disk',radius) returns a circular averaging filter (pillbox) within the square matrix of side
2*radius+1. The default radius is 5
The filter becomes a vector for horizontal and vertical motions. The default len is 9 and the default
theta is 0, which corresponds to a horizontal motion of nine pixels.
h = fspecial('prewitt') returns a 3-by-3 filter h (shown below) that emphasizes horizontal edges by
approximating a vertical gradient. If you need to emphasize vertical edges, transpose the filter h'.
[111 000 -1 -1 -1 ]
h = fspecial('sobel') returns a 3-by-3 filter h (shown below) that emphasizes horizontal edges using the
smoothing effect by approximating a vertical gradient. If you need to emphasize vertical edges,
transpose the filter h'.
[121 000 -1 -2 -1 ]
Class Support
h is of class double.
Example
I = imread('cameraman.tif');
subplot(2,2,1);
imshow(I); title('Original Image');
H = fspecial('motion',20,45);
MotionBlur = imfilter(I,H,'replicate');
subplot(2,2,2);
imshow(MotionBlur);title('Motion Blurred Image');
H = fspecial('disk',10);
blurred = imfilter(I,H,'replicate');
subplot(2,2,3);
imshow(blurred); title('Blurred Image');
H = fspecial('unsharp');
sharpened = imfilter(I,H,'replicate');
subplot(2,2,4);
imshow(sharpened); title('Sharpened Image');
imfilter
Multidimensional image filtering
Syntax
B = imfilter(A,H)
B = imfilter(A,H,option1,option2,...)
Description
B = imfilter(A,H) filters the multidimensional array A with the multidimensional filter H. The array A
can be a nonsparse numeric array of any class and dimension. The result B has the same size and
class as A.
Each element of the output B is computed using double-precision floating point. If A is an integer
array, then output elements that exceed the range of the integer type are truncated, and fractional
values are rounded.
Examples
originalRGB = imread('peppers.png');
imview(originalRGB)
Apply the filter, using imfilter, to the image rgb to create a new image, rgb2.
medfilt2
Perform two-dimensional median filtering
Syntax
B = medfilt2(A,[m n])
B = medfilt2(A)
B = medfilt2(A,'indexed',...)
Description
Median filtering is a nonlinear operation often used in image processing to reduce "salt and pepper"
noise. Median filtering is more effective than convolution when the goal is to simultaneously reduce
noise and preserve edges.
B = medfilt2(A,[m n]) performs median filtering of the matrix A in two dimensions. Each output pixel
contains the median value in the m-by-n neighborhood around the corresponding pixel in the input
image. medfilt2 pads the image with 0's on the edges, so the median values for the points within [m
n]/2 of the edges might appear distorted.
B = medfilt2(A) performs median filtering of the matrix A using the default 3-by-3 neighborhood.
Example
This example adds salt and pepper noise to an image, then restores the image using medfilt2.
I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.02);
K = medfilt2(J);
imview(J), imview(K)
******************************************************************
TASK
Using the built in functions described above, implement the following filtering
1. Averaging Filtering.
2. Median Filtering.
3. Laplacian Filtering.
******************************************************************