本文共 2662 字,大约阅读时间需要 8 分钟。
1 用途
图像降噪(图像修复,复原中),TVloss是一种较为有效的正则项,来保持图像的光滑性。 2 效果%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%I=imread('toys.bmp'); % load imageI=double(I(20:120,10:105)); % cut a piece, convert to double%%% Parametersstd_n=10; var_n=std_n^2; % Gaussian noise standard deviationreduced_pw = 1.5*var_n; % power to reduce in first phasesig_w = 5; ws=4*sig_w+1; % window size%%%%%%%%%%%%%%%%% Add noiseIn = randn(size(I))*std_n;I0 = I + In; % noisy input image% show original and noisy imagesfigure(1); imshow(uint8(I)); title('Original')figure(2); imshow(uint8(I0)); title('Noisy image')snr_noisy=db(I,I0)% run normal tv - strong denoisingtic;J=I0; ep_J=0.1; % minimum mean change in image JJ_old=0;lam=0; iter=10; dt=0.2; ep=1; while (mean(mean(abs(J - J_old))) > ep_J), % iterate until convergence J_old=J; J=tv(J,iter,dt,ep,lam,I0); % scalar lam lam = calc_lam(J,I0,reduced_pw); % update lambda (fidelity term)end % while% figure(3); imshow(uint8(J)); title('residue TV')% snr_residue= db(I,J)Ir=I0-J; % Ir scalarPr = mean(mean(Ir.^2)); % power of residueLV = loc_var(Ir,ws,sig_w^2); % local variance (local power of the residue )% P=mean(mean(LV));Pxy=1*(var_n^2)./LV; %% Sxy inverse proportional to the LV%%% Varying Lambdalamxy=zeros(size(I0));J=I0; J_old=0; ep_J=0.001;%eps=0.01;while (mean(mean(abs(J - J_old))) > ep_J), % iterate until convergence J_old=J; J=tv(J,iter,dt,ep,lamxy,I0); % adaptive lam %J=tv(J,iter,dt,ep_J,lamxy,I0); lamxy = calc_lamxy(J,I0,Pxy,sig_w); % update lambda (fidelity term)end % whilefigure(3); imshow(uint8(J)); title('Adaptive TV')snr_adap= db(I,J)toct1=toc%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Run scalar TV denoising for comparision %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ticJ=I0; % paramsep_J = 0.01; % minimum mean change in image Jlam=0; J_old=0;i=0;while (mean(mean(abs(J - J_old))) > ep_J), % iterate until convergence J_old = J; J=tv(J,iter,dt,ep,lam,I0); % scalar lam lam = calc_lam(J,I0,var_n,ep); % update lambda (fidelity term) end % for i% Ir=I0-J; % Ir scalar% Pr = mean(mean(Ir.^2)); % power of residuefunction Ig=gauss(I,ks,sigma2)%private function: gauss (by Guy Gilboa):% Ig=gauss(I,ks,sigma2)% ks - kernel size (odd number)% sigma2 - variance of Gaussian[Ny,Nx]=size(I);hks=(ks-1)/2; % half kernel sizeif (Ny1) xL=mean(I(:,1:hks)')'; xR=mean(I(:,Nx-hks+1:Nx)')';% xL,xR :Ny x 1 else xL=I(:,1); xR=I(:,Nx); end eI=[xL*ones(1,hks) I xR*ones(1,hks)]; % Ny x Nx+2hks if (hks>1) xU=mean(eI(1:hks,:)); xD=mean(eI(Ny-hks+1:Ny,:)); % xU,xD: 1 x Nx+2hks else xU=eI(1,:); xD=eI(Ny,:); end
完整代码或者代写添加QQ1575304183
转载地址:http://jigx.baihongyu.com/