关闭

高斯滤波

标签: MATLAB图像处理
562人阅读 评论(0) 收藏 举报
分类:

1.通过调整高斯函数的标准差(sigma)来控制平滑程度;
2.滤波窗口大小取为[6*sigma-1]/2*2+1[.]表示取整;
3.利用二维高斯函数的行列可分离性进行加速;
•先对每行进行一维高斯滤波,再对结果的每列进行同样的一维高斯滤波;

clear;
tic;
P=imread('H:/数字图像处理/13_input.jpg');
%subplot(131);
imshow(P),title('原图像');
P=imnoise(P,'salt & pepper',0.02);
%subplot(132);b9g
figure;
imshow(P),title('加入高斯噪声后的图片');
P=double(P);
[nrows ncols p]=size(P);
OUT = uint8(zeros(nrows,ncols,p));%输出图像
sigma=str2double(inputdlg('请输入高斯滤波器的标准差', 'INPUT scale factor', 1, {'2.5'}));
c=floor(6*sigma-1)/2*2+1;
k=floor((c+1)/2);%计算图象中心
a=zeros(c);
sum=0;
for i=1:c
    a(i)= exp(-((i-k).^2)/(2*sigma^2))/(sqrt(2*3.1415926*sigma));
    sum=sum+a(i);
end
for m=1:c  %归一化
    a(m)=a(m)/sum;
end

nLen=(c-1)/2;
if p==1 %处理灰度图像
  %x方向滤波
  for y=1:ncols 
      for x=1-nLen:nrows-nLen  
          dDotMul = 0;  
          dWeightSum = 0;  
          for i=-nLen:nLen             
                %判断是否在图像内部  
                if((i+x)>=1 && (i+x)<nrows)                  
                    dDotMul=dDotMul+(P(i+x,y) * a(nLen+i+1));  
                    dWeightSum =dWeightSum+ a(nLen+i+1);  
                end
           end
           OUT(i+x,y) = dDotMul/dWeightSum;  
      end        
  end 
  %y方向滤波
  for x=1:nrows 
      for y=1-nLen:ncols-nLen  
          dDotMul = 0;  
            dWeightSum = 0;  
            for i=-nLen:nLen             
                %判断是否在图像内部  
                if((i+y)>=1 && (i+y)<ncols)                  
                    dDotMul=dDotMul+(OUT(x,i+y) * a(nLen+i+1));  
                    dWeightSum =dWeightSum+ a(nLen+i+1);  
                end
            end
            P(x,i+y) = dDotMul/dWeightSum;  
      end        
  end 
  OUT=P;
end

if p==3  %处理真彩色
    R = P(:,:,1);
    G = P(:,:,2);
    B = P(:,:,3);
    RR = uint8(zeros(nrows,ncols));
    GG = uint8(zeros(nrows,ncols));
    BB = uint8(zeros(nrows,ncols));
   %x方向滤波
   for y=1:ncols 
      for x=1-nLen:nrows-nLen  
            dDotMul_R = 0;
            dDotMul_G = 0;
            dDotMul_B = 0;
            dWeightSum = 0;  
            for i=-nLen:nLen             
                %判断是否在图像内部  
                if((i+x)>=1 && (i+x)<=nrows)                  
                    dDotMul_R=dDotMul_R+(R(i+x,y) * a(nLen+i+1));
                    dDotMul_G=dDotMul_G+(G(i+x,y) * a(nLen+i+1));
                    dDotMul_B=dDotMul_B+(B(i+x,y) * a(nLen+i+1));
                    dWeightSum =dWeightSum+ a(nLen+i+1);  
                end
            end
            RR(i+x,y) = dDotMul_R/dWeightSum;  
            GG(i+x,y) = dDotMul_G/dWeightSum;
            BB(i+x,y) = dDotMul_B/dWeightSum;
      end        
   end  
  %y方向滤波
  for x=1:nrows 
      for y=1-nLen:ncols-nLen  
            dDotMul_R = 0;
            dDotMul_G = 0;
            dDotMul_B = 0;
            dWeightSum = 0;  
            for i=-nLen:nLen             
                %判断是否在图像内部  
                if((i+y)>=1 && (i+y)<=ncols)                  
                    dDotMul_R=dDotMul_R+(RR(x,i+y) * a(nLen+i+1));
                    dDotMul_G=dDotMul_G+(GG(x,i+y) * a(nLen+i+1));
                    dDotMul_B=dDotMul_B+(BB(x,i+y) * a(nLen+i+1));
                    dWeightSum =dWeightSum+ a(nLen+i+1);  
                end
            end
            R(x,i+y) = dDotMul_R/dWeightSum;  
            G(x,i+y) = dDotMul_G/dWeightSum;
            B(x,i+y) = dDotMul_B/dWeightSum;
      end        
  end 
  OUT(:,:,1) = R;
  OUT(:,:,2) = G;
  OUT(:,:,3) = B;
end
%subplot(133);
figure;
imshow(OUT);
title('高斯滤波去噪结果');
toc;



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23402次
    • 积分:459
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:8篇
    • 译文:0篇
    • 评论:20条
    最新评论