denoise.m

来自「You need to download the general purpose」· M 代码 · 共 54 行

M
54
字号
function y = denoise(M,k,weight_fun)

% denoise - denoise an image.
%
%   y = denoise(M,k,weight_fun);
%
%   'k' is the size of the neighborhood (ie the square
%   taken around each pixel is of size (2k+1)x(2k+1)).
%   
%   Copyright (c) 2004 Gabriel Peyr

if nargin<2
    k = 3;
end
if nargin<3
    weight_fun = @weight_lin;
end

n = length(M);
y = zeros(n);



for i = k+1:n-k
    for j = k+1:n-k
        
        % current windows
        M1 = M(i-k:i+k,j-k:j+k);
        % compute distance matrix
        dist = zeros(n,n) + Inf;
        for i1 = k+1:n-k
            for j1 = k+1:n-k
                M2 = M(i1-k:i1+k,j1-k:j1+k);   % difference image
                dist(i1,j1) = norm(M2-M1,'fro');
            end
        end
        dist = dist/(2*k+1)^2;
        w = feval(weight_fun,dist);
        w = w ./ sum(sum(w));
        
%         I = find(w>0);
%         for s = I';
%             [i1,j1] = ind2sub(size(M),s);
%             y(i,j) = y(i,j) + w(i1,j1) * M(i1,j1);
%         end

for i1 = k+1:n-k
    for j1 = k+1:n-k
        y(i,j) = y(i,j) + w(i1,j1) * M(i1,j1);
    end
end
        
    end
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?