0% found this document useful (0 votes)
100 views

Write A MATLAB Code For The Following: 1) Average Filter/Box Filter A) On Image: I) 3 3 Mask

The document describes MATLAB code implementations of common image filtering techniques, including average, weighted average, and median filters, on both images and matrices. Code examples are provided applying 3x3, 5x5, and 7x7 masks for each filter type. The average filter uses a uniform mask, weighted average filter assigns different weights, and median filter selects the median value in the mask region. MAX and MIN filters are also briefly mentioned.

Uploaded by

TRUE LOVERS
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Write A MATLAB Code For The Following: 1) Average Filter/Box Filter A) On Image: I) 3 3 Mask

The document describes MATLAB code implementations of common image filtering techniques, including average, weighted average, and median filters, on both images and matrices. Code examples are provided applying 3x3, 5x5, and 7x7 masks for each filter type. The average filter uses a uniform mask, weighted average filter assigns different weights, and median filter selects the median value in the mask region. MAX and MIN filters are also briefly mentioned.

Uploaded by

TRUE LOVERS
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Muhammad Umair Khan FA18-BCE-010

Write a MATLAB code for the following:


1) Average Filter/Box Filter
a) On Image:
i) 3*3 Mask:
Code:
% Average Filter/Box Filter
img=imread('cameraman.png');
%img=[1,2,3;4,5,6;7,8,9];
% Add Noise
Noi_img = imnoise(img,'salt & pepper', 0.02);
% Mask Definition
f=1/9*[1,1,1;1,1,1;1,1,1];
%Apply filter2 function
de_noi=filter2(f,Noi_img);
%de_noi=img.*f
figure;
subplot(1,3,1);imshow(img);title('Original image')
subplot(1,3,2);imshow(Noi_img);title('Noisy image')
subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')
Output:

ii) 5*5 Mask:


Code:
% Average Filter/Box Filter
img=imread('cameraman.png');
%img=[1,2,3;4,5,6;7,8,9];
% Add Noise
Noi_img = imnoise(img,'salt & pepper', 0.02);
% Mask Definition
f=1/25*[1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;1,1,1,1,1];
%Apply filter2 function
de_noi=filter2(f,Noi_img);
%de_noi=img.*f
figure;
subplot(1,3,1);imshow(img);title('Original image')
subplot(1,3,2);imshow(Noi_img);title('Noisy image')
subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')

1|Page Digital image processing


Muhammad Umair Khan FA18-BCE-010

Output:

iii) 7*7 Mask:


Code:
% Average Filter/Box Filter
img=imread('cameraman.png');
%img=[1,2,3;4,5,6;7,8,9];
% Add Noise
Noi_img = imnoise(img,'salt & pepper', 0.02);
% Mask Definition
f=1/49*[1,1,1,1,1,1,1;1,1,1,1,1,1,1;1,1,1,1,1,1,1;1,1,1,1,1,1,1;1,1,1,1,1,1,1;
1,1,1,1,1,1,1;1,1,1,1,1,1,1];
%Apply filter2 function
de_noi=filter2(f,Noi_img);
%de_noi=img.*f
figure;
subplot(1,3,1);imshow(img);title('Original image')
subplot(1,3,2);imshow(Noi_img);title('Noisy image')
subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')
Output:

b) On Matrix:
i) 3*3 Mask:
Code:
% Average Filter/Box Filter
img=[1,2,3;4,5,6;7,8,9];
% Mask Definition
f=1/9*[1,1,1;1,1,1;1,1,1];
de_noi=img.*f

2|Page Digital image processing


Muhammad Umair Khan FA18-BCE-010

Result:
de_noi =

0.1111 0.2222 0.3333

0.4444 0.5556 0.6667

0.7778 0.8889 1.0000


ii) 5*5 Mask:
Code:
% Average Filter/Box Filter
img=[1,2,3,2,1;4,5,6,2,5;7,8,9,8,7;1,3,5,7,9;2,4,6,8,9];
% Mask Definition
f=1/25*[1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;1,1,1,1,1];
de_noi=img.*f
Result:
de_noi =

0.0400 0.0800 0.1200 0.0800 0.0400

0.1600 0.2000 0.2400 0.0800 0.2000

0.2800 0.3200 0.3600 0.3200 0.2800

0.0400 0.1200 0.2000 0.2800 0.3600

0.800 0.1600 0.2400 0.3200 0.3600


iii) 7*7 Mask:
Code:
% Average Filter/Box Filter
img=[1,2,3,2,1,4,3;4,5,6,2,5,4,3;7,8,9,8,7,4,6;1,3,5,7,9,2,5;2,4,6,8,9,1,4;3,1
,2,4,6,5,7;5,6,2,7,2,1,5];
% Mask Definition
f=1/49*[1,1,1,1,1,1,1;1,1,1,1,1,1,1;1,1,1,1,1,1,1;1,1,1,1,1,1,1;1,1,1,1,1,1,1;
1,1,1,1,1,1,1;1,1,1,1,1,1,1];
de_noi=img.*f
Result:
de_noi =

0.0204 0.0408 0.0612 0.0408 0.0204 0.0816 0.0612

0.0816 0.1020 0.1224 0.0408 0.1020 0.0816 0.0612

0.1429 0.1633 0.1837 0.1633 0.1429 0.0816 0.1224

0.0204 0.0612 0.1020 0.1429 0.1837 0.0408 0.1020

0.0408 0.0816 0.1224 0.1633 0.1837 0.0204 0.0816

0.0612 0.0204 0.0408 0.0816 0.1224 0.1020 0.1429

0.1020 0.1224 0.0408 0.1429 0.0408 0.0204 0.10

3|Page Digital image processing


Muhammad Umair Khan FA18-BCE-010

2) Weighted Average Filter


b) On Image:
i) 3*3 Mask:
Code:
% Weighted Average Filter
img=imread('cameraman.png');
% Add Noise
Noi_img = imnoise(img,'salt & pepper', 0.02);
% Mask Definition
f=1/16*[1,2,1;2,4,2;1,2,1];
% Apply filter2 function
de_noi=filter2(f,Noi_img);
figure;
subplot(1,3,1);imshow(img);title('Original image')
subplot(1,3,2);imshow(Noi_img);title('Noisy image')
subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')
Output:

ii) 5*5 Mask:


Code:
% Weighted Average Filter
img=imread('cameraman.png');
% Add Noise
Noi_img = imnoise(img,'salt & pepper', 0.02);
% Mask Definition
f=1/32*[1,1,1,1,1;1,1,2,1,1;1,2,4,2,1;1,1,2,1,1;1,1,1,1,1];
% Apply filter2 function
de_noi=filter2(f,Noi_img);
figure;
subplot(1,3,1);imshow(img);title('Original image')
subplot(1,3,2);imshow(Noi_img);title('Noisy image')
subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')
Output:

iii) 7*7
Mask:
Code:

4|Page Digital image processing


Muhammad Umair Khan FA18-BCE-010

% Weighted Average Filter


img=imread('cameraman.png');
% Add Noise
Noi_img = imnoise(img,'salt & pepper', 0.02);
% Mask Definition
f=1/56*[1,1,1,1,1,1,1;1,1,1,1,1,1,1;1,1,1,2,1,1,1;1,1,2,4,2,1,1;1,1,1,2,1,1,1;
1,1,1,1,1,1,1;1,1,1,1,1,1,1];
% Apply filter2 function
de_noi=filter2(f,Noi_img);
figure;
subplot(1,3,1);imshow(img);title('Original image')
subplot(1,3,2);imshow(Noi_img);title('Noisy image')
subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')
Output:

b) On Matrix:
i) 3*3 Mask:
Code:
% Weighted Average Filter
img=[1,2,3;4,5,6;7,8,9];
% Mask Definition
f=1/16*[1,2,1;2,4,2;1,2,1];
de_noi=img.*f
Result:
de_noi =

0.0625 0.2500 0.1875

0.5000 1.2500 0.7500

0.4375 1.0000 0.5625

ii) 5*5 Mask:


Code:
% Weighted Average Filter

5|Page Digital image processing


Muhammad Umair Khan FA18-BCE-010

img=[1,2,3,2,1;4,5,6,2,5;7,8,9,8,7;1,3,5,7,9;2,4,6,8,9];
% Mask Definition
f=1/32*[1,1,1,1,1;1,1,2,1,1;1,2,4,2,1;1,1,2,1,1;1,1,1,1,1];
de_noi=img.*f
Result:
de_noi =

0.0313 0.0625 0.0938 0.0625 0.0313

0.1250 0.1563 0.3750 0.0625 0.1563

0.2188 0.5000 1.1250 0.5000 0.2188

0.0313 0.0938 0.3125 0.2188 0.2813

0.0625 0.1250 0.1875 0.2500 0.2813

iii) 7*7 Mask:


Code:
% Weighted Average Filter
img=[1,2,3,2,1,4,3;4,5,6,2,5,4,3;7,8,9,8,7,4,6;1,3,5,7,9,2,5;2,4,6,8,9,1,4;3,1
,2,4,6,5,7;5,6,2,7,2,1,5];
% Mask Definition
f=1/56*[1,1,1,1,1,1,1;1,1,1,1,1,1,1;1,1,1,2,1,1,1;1,1,2,4,2,1,1;1,1,1,2,1,1,1;
1,1,1,1,1,1,1;1,1,1,1,1,1,1];
de_noi=img.*f
Result:
de_noi =

0.0179 0.0357 0.0536 0.0357 0.0179 0.0714 0.0536

0.0714 0.0893 0.1071 0.0357 0.0893 0.0714 0.0536

0.1250 0.1429 0.1607 0.2857 0.1250 0.0714 0.1071

0.0179 0.0536 0.1786 0.5000 0.3214 0.0357 0.0893

0.0357 0.0714 0.1071 0.2857 0.1607 0.0179 0.0714

0.0536 0.0179 0.0357 0.0714 0.1071 0.0893 0.1250

0.0893 0.1071 0.0357 0.1250 0.0357 0.0179 0.0893

3) Median Filter
a) On Image:
6|Page Digital image processing
Muhammad Umair Khan FA18-BCE-010

i) 3*3 Mask:
Code:
% Median Filter
img=imread('cameraman.png');
% Add Noise
Noi_img = imnoise(img,'salt & pepper', 0.02);
% Apply medfilt2 function
de_noi=medfilt2(Noi_img,[3 3]);
figure;
subplot(1,3,1);imshow(img);title('Original image')
subplot(1,3,2);imshow(Noi_img);title('Noisy image')
subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')

ii) 5*5
Mask:
Code:
% Median Filter

img=imread('cameraman.png');
% Add Noise
Noi_img = imnoise(img,'salt & pepper', 0.02);
% Apply medfilt2 function
de_noi=medfilt2(Noi_img,[5 5]);
figure;
subplot(1,3,1);imshow(img);title('Original image')
subplot(1,3,2);imshow(Noi_img);title('Noisy image')
subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')

iii) 7*7 Mask:


Code:
% Median Filter
img=imread('cameraman.png');
% Add Noise
Noi_img = imnoise(img,'salt & pepper', 0.02);
% Apply medfilt2 function
de_noi=medfilt2(Noi_img,[7 7]);

7|Page Digital image processing


Muhammad Umair Khan FA18-BCE-010

figure;
subplot(1,3,1);imshow(img);title('Original image')
subplot(1,3,2);imshow(Noi_img);title('Noisy image')
subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')

4) MAX & MIN filter


a) On Image:
i) 3*3 Mask:
Code:
Original=imread('cameraman.png'); %Read in image
minf=@(x) min(x(:)); %set 'min()' filter
maxf=@(x)max(x(:)); %set 'max()' filter
min_Image=nlfilter(Original,[3 3],minf); %Apply over 3 x 3 neighbourhood
max_Image=nlfilter(Original,[3 3],maxf); %Apply over 3 x 3 neighbourhood
subplot(2,2,1), imshow(Original), title('Original'); %Display image
subplot(2,2,2), imshow(min_Image), title('Min'); %Display min image
subplot(2,2,3), imshow(max_Image), title('Max'); %Display max image

ii) 5*5 Mask:


Code:
Original=imread('cameraman.png'); %Read in image
minf=@(x) min(x(:)); %set 'min()' filter
maxf=@(x)max(x(:)); %set 'max()' filter
min_Image=nlfilter(Original,[5 5],minf); %Apply over 5 x 5 neighbourhood
max_Image=nlfilter(Original,[5 5],maxf); %Apply over 5 x 5 neighbourhood

8|Page Digital image processing


Muhammad Umair Khan FA18-BCE-010

subplot(2,2,1), imshow(Original), title('Original'); %Display image


subplot(2,2,2), imshow(min_Image), title('Min'); %Display min image
subplot(2,2,3), imshow(max_Image), title('Max'); %Display max image

iii) 7*7 Mask:


Code:
Original=imread('cameraman.png'); %Read in image
minf=@(x) min(x(:)); %set 'min()' filter
maxf=@(x)max(x(:)); %set 'max()' filter
min_Image=nlfilter(Original,[7 7],minf); %Apply over 7 x 7 neighbourhood
max_Image=nlfilter(Original,[7 7],maxf); %Apply over 7 x 7 neighbourhood

9|Page Digital image processing


Muhammad Umair Khan FA18-BCE-010

subplot(2,2,1), imshow(Original), title('Original'); %Display image


subplot(2,2,2), imshow(min_Image), title('Min'); %Display min image
subplot(2,2,3), imshow(max_Image), title('Max'); %Display max image

10 | P a g e Digital image processing

You might also like