0% found this document useful (0 votes)
72 views4 pages

Experiment No 14

This document discusses using the imfilter function in MATLAB to perform linear filtering on images through convolution and correlation. It explains that imfilter can filter an image using a convolution or correlation kernel, with correlation being the default. The document provides examples of computing output pixels using convolution and correlation. It also discusses options for imfilter like the filtering mode, boundary handling, and output size. Code examples are given to demonstrate applying an average filter to an image using imfilter in correlation and convolution mode.

Uploaded by

Mohamd barca
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)
72 views4 pages

Experiment No 14

This document discusses using the imfilter function in MATLAB to perform linear filtering on images through convolution and correlation. It explains that imfilter can filter an image using a convolution or correlation kernel, with correlation being the default. The document provides examples of computing output pixels using convolution and correlation. It also discusses options for imfilter like the filtering mode, boundary handling, and output size. Code examples are given to demonstrate applying an average filter to an image using imfilter in correlation and convolution mode.

Uploaded by

Mohamd barca
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
You are on page 1/ 4

Multimedia Computing Lab. Ruaa Hussam Al-Mallah/Lecturer Asst.

Computer Technical Eng. Dept.


4th Class

EXPERIMENT NO.14
Filtering with imfilter

Introduction :
imfilter: The syntax in matlab is:
B = imfilter(A,H,OPTION1,OPTION2,....)

- This filters the multidimensional array A with the multidimensional filter H.


- The following table summarizes some of the additional options available with imfilter.
Correlation and Convolution Options

Filtering mode Description


'corr' Filtering is done using correlation. This is the default.
'conv' Filtering is done using convolution.

Boundary Options
Option1 Description
X Input array values outside the bounds of the array are implicitly assumed to have
the value X. When no boundary option is specified, imfilter uses X = 0.
'symmetric' Input array values outside the bounds of the array are computed by mirror-
reflecting the array across the array border.
'replicate' Input array values outside the bounds of the array are assumed to equal the
nearest array border value.
'circular' Input array values outside the bounds of the array are computed by implicitly
assuming the input array is periodic.
Output Size Options

Option Description
'same' The output array is the same size as the input array. This is the default behavior
when no output size options are specified.

'full' The output array is the full filtered result, and so is larger than the input array.

Imfilter—Filtering Mode (Convolution)


Linear filtering of an image is accomplished through an operation called convolution.
Convolution is a neighborhood operation in which each output pixel is the weighted sum of
neighboring input pixels. The matrix of weights is called the convolution kernel, also known

EXP. No. 14: Filtering with imfilter Page 43


Multimedia Computing Lab. Ruaa Hussam Al-Mallah/Lecturer Asst.
Computer Technical Eng. Dept.
4th Class

as the filter. A convolution kernel is a correlation kernel that has been rotated 180 degrees.

For example, suppose the image is


A = [17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9]
And the convolution kernel
is h = [8 1 6
3 5 7
4 9 2]
The following figure shows how to compute the (2,4) output pixel using these steps:
1. Rotate the convolution kernel 180 degrees about its center element.
2. Slide the center element of the convolution kernel so that it lies on top of the (2,4)
element of A.
3. Multiply each weight in the rotated convolution kernel by the pixel of A underneath.
4. Sum the individual products from step 3. Hence the (2,4) output pixel is

Computing the (2,4) Output of Convolution

EXP. No. 14: Filtering with imfilter Page 44


Multimedia Computing Lab. Ruaa Hussam Al-Mallah/Lecturer Asst.
Computer Technical Eng. Dept.
4th Class

Imfilter—Filtering Mode (Correlation)


The operation called correlation is closely related to convolution. In correlation, the value of
an output pixel is also computed as a weighted sum of neighboring pixels. The difference is
that the matrix of weights, in this case called the correlation kernel, is not rotated during the
computation. The Image Processing Toolbox filter design functions return correlation kernels.
The following figure shows how to compute the (2,4) output pixel of the correlation of A:

Computing the (2,4) Output of Correlation

Assuming h is a correlation kernel instead of a convolution kernel, using these steps:


5. Slide the center element of the correlation kernel so that lies on top of the
(2,4) element of A.
6. Multiply each weight in the correlation kernel by the pixel of A underneath.
7. Sum the individual products from step 3.

The (2,4) output pixel from the correlation is

Performing Linear Filtering of Images using imfilter


Filtering of images, either by correlation or convolution can be performed using the toolbox
function imfilter.

EXP. No. 14: Filtering with imfilter Page 45


Multimedia Computing Lab. Ruaa Hussam Al-Mallah/Lecturer Asst.
Computer Technical Eng. Dept.
4th Class

Correlation and Convolution Options


The imfilter function can perform filtering using either correlation or convolution. It uses
correlation by default, because the filter design functions and the fspecial function produce
correlation kernels.
However, if you want to perform filtering using convolution instead, you can pass the
string 'conv' as an optional input argument to imfilter.
This example filters an image with a 5-by-5 filter containing equal weights. Such a filter is often
called an averaging filter.

Procedure:
1- Read the image
I = imread(peppers.png);
2- Design the average filter 5x5
h = ones(5,5)/25;
3- Apply the imfilter in coorelation model
I2 = imfilter(I,h);
4- Apply the imfilter in covelution model
I3 = imfilter(I,h,'conv');
5- Display the image in step 3 and 4
subplot(131),imshow(I), title('Original Image');
subplot(132), imshow(I2), title('Filtered Image with corr')
subplot(133), imshow(I3), title('Filtered Image with conv')

Report:
1- Write a code in Matlab to apply imfilter with gray scale image and average filter in mask
7x7 and replication mode?
2- Write a code in Matlab to apply imfilter with gray scale image and average filter in mask
7x7 and symmatric mode?
3- Write a code in Matlab to apply imfilter with gray scale image and average filter in mask
7x7 and circular mode?
4- Discuss the result in all steps.

EXP. No. 14: Filtering with imfilter Page 46

You might also like