function [Result, cost, SNR]= denoising(input, lambda, max_Iter, label, Ori_Img) cost = []; SNR = []; Img_ori = im2double(input); [height,width,ch] = size(input);1 denom_tmp = (abs(psf2otf([1, -1],[height,width])).^2 + abs(psf2otf([1; -1],[height,width])).^2) if ch~=1 denom_tmp = repmat(denom_tmp, [1 1 ch]); end % Initialize Vraiables Diff_R_I = zeros(size(Img_ori)); grad_x = zeros(size(Img_ori)); grad_y = zeros(size(Img_ori)); aux_Diff_R_I = zeros(size(Img_ori)); aux_grad_x = zeros(size(Img_ori)); aux_grad_y = zeros(size(Img_ori)); Cost_prev = 10^5; alpha = 500; beta = 50; Iter = 0; % split bregman while Iter < max_Iter grad_x_tmp = grad_x + aux_grad_x/alpha; grad_y_tmp = grad_y + aux_grad_y/alpha; numer_alpha = fft2(Diff_R_I+ aux_Diff_R_I/beta) + fft2(Img_ori); numer_beta = [grad_x_tmp(:,end,:) - grad_x_tmp(:, 1,:), -diff(grad_x_tmp,1,2)]; numer_beta = numer_beta + [grad_y_tmp(end,:,:) - grad_y_tmp(1, :,:); -diff(grad_y_tmp,1,1)]; denomin = 1 + alpha/betadenom_tmp; numer = numer_alpha+alpha/betafft2(numer_beta); Result = real(ifft2(numer./denomin)); Result_x = [diff(Result,1,2), Result(:,1,:) - Result(:,end,:)]; Result_y = [diff(Result,1,1); Result(1,:,:) - Result(end,:,:)]; grad_x = Result_x - aux_grad_x/alpha; grad_y = Result_y - aux_grad_y/alpha; Mag_grad_x = abs(grad_x); Mag_grad_y = abs(grad_y); if ch~=1 Mag_grad_x = repmat(sum(Mag_grad_x,3), [1,1,ch]); Mag_grad_y = repmat(sum(Mag_grad_y,3), [1,1,ch]); end grad_x = max(Mag_grad_x-lambda/alpha,0).(grad_x./Mag_grad_x); grad_y = max(Mag_grad_y-lambda/alpha,0).(grad_y./Mag_grad_y); grad_x(Mag_grad_x == 0) = 0; grad_y(Mag_grad_y == 0) = 0; Diff_R_I = Result-Img_ori-aux_Diff_R_I/beta; Mag_Diff_R_I = abs(Diff_R_I); if ch~=1 Mag_Diff_R_I = repmat(sum(Mag_Diff_R_I,3), [1,1,ch]); end if label == 1 Diff_R_I=max(Mag_Diff_R_I-1/beta,0).(Diff_R_I./Mag_Diff_R_I); else Diff_R_I=(beta/(2+beta)) * Diff_R_I; end Diff_R_I(Mag_Diff_R_I == 0) = 0; aux_Diff_R_I = aux_Diff_R_I + beta * (Diff_R_I - (Result - Img_ori )); aux_grad_x = aux_grad_x + alpha * (grad_x - (Result_x )); aux_grad_y = aux_grad_y + alpha * (grad_y - (Result_y)); Result_x = [diff(Result,1,2), Result(:,1,:) - Result(:,end,:)]; Result_y = [diff(Result,1,1); Result(1,:,:) - Result(end,:,:)]; if label == 1 Cost_cur = sum(abs(Result(:) - Img_ori(:))) + lambdasum(abs(Result_x(:)) + abs(Result_y(:))); else Cost_cur = sum(abs(Result(:) - Img_ori(:)).^2) + lambda*sum(abs(Result_x(:)) + abs(Result_y(:))); end Diff = abs(Cost_cur - Cost_prev); Cost_prev = Cost_cur; cost = [cost Cost_cur]; SNR_tmp = sqrt( sum( (Result(:)-double(Ori_Img(:))).^2 )) / sqrt(numel(Result)); SNR = [SNR SNR_tmp]; Iter = Iter + 1; end end
时间: 2023-08-31 09:06:23 浏览: 115
yuzhi.rar_SNR denoising_硬阈值_软阈值去噪_阈值_阈值算法
5星 · 资源好评率100%
这段代码实现了一种图像去噪算法,使用了Split Bregman方法。具体来说,该算法通过最小化一个带有$L_1$正则项的能量函数来去除噪声。其中,$L_1$正则项用于促使平滑图像的梯度尽可能小,从而去除噪声。
该算法的主要思想是将图像分解为梯度和残差两个部分,并分别对其进行处理。具体来说,算法首先通过FFT计算分母,并计算分子的两部分(其中一部分使用了差分算子),然后通过IFFT计算出分子除以分母的结果。接着,算法对计算得到的梯度进行$L_1$正则化,并更新残差。最后,算法计算当前的损失函数值和信噪比,并继续迭代直到达到最大迭代次数。
需要注意的是,该算法还可以根据label参数的不同取值,分别使用$L_2$正则项或$L_1$正则项来计算损失函数。同时,该算法还使用了一个特殊的变量$\beta$来平衡残差和梯度的影响,以实现更好的去噪效果。
阅读全文