View File - University of Engineering and Technology, Taxila

advertisement
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
COMPUTER GRAPHICS
&
IMAGE PROCESSING
LAB MANUAL 5
PREPARED BY:: ENGR. ALI JAVED
Computer Graphics & Image Processing
7 th Term-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
LINEAR & NON LINEAR SPATIAL FILTERS
LAB OBJECTIVE:
The objective of this lab is to understand & implement
1. Averaging Filtering
2. Median Filtering
3. Laplacian Filtering
4. imnoise, fspecial and imfilter functions
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,type,parameters) accepts an algorithm type plus additional modifying parameters
particular to the type of algorithm chosen. If you omit these arguments, imnoise uses default values for
the parameters. Here are examples of the noise types and their parameters:
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.
Computer Graphics & Image Processing
7 th Term-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
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,'localvar',image_intensity,var) adds zero-mean, Gaussian noise to an image I, where the
local variance of the noise, var, is a function of the image intensity values in I. The image_intensity and
var arguments are vectors of the same size, and plot(image_intensity,var) plots the functional
relationship between noise variance and image intensity. The image_intensity vector must contain
normalized intensity values ranging from 0 to 1.
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)
Computer Graphics & Image Processing
7 th Term-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
fspecial
Create 2-D special filters
Syntax
h = fspecial(type)
h = fspecial(type,parameters)
Computer Graphics & Image Processing
7 th Term-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Description
h = fspecial(type) creates a two-dimensional filter h of the specified type. fspecial returns h as a
correlation kernel, which is the appropriate form to use with imfilter. type is a string having one of
these values.
h = fspecial(type,parameters) accepts a filter type plus additional modifying parameters particular to
the type of filter chosen. If you omit these arguments, fspecial uses default values for the parameters.
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
h = fspecial('gaussian',hsize,sigma) returns a rotationally symmetric Gaussian lowpass filter of size
hsize with standard deviation sigma (positive). 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]; the default value for sigma is 0.5.
h = fspecial('laplacian',alpha) returns a 3-by-3 filter approximating the shape of the two-dimensional
Laplacian operator. The parameter alpha controls the shape of the Laplacian and must be in the range
0.0 to 1.0. The default value for alpha is 0.2.
h = fspecial('log',hsize,sigma) returns a rotationally symmetric Laplacian of Gaussian filter of size hsize
with standard deviation sigma (positive). 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 [5
5] and 0.5 for sigma.
h = fspecial('motion',len,theta) returns a filter to approximate, once convolved with an image, the
linear motion of a camera by len pixels, with an angle of theta degrees in a counterclockwise direction.
Computer Graphics & Image Processing
7 th Term-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
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 ]
To find vertical edges, or for x-derivatives, use h'.
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 ]
h = fspecial('unsharp',alpha) returns a 3-by-3 unsharp contrast enhancement filter. fspecial creates the
unsharp filter from the negative of the Laplacian filter with parameter alpha. alpha controls the shape
of the Laplacian and must be in the range 0.0 to 1.0. The default value for alpha is 0.2.
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');
Computer Graphics & Image Processing
7 th Term-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Computer Graphics & Image Processing
7 th Term-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
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.
B = imfilter(A,H,option1,option2,...) performs multidimensional filtering according to the specified
options. Option arguments can have the following values.
Examples
Read a color image into the workspace and view it.
originalRGB = imread('peppers.png');
imview(originalRGB)
Create a filter, h, that can be used to approximate linear camera motion
h = fspecial('motion', 50, 45);
Computer Graphics & Image Processing
7 th Term-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Apply the filter, using imfilter, to the image rgb to create a new image, rgb2.
filteredRGB = imfilter(originalRGB, h);
imview(filteredRGB)
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.
B = medfilt2(A,'indexed',...) processes A as an indexed image, padding with 0's if the class of A is
uint8, or 1's if the class of A is double.
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)
Computer Graphics & Image Processing
7 th Term-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
******************************************************************
TASK
Using the built in functions described above, implement the following filtering
1. Averaging Filtering.
2. Median Filtering.
3. Laplacian Filtering.
******************************************************************
Computer Graphics & Image Processing
7 th Term-SE
UET Taxila
Download