Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Adaptive filtering-local noise filter

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;

%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');

like button Like "IMAGE PROCESSING" page


achu said... Reply to comment

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!

coJetty said... Reply to comment

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.

Aaron Angel said... Reply to comment

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!

Sri said... Reply to comment


Unknown said... Reply to comment

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?

Benjamin Thornton said... Reply to comment

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

Akankhya Behera said... Reply to comment

why two layer padding?

Aaron Angel said... Reply to comment

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

nebil said... Reply to comment

how to convert this matlab code to c++ code

Aaron Angel said... Reply to comment


Try with MATLAB coder

Mohammad Reza Behboodi said... Reply to comment

I check this code, it was true. Thanks.

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Related Posts Plugin for WordPress, Blogger...
Google ping Hypersmash.com