Sie sind auf Seite 1von 9

Experiment-4

Date: / /2013

AIM:
(a) Read given image in MATLAB, convolve the image with 3x3 mask
consisting of all ones and show that it performs average operation which
results in blurring of image. Also, analyze the impact of increasing the size
of mask to 5x5.

(b) Read an image and then corrupt it by salt and pepper, Gaussian,
speckle noise then apply averaging filter with 3x3 mask and then with
5X5 mask. Compare the results.

(c) Now apply MEDIAN filter of mask 3x3 to each image. Comment on the
result. (With and without using inbuilt functions)

Goswami Pratikgiri R.(120420704006) Page 1


(a)Averaging using 33 and 55 masks

clear all;
clc;
img = imread('testpat1.png');
a=double(img);
w=[1 1 1; 1 1 1; 1 1 1]/9;
[row col]=size(a);
for x=2:1:row-1
for y=2:1:col-1
a1(x,y)=w(1)*a(x-1,y-1)+w(2)*a(x-1,y)+w(3)*a(x-1,y+1)+w(4)*a(x,y-
1)+w(5)*a(x,y)+w(6)*a(x,y+1)+w(7)*a(x+1,y-1)+w(8)*a(x+1,y)+w(9)*a(x+1,y+1);
end
end
subplot(1,2,1); imshow(img); title('Original Image');
subplot(1,2,2); imshow(uint8(a1)); title('Image after averaging using mask of
3 * 3');

Goswami Pratikgiri R.(120420704006) Page 2


clear all;
clc;
img = imread('testpat1.png');
a=double(img);
w=[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]/25;
[row col]=size(a);
for x=3:1:row-2
for y=3:1:col-2
a1(x,y)=w(1)*a(x-2,y-2)+w(2)*a(x-2,y-1)+w(3)*a(x-2,y)+w(4)*a(x-
2,y+1)+w(5)*a(x-2,y+2)+w(6)*a(x-1,y-2)+w(7)*a(x-1,y-1)+w(8)*a(x-
1,y)+w(9)*a(x-1,y-1)+w(10)*a(x-1,y+2)+w(11)*a(x,y-2)+w(12)*a(x,y-
1)+w(13)*a(x,y)+w(14)*a(x,y+1)+w(15)*a(x,y+2)+w(16)*a(x+1,y-2)+w(17)*a(x+1,y-
1)+w(18)*a(x+1,y)+w(19)*a(x+1,y+1)+w(20)*a(x+1,y+2)+w(21)*a(x+2,y-
2)+w(22)*a(x+2,y-1)+w(23)*a(x+2,y)+w(24)*a(x+2,y+1)+w(25)*a(x+2,y+2);
end
end
subplot(1,2,1); imshow(img); title('Original Image');
subplot(1,2,2); imshow(uint8(a1)); title('Image after averaging using mask of
5 * 5');

Goswami Pratikgiri R.(120420704006) Page 3


(b)Averaging on noisy images

33 mask
clear all;
clc;
img = imread('testpat1.png');

%---Gaussian---%
ab=imnoise(img,'gaussian');
a=double(ab);
w=[1 1 1; 1 1 1; 1 1 1]/9;
[row col]=size(a);
for x=2:1:row-1
for y=2:1:col-1
a1(x,y)=w(1)*a(x-1,y-1)+w(2)*a(x-1,y)+w(3)*a(x-1,y+1)+w(4)*a(x,y-
1)+w(5)*a(x,y)+w(6)*a(x,y+1)+w(7)*a(x+1,y-1)+w(8)*a(x+1,y)+w(9)*a(x+1,y+1);
end
end
figure;
subplot(1,3,1); imshow(img); title('Original Image');
subplot(1,3,2); imshow(uint8(a)); title('Image with Guassian noise');
subplot(1,3,3); imshow(uint8(a1)); title('averaging');

%---Salt n Pepper---%
ab=imnoise(img,'salt & pepper');
a=double(ab);
w=[1 1 1; 1 1 1; 1 1 1]/9;
[row col]=size(a);
for x=2:1:row-1
for y=2:1:col-1
a1(x,y)=w(1)*a(x-1,y-1)+w(2)*a(x-1,y)+w(3)*a(x-1,y+1)+w(4)*a(x,y-
1)+w(5)*a(x,y)+w(6)*a(x,y+1)+w(7)*a(x+1,y-1)+w(8)*a(x+1,y)+w(9)*a(x+1,y+1);
end
end
figure;
subplot(1,3,1); imshow(img); title('Original Image');
subplot(1,3,2); imshow(uint8(a)); title('Image with salt & pepper noise');
subplot(1,3,3); imshow(uint8(a1)); title('averaging');

%---speckle---%
ab=imnoise(img,'speckle');
a=double(ab);
w=[1 1 1; 1 1 1; 1 1 1]/9;
[row col]=size(a);
for x=2:1:row-1
for y=2:1:col-1
a1(x,y)=w(1)*a(x-1,y-1)+w(2)*a(x-1,y)+w(3)*a(x-1,y+1)+w(4)*a(x,y-
1)+w(5)*a(x,y)+w(6)*a(x,y+1)+w(7)*a(x+1,y-1)+w(8)*a(x+1,y)+w(9)*a(x+1,y+1);
end
end
figure;
subplot(1,3,1); imshow(img); title('Original Image');

Goswami Pratikgiri R.(120420704006) Page 4


subplot(1,3,2); imshow(uint8(a)); title('Image with speckle noise');
subplot(1,3,3); imshow(uint8(a1)); title('averaging');

Goswami Pratikgiri R.(120420704006) Page 5


55 mask
clear all;
clc;
img = imread('testpat1.png');

%---Gaussian---%
ab=imnoise(img,'gaussian');
a=double(ab);
w=[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]/25;
[row col]=size(a);
for x=3:1:row-2
for y=3:1:col-2
a1(x,y)=w(1)*a(x-2,y-2)+w(2)*a(x-2,y-1)+w(3)*a(x-2,y)+w(4)*a(x-
2,y+1)+w(5)*a(x-2,y+2)+w(6)*a(x-1,y-2)+w(7)*a(x-1,y-1)+w(8)*a(x-
1,y)+w(9)*a(x-1,y-1)+w(10)*a(x-1,y+2)+w(11)*a(x,y-2)+w(12)*a(x,y-
1)+w(13)*a(x,y)+w(14)*a(x,y+1)+w(15)*a(x,y+2)+w(16)*a(x+1,y-2)+w(17)*a(x+1,y-
1)+w(18)*a(x+1,y)+w(19)*a(x+1,y+1)+w(20)*a(x+1,y+2)+w(21)*a(x+2,y-
2)+w(22)*a(x+2,y-1)+w(23)*a(x+2,y)+w(24)*a(x+2,y+1)+w(25)*a(x+2,y+2);
end
end
figure;
subplot(1,3,1); imshow(img); title('Original Image');
subplot(1,3,2); imshow(uint8(a)); title('Image with Guassian noise');
subplot(1,3,3); imshow(uint8(a1)); title('averaging');

%---salt & pepper---%


ab=imnoise(img,'salt & pepper');
a=double(ab);
w=[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]/25;
[row col]=size(a);
for x=3:1:row-2
for y=3:1:col-2
a1(x,y)=w(1)*a(x-2,y-2)+w(2)*a(x-2,y-1)+w(3)*a(x-2,y)+w(4)*a(x-
2,y+1)+w(5)*a(x-2,y+2)+w(6)*a(x-1,y-2)+w(7)*a(x-1,y-1)+w(8)*a(x-
1,y)+w(9)*a(x-1,y-1)+w(10)*a(x-1,y+2)+w(11)*a(x,y-2)+w(12)*a(x,y-
1)+w(13)*a(x,y)+w(14)*a(x,y+1)+w(15)*a(x,y+2)+w(16)*a(x+1,y-2)+w(17)*a(x+1,y-
1)+w(18)*a(x+1,y)+w(19)*a(x+1,y+1)+w(20)*a(x+1,y+2)+w(21)*a(x+2,y-
2)+w(22)*a(x+2,y-1)+w(23)*a(x+2,y)+w(24)*a(x+2,y+1)+w(25)*a(x+2,y+2);
end
end
figure;
subplot(1,3,1); imshow(img); title('Original Image');
subplot(1,3,2); imshow(uint8(a)); title('Image with salt & pepper');
subplot(1,3,3); imshow(uint8(a1)); title('averaging');

%---speckle---%
ab=imnoise(img,'speckle');
a=double(ab);
w=[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]/25;
[row col]=size(a);
for x=3:1:row-2
for y=3:1:col-2
a1(x,y)=w(1)*a(x-2,y-2)+w(2)*a(x-2,y-1)+w(3)*a(x-2,y)+w(4)*a(x-
2,y+1)+w(5)*a(x-2,y+2)+w(6)*a(x-1,y-2)+w(7)*a(x-1,y-1)+w(8)*a(x-
1,y)+w(9)*a(x-1,y-1)+w(10)*a(x-1,y+2)+w(11)*a(x,y-2)+w(12)*a(x,y-

Goswami Pratikgiri R.(120420704006) Page 6


1)+w(13)*a(x,y)+w(14)*a(x,y+1)+w(15)*a(x,y+2)+w(16)*a(x+1,y-2)+w(17)*a(x+1,y-
1)+w(18)*a(x+1,y)+w(19)*a(x+1,y+1)+w(20)*a(x+1,y+2)+w(21)*a(x+2,y-
2)+w(22)*a(x+2,y-1)+w(23)*a(x+2,y)+w(24)*a(x+2,y+1)+w(25)*a(x+2,y+2);
end
end
figure;
subplot(1,3,1); imshow(img); title('Original Image');
subplot(1,3,2); imshow(uint8(a)); title('Image with speckle noise');
subplot(1,3,3); imshow(uint8(a1)); title('averaging');

Goswami Pratikgiri R.(120420704006) Page 7


(c) Median filter

clear all;
clc;

img = imread('cameraman.tif');
ab=imnoise(img,'salt & pepper');
a=double(ab);
b=a;
[row col]=size(a);
for x=2:1:row-1
for y=2:1:col-1
a1=[a(x-1,y-1) a(x-1,y) a(x-1,y+1) a(x,y-1) a(x,y) a(x,y+1) a(x+1,y-
1) a(x+1,y) a(x+1,y+1)];

a2=sort(a1);
med=a2(5);
b(x,y)=med;

end
end
figure;
subplot(1,3,1); imshow(img); title('Original Image');
subplot(1,3,2); imshow(uint8(ab)); title('salt & pepper noise');
subplot(1,3,3); imshow(uint8(b)); title('Median filtering');

ab=imnoise(img,'gaussian');
a=double(ab);
b=a;
[row col]=size(a);
for x=2:1:row-1
for y=2:1:col-1
a1=[a(x-1,y-1) a(x-1,y) a(x-1,y+1) a(x,y-1) a(x,y) a(x,y+1) a(x+1,y-
1) a(x+1,y) a(x+1,y+1)];

a2=sort(a1);
med=a2(5);
b(x,y)=med;

end
end
figure;
subplot(1,3,1); imshow(img); title('Original Image');
subplot(1,3,2); imshow(uint8(ab)); title('gaussian noise');
subplot(1,3,3); imshow(uint8(b)); title('Median filtering');

ab=imnoise(img,'speckle');
a=double(ab);
b=a;
[row col]=size(a);
for x=2:1:row-1

Goswami Pratikgiri R.(120420704006) Page 8


for y=2:1:col-1
a1=[a(x-1,y-1) a(x-1,y) a(x-1,y+1) a(x,y-1) a(x,y) a(x,y+1) a(x+1,y-
1) a(x+1,y) a(x+1,y+1)];

a2=sort(a1);
med=a2(5);
b(x,y)=med;

end
end
figure;
subplot(1,3,1); imshow(img); title('Original Image');
subplot(1,3,2); imshow(uint8(ab)); title('speckle noise');
subplot(1,3,3); imshow(uint8(b)); title('Median filtering');

Goswami Pratikgiri R.(120420704006) Page 9

Das könnte Ihnen auch gefallen