【图像去噪】基于matlab即插即用法图像去噪【含Matlab源码 152期】

455 阅读2分钟

一、简介

理论知识参考文献:基于Retinex和ADMM优化的水下光照不均匀图像增强算法

二、部分源代码

function out = PlugPlayADMM_deblur(y,h,lambda,method,opts)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%out = PlugPlayADMM_deblur(y,h,lambda,method,opts)
%deblurs image y by solving the ADMM:
%
%inversion step: x=argmin_x(||Ax-y||^2+rho/2||x-(v-u)||^2)
%denoising step: v=Denoise(x+u)
%      update u: u=u+(x-v)
%
%Input:           y    -  the observed gray scale image
%                 h    -  blur kernel
%              lambda  -  regularization parameter
%              method  -  denoiser, e.g., 'BM3D'
%       opts.rho       -  internal parameter of ADMM {1}
%       opts.gamma     -  parameter for updating rho {1}
%       opts.maxitr    -  maximum number of iterations for ADMM {20}
%       opts.tol       -  tolerance level for residual {1e-4}   
%       ** default values of opts are given in {}. 
%
%Output:          out  -  recovered gray scale image 
%
%Xiran Wang and Stanley Chan
%Copyright 2016
%Purdue University, West Lafayette, In, USA.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check inputs
if nargin<4
    error('not enough input, try again \n');
elseif nargin==4
    opts = [];
end

% Check defaults
if ~isfield(opts,'rho')
    opts.rho = 1;
end
if ~isfield(opts,'max_itr')
    opts.max_itr = 20;
end
if ~isfield(opts,'tol')
    opts.tol = 1e-4;
end
if ~isfield(opts,'gamma')
    opts.gamma=1;
end
if ~isfield(opts,'print')
    opts.print = false;
end


% set parameters
max_itr   = opts.max_itr;
tol       = opts.tol;
gamma     = opts.gamma;
rho       = opts.rho;

%initialize variables
dim         = size(y);          
N           = dim(1)*dim(2);    
Hty         = imfilter(y,h,'circular'); 
eigHtH      = abs(fftn(h, dim)).^2;

residual    = inf;

%set function handle for denoiser
switch method
    case 'BM3D'
        denoise=@wrapper_BM3D;
    case 'TV'
        denoise=@wrapper_TV;
    case 'NLM'
        denoise=@wrapper_NLM;
    case 'RF'
        denoise=@wrapper_RF;
    otherwise
        error('unknown denoiser \n');
end

% main loop

if opts.print==true
    fprintf('Plug-and-Play ADMM --- Deblurring \n');
    fprintf('Denoiser = %s \n\n', method);
    fprintf('itr \t ||x-xold|| \t ||v-vold|| \t ||u-uold|| \n');
end

itr = 1;
while(residual>tol&&itr<=max_itr)
    %store x, v, u from previous iteration for psnr residual calculation
    x_old = x;
    v_old = v;
    u_old = u;
    
    %inversion step
    xtilde = v-u;
    rhs    = fftn(Hty+rho*xtilde,dim);
    x      = real(ifftn(rhs./(eigHtH+rho),dim));
    
    %denoising step
    vtilde = x+u;
    vtilde = proj(vtilde);
    sigma  = sqrt(lambda/rho);
    v      = denoise(vtilde,sigma);
    
    %update langrangian multiplier
    u      = u + (x-v);
    
    %update rho
    rho = rho*gamma;
    
    %calculate residual
    residualx = (1/sqrt(N))*(sqrt(sum(sum((x-x_old).^2))));
    residualv = (1/sqrt(N))*(sqrt(sum(sum((v-v_old).^2))));
    residualu = (1/sqrt(N))*(sqrt(sum(sum((u-u_old).^2))));
    
    residual = residualx + residualv + residualu;

    if opts.print==true
        fprintf('%3g \t %3.5e \t %3.5e \t %3.5e \n', itr, residualx, residualv, residualu);
    end
    
    itr = itr+1;
end
out = v;
end
function y = afun(x,transp_flag,h,dim)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example of the A matrix
% 
% This example illustrates how to construct the A matrix
% for deblurring problem. The function executes the operations of
% A*x and A'*x
%
% Stanley Chan
% Purdue University
% Nov 24, 2016
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rows = dim(1);
cols = dim(2);
if strcmp(transp_flag,'transp')         % y = A'*x
    x = reshape(x,[rows,cols]);
    y = imfilter(x,rot90(h,2),'circular');
    y = y(:);
elseif strcmp(transp_flag,'notransp')   % y = A*x
    x = reshape(x,[rows,cols]);
    y = imfilter(x,h,'circular');
    y = y(:);
end
end
function out = wrapper_NLM(in,sigma)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% out = wrapper_NLM(in,sigma)
% performs non-local means denoising
% 
% Require NLM package
%
% Download:
% http://www.ipol.im/pub/art/2011/bcm_nlm/
%
% Xiran Wang and Stanley Chan
% Copyright 2016
% Purdue University, West Lafayette, In, USA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Options.filterstrength=sigma;
out = NLMF(in,Options);
end

三、运行结果

在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本 2014a

2 参考文献 [1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020. [2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013. [3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013. [4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015. [5]刘卫东,李吉玉,张文博,李乐.基于Retinex和ADMM优化的水下光照不均匀图像增强算法[J].西北工业大学学报. 2021,39(04)