Median filter

20 views (last 30 days)
Darshan A
Darshan A on 17 May 2011
I am trying to understand the working of median filter. this is the code %start
A = imread('/*path name*/') ;
A = im2double(A);
[m n] = size(A);
Med = [];
%Modified filter
for i=2:m-1
for j=2:n-1
Med(1) = A(i-1,j-1);
Med(2) =A(i-1,j) ;
Med(3) = A(i-1,j+1);
Med(4) = A(i,j-1);
Med(5) = A(i,j+1);
Med(6) = A(i+1, j-1);
Med(7) = A(i+1,j);
Med(8) = A(i+1,j+1);
A(i,j) = median(Med);
end
end
imshow(A);
but the output is not as expected. it is blurry. need help..
  2 Comments
Ahmed
Ahmed on 28 Mar 2013
I tried this code in my research for 2-D medical images and i get a good results better than the standard function of median in matlab and i can send the result to compare between the standard median and this modified one
thanx alot for this code ahm179@yahoo.com
Changa Hettiarachchi
Changa Hettiarachchi on 6 Mar 2020
Why i get an error in line Med(1)???

Sign in to comment.

Accepted Answer

Arturo Moncada-Torres
Arturo Moncada-Torres on 17 May 2011
To understand the medial filter, maybe it is better if we start in 1D.
Let's suppose the following array:
a = [3 2 1 6 5];
Also, let's suppose you define a window size of n = 3.
You place yourself in the first value of the array, that is 3. Since your window size is 3, you take the sample you are, one sample to the left and one sample to the right. Since this is the first value, there is no sample to the left, so we will just consider a zero:
0 3 2
From this window, you sort the values from minimum to maximum and get the median (the value in the middle)
0 2 3
In this case, the value in the middle is 2. This is the value you write in the filtered vector a_
a_ = [2 2 1 6 5];
Then you go to the next value of a, that is a 2. You take the left sample (which has already been modified) and the right sample
2 2 1
Your sort them and choose the median value (in this case, a 2 again)
1 2 2
So your filtered vector a_ is now
a_ = [2 2 1 6 5];
An so on. In the end, you will have a filtered vector a_
a_ = [2 2 2 5 5];
A median filter in images works the same way, only in 2D. So you take not only the values (pixels) that are left or right, but all the values that surround the sample (pixel) you are in.
In MATLAB, check medfilt1 and medfilt2 ;) .
doc medfilt1;
doc medfilt2;
Anyway, take a look at this useful tutorial http://www.librow.com/articles/article-1 by Chernenko. Even though it is not coded in MATLAB, the explanation is very, very clear.
Keep coding!
  4 Comments
Darshan A
Darshan A on 17 May 2011
Tried it. Still no change in the output.. once this problem is fixed , i can code the rest of the filter.. Please help..!!!
dunklevision
dunklevision on 18 Dec 2016
Edited: dunklevision on 18 Dec 2016
I have an ECG signal which I want to filter with the median filter without using (medfilt1). So I'm supposed to build a function. Can you help me please ?

Sign in to comment.

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 17 May 2011
The way you do your itterative medians lead to not taking the median of the input image for pixel [i,j] except for the very first pixel (i=2, j=2). After that (say i=2, j=3) you have overwritten the original pixel value in some of the neighbouring pixels (for the example pixel you'd have the 3-by-3 median in pixel A(2,2), not the original pixel intensity). You should assign the median filtered intensities to pixels in another image array, say Afiltered(i,j).
HTH, Bjeorn
  4 Comments
Darshan A
Darshan A on 18 May 2011
It is working. but the output is not very clear when compared to medfilt2(). wanted to know why isn't both the outputs same..
Andrei Bobrov
Andrei Bobrov on 18 May 2011
analog function medfil2 <http://www.mathworks.com/matlabcentral/answers/5656-program-to-find-the-median-of-the-matrix-need-to-use-for-loop>

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!