Sayfalar

19 Mart 2012 Pazartesi

Median Filter For Salt and Papper Noise

Median Filter For Salt and Papper Noise
Salt is refer to max and papper refer to min. In this reason median filter is clear min-max values from image and replace avarage of neighbord pixels. This neighboring may be matris of 3x3 or larger. We test this neighboring and finding optimal solution for this reason.

 Kodding Problem(For matlab)
This function has tre parameters. The first parameter is image,you may read the image code of  "imread('filepath')".The second parameter is mask you may enter  the code of "ones(3,3)". The last parameter is filter type, you must write "median" for median filter.

function [ rtn] = Conv( image,mask, type )
irl=size(image,1);
icl=size(image,2);
result=zeros(size(image,1),size(image,2));
mr_=size(mask,1)/2;
mc_=size(mask,2)/2;
mr=uint32(mr_);
mc=uint32(mc_);
for i=1:size(image,1)
    for j=1:size(image,2)
        ri=i;
        rs=i;
        ci=j;
        cs=j;
       
          while(ri-1>0&&ri-1>i-mr)
            ri=ri-1;
          end
          while(rs+1<irl&&rs+1<i+mr)
            rs=rs+1;
          end
          while(ci-1>0&&ci-1>j-mc)
            ci=ci-1;
          end
          while(cs+1<icl&&cs+1<j+mc)
            cs=cs+1;
          end
        croppedimage=uint16(image(ri:1:rs,ci:1:cs));
        croppedmask=uint16(mask(mr-(i-ri):1:mr+(rs-i),mc-(j-ci):1:mc+(cs-j)));
        mask_mxn=size(croppedmask,1)*size(croppedmask,2);
        pixel=0;
        switch lower(type)
             case{'median'}
                temp=croppedimage(:);
                temp=sort(temp);
                pixel=temp(uint16((mask_mxn/2)));
            case{'yoldas1'}
                temp=croppedimage(:);
                temp=sort(temp);
                tempMinMax=temp(uint8(mask_mxn/3):1:uint8(mask_mxn*2/3));
                pixel=(max(tempMinMax)+min(tempMinMax))/2;
        end 
        result(i,j)=pixel;
    end
end
if( max(result)>256)
   result=mod(result,256);
end
  rtn=uint8(result);
end

Picture 1 Salt and Papper problem

 Picture 2: Median filtered picture for mask of(3x3)
Picture 3: Median filtered picture for mask of 5x5 

Hiç yorum yok:

Yorum Gönder