Sie sind auf Seite 1von 4

Code for static median filter:

%==========================================================================
function[A] = imgMaskMed(src_matrix,dim_matrix)
% imgMask Applies a variable sized median filter to an image
% src_matrix is the original image to copy and modify afterwards
% dim_matrix is the dimension of the mask size
% Copy original image data to a temporary buffer
% and flatten to a 2D grayscale matrix
copy_matrix = src_matrix;
% Determine the dimensions of the source matrix
[x,y] = size(copy_matrix);
% Determine the dimensions to use for the median filter
a = dim_matrix;
b = dim_matrix;
% Error checking code
% Non-square mask matrix
if(a~=b)
disp(sprintf('Mask matrix is not square!'))
elsif((a==0) | (b==0))
disp(sprintf('Mask matrix has a singleton dimension!'))
elsif((a<3) | (b<3))
disp(sprintf('Mask matrix is not at least 3x3!'))
elsif((a>=(x-1)) | (b>=(y-1)))
disp(sprintf('Mask matrix dimenions are too large!'))
else
% Pad the matrix edges so we don't lose data
for j = 1:b
[a_temp, b_temp] = size(copy_matrix);
copy_matrix = vertcat(copy_matrix, copy_matrix(a_temp,:));
copy_matrix = vertcat(copy_matrix(1,:), copy_matrix);
copy_matrix = horzcat(copy_matrix, copy_matrix(:,b_temp));
copy_matrix = horzcat(copy_matrix(:,1), copy_matrix);
end
% Re-read the new (padded) image size
[x,y] = size(copy_matrix);
% Generate a vector containing all elements of the mask matrix
median_matrix = [];
for k1=1+ceil(b/2):y-ceil(b/2)
for k2=1+ceil(a/2):x-ceil(a/2)
for k3=1:b
for k4 = 1:a
median_matrix = horzcat(median_matrix,copy_matrix(k2-floor(b/2)+k4,k1-floor(a/2)+k3));
end end
copy_matrix(k2,k1) = median(median_matrix);
median_matrix = [];
end
end
% Trim the matrix edges so input resolution = output resolution
for j = 1:b
[a_temp, b_temp] = size(copy_matrix);
copy_matrix(a_temp,:) = [];
copy_matrix(:,b_temp) = [];
copy_matrix(1,:) = [];
copy_matrix(:,1) = [];
end end
%==========================================================================
%======= Complete
%==========================================================================
A = copy_matrix;
---------------------------------------------------------------------------------------------------------------------------
Code for adaptive median filter:
%==========================================================================
function[A] = imgMaskMedAdaptive(src_matrix,dim_matrix)
% imgMaskMedAdatptive - Applies an adaptive median filter to a noisy
% source image
% src_matrix - the original image to copy and modify afterwards
% dim_matrix - is the dimension of the mask size
% (C) 2010 Matthew Giassa, <teo@giassa.net> www.giassa.net
%==========================================================================
% Copy original image data to a temporary buffer
% and flatten to a 2D grayscale matrix
copy_matrix = src_matrix;
%Definitions
SALT_VALUE = intmax('uint8');
PEPPER_VALUE = intmin('uint8');
% Determine the dimensions of the source matrix
[x,y] = size(copy_matrix);
% Determine the dimensions to use for the median filter
a = dim_matrix;
b = dim_matrix;
% Error checking code
% Non-square mask matrix
if(a~=b)
disp(sprintf('Mask matrix is not square!'))
elsif((a==0) | (b==0))
disp(sprintf('Mask matrix has a singleton dimension!'))
elsif((a<3) | (b<3))
disp(sprintf('Mask matrix is not at least 3x3!'))
elsif((a>=(x-1)) | (b>=(y-1)))
disp(sprintf('Mask matrix dimenions are too large!'))
else
% Pad the matrix edges so we don't lose data
for j = 1:b
[a_temp, b_temp] = size(copy_matrix);
copy_matrix = vertcat(copy_matrix, copy_matrix(a_temp,:));
copy_matrix = vertcat(copy_matrix(1,:), copy_matrix);
copy_matrix = horzcat(copy_matrix, copy_matrix(:,b_temp));
copy_matrix = horzcat(copy_matrix(:,1), copy_matrix);
end
% Re-read the new (padded) image size
[x,y] = size(copy_matrix);
% Generate a vector containing all elements of the mask matrix
median_matrix = [];
for k1=1+ceil(b/2):y-ceil(b/2)
for k2=1+ceil(a/2):x-ceil(a/2)
for k3=1:b
for k4 = 1:a
median_matrix = horzcat(median_matrix,copy_matrix(k2-floor(b/2)+k4,k1-floor(a/2)+k3));
end
end
centrePixel = copy_matrix(k2,k1);
average = mean(median_matrix);
stddev = sqrt(var(median_matrix));
%Two major conditions:
%1-Is the pixel an extreme statistical outlier, ie: more than 3
%times the standard deviation from the mean?
%2-Is the pixel a salt/pepper pixel?
if((abs(centrePixel-average)<3.0*stddev) && ((centrePixel == SALT_VALUE) || (centrePixel ==
PEPPER_VALUE)))
result = median(median_matrix);
else
result = centrePixel;
end
copy_matrix(k2,k1) = result;
median_matrix = [];
end
end
% Trim the matrix edges so input resolution = output resolution
for j = 1:b
[a_temp, b_temp] = size(copy_matrix);
copy_matrix(a_temp,:) = [];
copy_matrix(:,b_temp) = [];
copy_matrix(1,:) = [];
copy_matrix(:,1) = [];
end end
%==========================================================================
%======= Complete
%==========================================================================
A = copy_matrix;
-------------------------------------------------------------------------------------------------------------------------
MATLAB CODE:
%READ AN 2D IMAGE
A=imread('zebra.jpg');
title('IMAGE WITH SALT AND PEPPER NOISE');
figure,imshow(A);
%PAD THE MATRIX WITH ZEROS ON ALL SIDES
modifyA=zeros(size(A)+2);
B=zeros(size(A));
%COPY THE ORIGINAL IMAGE MATRIX TO THE PADDED MATRIX
for x=1:size(A,1)
for y=1:size(A,2)
modifyA(x+1,y+1)=A(x,y);
end
end
%LET THE WINDOW BE AN ARRAY
%STORE THE 3-by-3 NEIGHBOUR VALUES IN THE ARRAY
%SORT AND FIND THE MIDDLE ELEMENT
for i= 1:size(modifyA,1)-2
for j=1:size(modifyA,2)-2
window=zeros(9,1);
inc=1;
for x=1:3
for y=1:3
window(inc)=modifyA(i+x-1,j+y-1);
inc=inc+1;
end
end
med=sort(window);
%PLACE THE MEDIAN ELEMENT IN THE OUTPUT MATRIX
B(i,j)=med(5);
end
end
%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
B=uint8(B);
title('IMAGE AFTER MEDIAN FILTERING');
figure,imshow(B);

Das könnte Ihnen auch gefallen