www.pudn.com > ramf.rar > RAMF.m, change:2015-05-31,size:1497b


clear all; 
close all; 
clc; 
 
img=mat2gray(imread('lena.jpg')); 
[m n]=size(img); 
 
img=imnoise(img,'salt & pepper',0.1);   %加入椒盐噪声 
imshow(img,[]); 
 
Nmax=10;        %确定最大的滤波半径 
 
%下面是边界扩展,图像上下左右各增加Nmax像素。 
imgn=zeros(m+2*Nmax+1,n+2*Nmax+1); 
imgn(Nmax+1:m+Nmax,Nmax+1:n+Nmax)=img; 
 
imgn(1:Nmax,Nmax+1:n+Nmax)=img(1:Nmax,1:n);                 %扩展上边界 
imgn(1:m+Nmax,n+Nmax+1:n+2*Nmax+1)=imgn(1:m+Nmax,n:n+Nmax);    %扩展右边界 
imgn(m+Nmax+1:m+2*Nmax+1,Nmax+1:n+2*Nmax+1)=imgn(m:m+Nmax,Nmax+1:n+2*Nmax+1);    %扩展下边界 
imgn(1:m+2*Nmax+1,1:Nmax)=imgn(1:m+2*Nmax+1,Nmax+1:2*Nmax);       %扩展左边界 
 
re=imgn; 
for i=Nmax+1:m+Nmax 
    for j=Nmax+1:n+Nmax 
         
        r=1;                %初始滤波半径 
        while r~=Nmax 
            W=imgn(i-r:i+r,j-r:j+r); 
            W=sort(W); 
            Imin=min(W(:)); 
            Imax=max(W(:)); 
            Imed=W(uint8((2*r+1)^2/2)); 
            if Imin<Imed && Imed<Imax       %如果当前邻域中值不是噪声点,那么就用此次的邻域 
               break; 
            else 
                r=r+1;              %否则扩大窗口,继续判断 
            end           
        end 
         
        if Imin<imgn(i,j) && imgn(i,j)<Imax         %如果当前这个像素不是噪声,原值输出 
            re(i,j)=imgn(i,j); 
        else                                        %否则输出邻域中值 
            re(i,j)=Imed; 
        end 
         
    end 
end 
 
figure; 
imshow(re(Nmax+1:m+Nmax,Nmax+1:n+Nmax),[]);