Adaptive
filter is performed on the degraded image that contains original image and
noise. The mean and variance are the two statistical measures that a local
adaptive filter depends with a defined mxn window region.

A = imread('saturn.png');

B = rgb2gray(A);

sz = size(B,1)*size(B,2);

%Add gaussian noise with mean 0 and variance 0.005

B = imnoise(B,'gaussian',0,0.005);

figure,imshow(B); title('Image with gaussian noise');

B = double(B);

%Define the window size mxn

M = 5;

N = 5;

%Pad the matrix with zeros on all sides

C = padarray(B,[floor(M/2),floor(N/2)]);

lvar = zeros([size(B,1) size(B,2)]);

lmean = zeros([size(B,1) size(B,2)]);

temp = zeros([size(B,1) size(B,2)]);

NewImg = zeros([size(B,1) size(B,2)]);

for i = 1:size(C,1)-(M-1)

for j = 1:size(C,2)-(N-1)

temp = C(i:i+(M-1),j:j+(N-1));

tmp = temp(:);

%Find the local mean and local variance for the local region

lmean(i,j) = mean(tmp);

lvar(i,j) = mean(tmp.^2)-mean(tmp).^2;

end

end

%Noise variance and average of the local variance

nvar = sum(lvar(:))/sz;

%If noise_variance > local_variance then local_variance=noise_variance

lvar = max(lvar,nvar);

%Final_Image = B- (noise variance/local variance)*(B-local_mean);

NewImg = nvar./lvar;

NewImg = NewImg.*(B-lmean);

NewImg = B-NewImg;

%Convert the image to uint8 format.

NewImg = uint8(NewImg);

figure,imshow(NewImg);title('Restored Image using Adaptive Local filter');

## 10 comments:

Your site has been really useful to me for my project especially these posts but it would be really helpful if i'm cleared up with finding the Local Variance as you have given. I'm getting stuck there!

Pls reply.

Thank you!

This is fake! The alogrithem convolves over the wrong indices and the so-called output image is not the one you get from copying the code 1:1. More likely it is the input image...mathematical background seems to be fine though. Some of the code is usable, but one has to correct it.

@coJetty

The code which I have provided here is for local noise. I have posted whatever result I obtained by executing the code, including the images. Kindly mail me your point of view more clearly. Happy Reading!

super

Why is it necessary to do a 2 boarder zero padding for a 3X3 window? Shouldn't you have done just one layer of zero padding?

For your example above, why is it necessary to do a two layer zero padding if you are using a 3X3 window?

why two layer padding?

@Akankhya Behera

Single layer padding is fine. I have updated the algorithm. Thank you for the comment.

how to convert this matlab code to c++ code

@nebil

Try with MATLAB coder

## Enjoyed Reading? Share Your Views