weighted median filter



Description

Weighted Median Filter in matlab:
It is same as median filter, only difference is the mask is not empty. It will having some weight (or values) and averaged.
The steps to perform weighted median filtering are as follows:
1) Assume a 3x3 weighted mask.
2) Place the mask at the left hand corner.
3) After convolution (multiplication), Arrange the 9 pixels in ascending or descending order.
4) Chose the median from these nine values.
5) Place this median at the centre.
6) Move the mask through out the image.




%% matlab code


%% weighted median filter on image with salt & pepper noise %%

I = imread('manish.jpg');

I = double(I)

Z = imnoise(I,'salt & pepper',0.02); % adding Noise

a = double(Z);

b = a;

W = [1,1,1; 1,4,1; 1,1,1]/12 % the values should taken like that, the total sum of values of filter

% is divided by there sum and value should be equal to 1.

[row col] = size(a);

for x = 2:1:row-1

for y = 2:1:col-1

%% To make a 3x3 weighted mask into a 1x9 mask

a1 = [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)];

a2 = sort(a1);

med = a2(5); % the 5th value is the weighted median

b(x,y) = med

end


end

figure(1); imshow(uint8(Z))

figure(2); imshow(uint8(b))

thumbnail
About The Author

Ut dignissim aliquet nibh tristique hendrerit. Donec ullamcorper nulla quis metus vulputate id placerat augue eleifend. Aenean venenatis consectetur orci, sit amet ultricies magna sagittis vel. Nulla non diam nisi, ut ultrices massa. Pellentesque sed nisl metus. Praesent a mi vel ante molestie venenatis.

2 comments