一、简介

1 NLM滤波原理【图像去噪】基于非局部均值(NLM)滤波图像去噪matlab源码_matlab
2 Pixelwise Implementation【图像去噪】基于非局部均值(NLM)滤波图像去噪matlab源码_matlab_02【图像去噪】基于非局部均值(NLM)滤波图像去噪matlab源码_matlab_03
3 Patchwise Implementation【图像去噪】基于非局部均值(NLM)滤波图像去噪matlab源码_matlab_04

  1. function [output]=NLmeans(input,t,f,h)
     
     %  输入: 待平滑的图像
     %  t: 搜索窗口半径
     %  f: 相似性窗口半径
     %  h: 平滑参数
     %  NLmeans(ima,5,2,sigma); 
     
     % 图像大小
     [m n]=size(input);
      % 输出
     Output=zeros(m,n);
     input2 = padarray(input,[f+t f+t],'symmetric');%边界作对称处理
     
     % 高斯核
     kernel = make_kernel(f);
     kernel = kernel / sum(sum(kernel));
     
     h=h*h;
     
     for i=1:m
        for j=1:n
                     
             i1 = i+ f+t;%原始图像的像素位置 (中心像素)
             j1 = j+ f+t;
                    
             W1= input2(i1-f:i1+f , j1-f:j1+f);%小窗口
             
             wmax=0; 
             average=0;
             sweight=0;
             
             %rmin = max(i1-t,f+1);
             %rmax = min(i1+t,m+f);
             %smin = max(j1-t,f+1);
             %smax = min(j1+t,n+f);
             rmin=i1-t;
             rmax=i1+t;
             smin=j1-t;
             smax=j1+t;
             
             for r=rmin:1:rmax %大窗口
                for s=smin:1:smax
                                                   
                    if(r==i1 && s==j1) 
                        continue; 
                    end;
                                    
                    W2= input2(r-f:r+f , s-f:s+f);    %大搜索窗口中的小相似性窗口     
                    d = sum(sum(kernel.*(W1-W2).*(W1-W2)));
                    w=exp(-d/h); %权重      
                                     
                    if w>wmax                
                        wmax=w;   %求最大权重            
                    end
                    
                    sweight = sweight + w;  %大窗口中的权重和
                    average = average + w*input2(r,s);                                  
                end 
             end
                 
            average = average + wmax*input2(i1,j1);
            sweight = sweight + wmax;
                       
            if sweight > 0
                output(i,j) = average / sweight;
            else
                output(i,j) = input(i,j);
            end                
        end
     end
     function nX = noise(varargin)
    % 图像加入噪声
    % 传入参数依次为:
    % X - 待处理的图像
    % type - 噪声类型
    % variance/density - 高斯噪声的方差/椒盐噪声的密度(optional)
    % M - 控制噪声区域的模板(optional)
     
    % 参数默认值
    variance = 0.01;                %高斯噪声的方差 默认值为0.01
    density = 0.05;                 %椒盐噪声的密度 默认值为0.05
    M = ones(size(varargin{1}));    %控制噪声区域的模板 默认对整个图像加噪声
     
    X = varargin{1};                %待处理的图像
    type = varargin{2};             %噪声类型
     
    % 可选参数
    if nargin>=3
        variance = varargin{3};
        density = varargin{3};
        
        if nargin == 4
            M = varargin{4};
        end
    end
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
    • 28.
    • 29.
    • 30.
    • 31.
    • 32.
    • 33.
    • 34.
    • 35.
    • 36.
    • 37.
    • 38.
    • 39.
    • 40.
    • 41.
    • 42.
    • 43.
    • 44.
    • 45.
    • 46.
    • 47.
    • 48.
    • 49.
    • 50.
    • 51.
    • 52.
    • 53.
    • 54.
    • 55.
    • 56.
    • 57.
    • 58.
    • 59.
    • 60.
    • 61.
    • 62.
    • 63.
    • 64.
    • 65.
    • 66.
    • 67.
    • 68.
    • 69.
    • 70.
    • 71.
    • 72.
    • 73.
    • 74.
    • 75.
    • 76.
    • 77.
    • 78.
    • 79.
    • 80.
    • 81.
    • 82.
    • 83.
    • 84.
    • 85.
    • 86.
    • 87.
    • 88.
    • 89.
    • 90.
    • 91.
    • 92.
    • 93.
    • 94.
    • 95.
    • 96.
    • 97.

    三、运行结果

    【图像去噪】基于非局部均值(NLM)滤波图像去噪matlab源码_matlab_05【图像去噪】基于非局部均值(NLM)滤波图像去噪matlab源码_matlab_06【图像去噪】基于非局部均值(NLM)滤波图像去噪matlab源码_matlab_07