You are on page 1of 3

a=imread('coins.

png');
subplot(3,3,1);
imshow(a);
title('Original Image');
b=imnoise(a ,'Salt & Pepper',0.4);
subplot(3,3,2);
imshow(b);
title('Salt & Pepper Noise');
h1=1/9*ones(3,3);
h2=1/25*ones(5,5);
c1=conv2(b,h1,'same');
c2=conv2(b,h2,'same');
subplot(3,3,4);
imshow(uint8(c1));
title('3x3 Median Filter');
subplot(3,3,5);
imshow(uint8(c2));
title('5x5 adap Median Filter');
c3=medfilt2(b,[3 3]);
subplot(3,3,6);
imshow(c3);
title('3x3 MFI');
c4=medfilt2(b,[5 5]);
subplot(3,3,7);
imshow(c4);
title('adaptive');
c5=wiener2(b,[3 3]);
subplot(3,3,3);
imshow(c5);
title('3x3 median filter');
c6=imfilter(b,h2);
subplot(3,3,8);
imshow(c6);
title('5x5 Filter');
max2_A
max2_B
min2_A
min2_B

=
=
=
=

max(max(b));
max(max(c5));
min(min(b));
min(min(c5));

if max2_A > 1 || max2_B > 1 || min2_A < 0 || min2_B < 0


disp('input matrices must have values in the interval [0,1]')
end
error_diff = max2_A - max2_B;
decibels = 100*(1/(sqrt(mean(mean(error_diff^2)))));
disp(sprintf('PSNR adaptive median = %5.2f ',decibels));
max2_n
max2_h
min2_n
min2_h

=
=
=
=

max(max(b));
max(max(c6));
min(min(b));
min(min(c6));

if max2_n > 1 || max2_h > 1 || min2_n < 0 || min2_h < 0


disp('input matrices must have values in the interval [0,1]')
end
error_diff2 = max2_n - max2_h;
decibels2 = 100*(1/(sqrt(mean(mean(error_diff2^2)))));

iii

disp(sprintf('PSNR median= %5.2f ',decibels2));


max2_d
max2_e
min2_d
min2_e

=
=
=
=

max(max(b));
max(max(c2));
min(min(b));
min(min(c2));

if max2_d > 1 || max2_e > 1 || min2_d < 0 || min2_e < 0


disp('input matrices must have values in the interval [0,1]')
end
error_diff1 = max2_d - max2_e;
decibels1 = 100*(1/(sqrt(mean(mean(error_diff1^2)))));
disp(sprintf('PSNR normal filter= %5.2f ',decibels1))

x= imread('coins.png');
%% Demo
disp('Noise density lies between 0 and 1');
%% Noise Generation
disp(' ');
ND = input('Enter Noise Density [0.5] : ');
if isempty(ND)
ND = 0.5;
end
y = imnoise(x,'salt & pepper',ND);
y = double(y);
Y = y;
[R C] = size(x);
%% Implementation
for i = 2:R-1
for j = 2:C-1
clear tmp;
tmp = Y(i-1:i+1,j-1:j+1);
flg = 0;
S = sort(tmp(:));
if Y(i,j) == 0 || Y(i,j) == 255
flg = 1;
end
if flg == 0 % if the Pixel is uncorrupted.
if S(1) < Y(i,j) && Y(i,j) < S(5) && 0 < S(1) && S(9) < 255 %
The
% P(X,Y) is an uncorrupted pixel if Pmin < P(X,Y) < Pmax,
% Pmin > 0 and Pmax < 255; the pixel being processed is
% left unchanged. Otherwise, P(X,Y) is a corrupted pixel.
Y(i,j) = Y(i,j);
end
else % if the Pixel is corrupted.
if S(1) < S(5) && S(5) < S(9) && 0 < S(5) && S(5) < 255 % If
P(X,Y)
% is a corrupted pixel, it is replaced by its median value
% if Pmin < Pmed < Pmax and 0 < Pmed < 255.
Y(i,j) = S(5);

iv

end
if S(1) >= S(5) || S(5) >= S(9) || S(5) == 255 && S(5) == 0
% If Pmin < Pmed < Pmax is not satisfied or 255 < Pmed =

0,

% then is a noisy pixel. In this case, the P(X,Y) is

replaced

% by the value of neighborhood pixel value.


Y(i,j) = Y(i,j-1);
end

end

end
end
%% Border Correction
Y(1,:) = Y(2,:);
Y(R,:) = Y(R-1,:);
Y(:,1) = Y(:,2);
Y(:,C) = Y(:,C-1);
f = medfilt2(y,[3 3]);
figure;subplot(2,2,1);imshow(x,[]);title('Given Image');
subplot(2,2,2);imshow(y,[]);title(strcat('Noisy : Noise Density ',num2str(ND)));xlabel('PSNR(x,y)');
subplot(2,2,3);imshow(f,[]);title('Traditional
Median');xlabel('PSNR(x,f)');
subplot(2,2,4);imshow(Y,[]);title('DBA');xlabel('PSNR(x,Y)');
%% PSNR
% function P = PSNR(A,B)
% A = double(A);B = double(B);
% error = abs(A - B);
% P = 20*log10(255/(sqrt(mean(mean(error.^2)))));
%
% )

You might also like