PGPCA、PHPCA、PLPCA去噪代码解析

一、整体Pipeline流程解析

clear all
close all

sigma=10;
randn('seed', 2);
ima = double(imread('data/barbara.png'));
% 添加噪音
ima_nse = ima + sigma * randn(size(ima));

figure('Position',[100 100  800 800]);
plotimage(ima_nse);
title(sprintf('Noisy: \n  PSNR %.2f', psnr(ima, ima_nse)));
set(get(gca,'Title'),'FontSize',12);

%% GLOBAL VERSION: PGPCA.m
%
% 首先依据噪声大小来获取最佳参数:Patch的大小以及阈值因子
[WP factor_thr] = PGPCA_best_params(sigma);
% 最终设定的阈值 = 阈值因子 * 噪声参数sigma (可以理解为设定的阈值是以噪声sigma参数为基础的)
threshold = factor_thr * sigma;
% @(x)类似于Pythonz中的lambda匿名函数
% 此处函数的作用是将用PCA方法作用于含噪图片后的分解结构进行硬阈值处理
% 这里的func_thresholding单纯只是一个函数,还没有被调用
func_thresholding = @(ima_ppca) ...
    hardthresholding(ima_ppca, threshold, sigma);
% matlab使用tic和toc来统计程序运行时间
global_time=tic;
%% Patchization step 
% 从含噪图片中提取大小为WP * WP的方形Patch块状结构
ima_patchs = spatial_patchization(ima_nse, WP);

% 去噪的核心步骤:进行Patch的全局去噪,然后进行重建
%% Global PCA denoising step
[ima_patchs_fil ima_ppca_fil] = ...
    PGPCA_denoising(ima_patchs, func_thresholding);
%% Uniform reprojection step
ima_fil_PGPCA = reprojection_UWA(ima_patchs_fil);
global_time=toc(global_time);

%% Display
% 展示去噪效果,并计算PSNR
figure('Position',[100 100  800 800])
plotimage(ima_fil_PGPCA);
title(sprintf('PGPCA: \n  PSNR %.2f', psnr(ima, ima_fil_PGPCA)));
sprintf('PGPCA:\n PSNR: %.2f dB \n Computing Time: %.2f s.',...
    psnr(ima, ima_fil_PGPCA),global_time)
set(get(gca,'Title'),'FontSize',12);



%% LOCAL VERSION: PLPCA.m
[WP factor_thr hW] = PLPCA_best_params(sigma);
threshold = factor_thr * sigma;
% Shift/Redudancy  parameter for the searching zone.
delta = hW; %< 2*hW+WP;
func_thresholding = @(ima_ppca) ...
    hardthresholding(ima_ppca, threshold, sigma);
local_time=tic;
%% Patchization step 
ima_patchs = spatial_patchization(ima_nse, WP);
%% Local PCA denoising step
ima_patchs_fil = PLPCA_denoising(ima_patchs, func_thresholding, ...
                                 hW, delta);
%% Uniform reprojection step
ima_fil_PLPCA = reprojection_UWA(ima_patchs_fil);
local_time=toc(local_time);
%% Display
figure('Position',[100 100  800 800])
plotimage(ima_fil_PLPCA);
title(sprintf('PLPCA: \n  PSNR %.2f', psnr(ima, ima_fil_PLPCA)));
set(get(gca,'Title'),'FontSize',12);
sprintf('PLPCA:\n PSNR: %.2f dB \n Computing Time: %.2f s.', ...
    psnr(ima, ima_fil_PLPCA),local_time)


%% HIERARCHICAL VERSION:PHPCA.m
%
[WP factor_thr depth nb_axis_kept] = PHPCA_best_params(sigma);
threshold = factor_thr * sigma;
func_thresholding = @(ima_ppca) ...
    hardthresholding(ima_ppca, threshold, sigma);
hierarchical_time=tic;
%% Patchization step 
ima_patchs = spatial_patchization(ima_nse, WP);
%% Hierarchical PCA denoising step
[ima_patchs_fil ima_ppca_fil tree] = ...
    PHPCA_denoising(ima_patchs, func_thresholding, ...
                    depth, nb_axis_kept);
%% Uniform reprojection step
ima_fil_PHPCA = reprojection_UWA(ima_patchs_fil);
hierarchical_time=toc(hierarchical_time);

%% Display
figure('Position',[100 100  800 800])
plotimage(ima_fil_PHPCA);
title(sprintf('PHPCA: \n  PSNR %.2f', psnr(ima, ima_fil_PHPCA)));
set(get(gca,'Title'),'FontSize',12);
sprintf('PHPCA:\n PSNR: %.2f dB \n Computing Time: %.2f s.', ...
    psnr(ima, ima_fil_PHPCA),hierarchical_time)

二、各类子函数代码解析

1)根据噪音参数sigma来获取最佳参数,从这里的代码来看,最佳参数的获取完全是事先预定好的:当sigma=5,10,20时,对应的patch大小都为7, 而当sigma=40时,对应的patch大小直接变成12,这其实也就是在间接说明,越大的噪音需要越大的patch来进行处理;当sigma=5,10时设置的硬阈值因子都是2.5,当sigma=20,40的时候设置的硬阈值因子直接变成2.75,这说明越大的噪音需要越大的硬阈值来进行处理(因子越大 等价于 硬阈值越大) 。

function [hP factor_thr] = PGPCA_best_params(sigma)

%PGPCA_best_params        computes the parameters for PGPCA.m, 			
%  			  optimized on a small data set.
%   INPUT:
%     sigma	  : standard deviation of the noise
%   OUTPUT:
%     hP          : half size of the patch 
%     factor_thr  : factor in front of the variance term for
%		    hardthresholding the coefficients
%
%   [hP factor_thr] = PGPCA_best_params(sigma)
%   Computes the best parameters hP and factor_thr for the using
%   PGPCA_denoising.m  for some values of noise level.  
%
%   Copyright (C) 2011 GP-PCA project
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)
%
%---------------------------------------------------------------------
%
%   This file is part of GP-PCA.
%
%   GP-PCA is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as
%   published by the Free Software Foundation, either version 3 of
%   the License, or (at your option) any later version.
%
%   GP-PCA is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public
%   License along with GP-PCA.  If not, see
%   <http://www.gnu.org/licenses/>.
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)

    switch sigma
      case 5
        hP = 7;
        factor_thr = 2.50;
      case 10
        hP = 7;
        factor_thr = 2.50;
      case 20
        hP = 7;
        factor_thr = 2.75;
      case 40
        hP = 12;
        factor_thr = 2.75;
    end

2)硬阈值处理:其实这里的sigma参数并没有用到,只是将ima_ppca.coefs中绝对值大于给定阈值的部分保留了,其余部分置0

function ima_ppca = hardthresholding(ima_ppca, threshold, sigma)

%hardthresholding        Hard thresholding the PCA coefficients
%   INPUT:
%     ima_ppca    : structure of the noisy PCAs decompositions.
%     threshold   : threshold used for the hardthresholding
%     sigma	  : standard deviation of the noise
%   OUTPUT:
%     ima_ppca    : sturture of of the PCAs after hardthresholding
%
%   ima_ppca = hardthresholding(ima_ppca, threshold, sigma)
%   hard thresholding of the PCA coefficients from the ima_ppca
%   structure.
%
%
%   Copyright (C) 2011 GP-PCA project
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)

%---------------------------------------------------------------------
%
%   This file is part of GP-PCA.
%
%   GP-PCA is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as
%   published by the Free Software Foundation, either version 3 of
%   the License, or (at your option) any later version.
%
%   GP-PCA is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public
%   License along with GP-PCA.  If not, see
%   <http://www.gnu.org/licenses/>.
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)

%   这里的维度转化感觉很奇怪,
%   貌似是因为这里的阈值计算只能使用2维来计算
%   所以得先确保coefs是2维的
%   但是为了原样还原,所以必须得记录之前的维度信息
    coefs = ima_ppca.coefs;
    if size(coefs, 3) > 1 %如果coefs第三个维度的长度大于1,其实也就是在说coefs有三个维度
        isimage = 1;
        [M,N,P] = size(coefs);
        MN = M*N;
        coefs = reshape(coefs, MN, P);
    else
        isimage = 0;
        [MN,P] = size(coefs);
    end

    coefs = coefs .* (threshold < abs(coefs)); %保留coefs中绝对值大于阈值的部分

    if isimage
        ima_ppca.coefs = reshape(coefs, M, N, P);
    else
        ima_ppca.coefs = reshape(coefs, MN, P);
    end

3)**空间PATCH块提取:这里无需进行任何形式的padding,只需要按照从左到右、从上到下的顺序滑动来获取每一个patch, 从左到右的每个patch是紧紧相互邻接的,也可以说stride=1,;从上到下的每个patch也是同样。因为没有设置padding,所以从原始图片中提取的总的patch数目是不等于原始图像的总像素数。这里返回了两个数据,一个是提取到的每一个patch数据, 即ima_patchs(size为【每列对应的patch数,每行对应的patch数,一个patch对应的像素总数WP * WP】), 另一个是原始图像中的每个像素点出现在patch中的次数,也就是每个像素点对应多少个Patch).

function [ima_patchs, ima_normalization] = spatial_patchization(im_ini,WP)

%spatial_patchization computes the collection of patches extracted from an image  
%                 
%   INPUT:
%     im_ini             : image of interest
%     WP                  : width of the square patches to consider
%  			         
%   OUTPUT:
%     ima_patchs         : the collection of all the patches
%     ima_normalization  : number of patches to which each pixel belongs
%   
%  [ima_patchs, ima_normalization] = spatial_patchization(im_ini,WP)
%   computes the whole collecitons of  the patches of size WP x WP 
%   of an image im_ini. It also provides the number of patches to 
%   which each pixel belongs in the image.
%
%
%   Copyright (C) 2011 GP-PCA project
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)

%---------------------------------------------------------------------
%
%   This file is part of GP-PCA.
%
%   GP-PCA is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as
%   published by the Free Software Foundation, either version 3 of
%   the License, or (at your option) any later version.
%
%   GP-PCA is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public
%   License along with GP-PCA.  If not, see
%   <http://www.gnu.org/licenses/>.
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)


[m,n] = size(im_ini);
ima_patchs=zeros(m-WP+1,n-WP+1,WP^2);
ima_normalization=zeros(m,n);


for i=1:(m-WP+1)
    for j=1:(n-WP+1)
        xrange = mod((i:i+WP-1)-1,m)+1; % (m:n)包括m到n的所有整数,m和n都是闭区间
        yrange = mod((j:j+WP-1)-1,n)+1; 
        B = im_ini(xrange, yrange);
        ima_patchs(i,j,:) = B(:);
        ima_normalization(xrange, yrange) = ...
            ima_normalization(xrange, yrange) + ones(WP,WP);
    end
end

4)PGPCA去噪核心部分:主要是做了三件事情,首先是对从图像上提取的所有patch进行PCA分解,得到PCA分解的结果,对应于 ima_ppca = ppca(ima_patchs)部分【1】;然后是对PCA分解的结果进行一个硬阈值来过滤掉噪音,对应于 ima_ppca_fil = func_thresholding(ima_ppca)部分【2】,返回的是一个去噪后的PCA分解结果;最后是对这个去噪后的PCA分解结果进行重建,重建得到的不是完整的这个图像,而是去噪图像理论上所有的patch块, 对应于代码ima_patchs_fil = reconstruction_ppca(ima_ppca_fil)【3】部分。在完成这些之后,在主程序会调用reprojection_UWA【4】部分从所有的patch块来还原出去噪后的完整图像,这个过程也就是从所有的patch块中来计算得到完整图像中每个像素点的值。

function [ima_patchs_fil ima_ppca_fil] = ...
        PGPCA_denoising(ima_patchs, func_thresholding)
%PGPCA_denoising  computes the denoised patches, denoised image 
%                 and tree structure using PLPCA (local PCAs)
%   
%   INPUT:
%     ima_patchs 	 : a collection of (noisy) patches
%     func_thresholding  : function that used to threshold the coefficients			
%   OUTPUT:
%     ima_patchs_fil     : structure of of the PCAs after func_thresholding
%                          reprojected in the space of patches
%     ima_ppca_fil       : structure of of the PCAs after func_thresholding
%                          but in the PCA domain
%
%   [ima_patchs_fil ima_ppca_fil] = ...
%        PGPCA_denoising(ima_patchs, func_thresholding)
%   
%
%

%   Copyright (C) 2011 GP-PCA project
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)

%---------------------------------------------------------------------
%
%   This file is part of GP-PCA.
%
%   GP-PCA is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as
%   published by the Free Software Foundation, either version 3 of
%   the License, or (at your option) any later version.
%
%   GP-PCA is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public
%   License along with GP-PCA.  If not, see
%   <http://www.gnu.org/licenses/>.
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)

    ima_ppca = ppca(ima_patchs);
    ima_ppca_fil = func_thresholding(ima_ppca); % 还是结构体,只不过进行了噪音过滤
    ima_patchs_fil = reconstruction_ppca(ima_ppca_fil); %(506,506,49)

[1] 重点是将每个patch对应的49个像素点张成一维的向量,然后再进行PCA分解:

function patchs_ppca = ppca(patchs)

%ppca  computes the PCA of a collection of patches 
%   
%   INPUT:
%     patchs       : a collection of (noisy) patches
%   OUTPUT:
%     patchs_ppca  : structure of of the PCAs (coefficients +components)
%
%
%patchs_ppca = ppca(patchs) computes the pca structures in patch_ppca of the
% collection of patches from patchs. 
%
%   Copyright (C) 2011 GP-PCA project
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)
%
%---------------------------------------------------------------------
%
%   This file is part of GP-PCA.
%
%   GP-PCA is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as
%   published by the Free Software Foundation, either version 3 of
%   the License, or (at your option) any later version.
%
%   GP-PCA is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public
%   License along with GP-PCA.  If not, see
%   <http://www.gnu.org/licenses/>.
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)


    if size(patchs, 3) > 1
        isimage = 1;
        [M,N,P] = size(patchs);
        MN = M*N;
        patchs = reshape(patchs, MN, P);
        
    else
        isimage = 0;
        [MN,P] = size(patchs);
    end

	% 计算平均值,patchs 256036 * 49
	% cst_patchs 256036 * 1
    cst_patchs = mean(patchs,2);
    % pc_axis是pca的主成分或者说新坐标轴,(49, 49)
    % patchs_newbasis是新坐标轴上X的新坐标系数,(256036, 49)
    % varaxis是每个轴上的方差或者说特征值 (49,1)
    % patchs - cst_patchs * ones(1, P)就是对每一行的元素减去该行元素的均值
    % 而前面已经知道每一行元素实际对应于一个patch,所以这里实际上是对每个patch内的元素去均值化
    [pc_axis,patchs_newbasis,varaxis] = ...
        princomp_eig(patchs - cst_patchs * ones(1, P));

    %(256036, 1), (256036, 48)】
    patchs_ppca.coefs = [ cst_patchs*sqrt(P) patchs_newbasis(:,1:end-1) ];
    patchs_ppca.iddico = ones(MN,1); %256036,1%patchs_ppca.score_rpjct=ones(M,N);

    patchs_ppca.dicos{1}.size = P;
    % 【(49,1)(49,48)】
    patchs_ppca.dicos{1}.axis = [1/sqrt(P)*ones(P,1) pc_axis(:,1:end-1)];
     %1,49)所有patch在49个像素点上各自的均值
    patchs_ppca.dicos{1}.mean = mean(patchs - cst_patchs * ones(1, P), 1);
    %0; (481)】
    patchs_ppca.dicos{1}.varaxis = [0; varaxis(1:end-1)];

    if isimage
        patchs_ppca.iddico = reshape(patchs_ppca.iddico, M, N); %506506)
        patchs_ppca.coefs = reshape(patchs_ppca.coefs, M, N, P); %506,506,49)
    end

在这里插入图片描述
[2] 对应于2)硬阈值部分
[3] 重点是每一部分patch重建的过程

function patchs = reconstruction_ppca(patchs_pca)

%reconstruction  computes the values of the patches, knowing the 
% 		 structure (dico+coefs) of the patch_pca 
%                 
%   INPUT:
%     patchs_pca	 : structures of the patches in the PCA basis

%   OUTPUT:
%     patchs             : the patches in original domain 
%   
%patchs = reconstruction_ppca(patchs_pca)
%   computes the patchs values knowing patchs_pca, the structure  
%   of those patches in the space spanned by dicos.  
%
%see also reconstruction.m and prejection.m (inverse operation)
%
%   Copyright (C) 2011 GP-PCA project
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)
%   Copyright (C) 2011 GP-PCA project
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)

%---------------------------------------------------------------------
%
%   This file is part of GP-PCA.
%
%   GP-PCA is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as
%   published by the Free Software Foundation, either version 3 of
%   the License, or (at your option) any later version.
%
%   GP-PCA is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public
%   License along with GP-PCA.  If not, see
%   <http://www.gnu.org/licenses/>.
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)

	% 获取重建图像需要的参数,
	% 需要在新坐标轴上的坐标参数coefs、
	% 其他信息dicos(包括每个patch的像素数,每个patch去均值化后的均值,新的坐标轴、每个新坐标轴上的方差)
    if size(patchs_pca.coefs, 3) > 1
        isimage = 1;
        [M,N,P] = size(patchs_pca.coefs);
        MN = M*N;
        coefs = reshape(patchs_pca.coefs, MN, P);
        iddico = reshape(patchs_pca.iddico, MN, 1); % iddico很奇怪,其所有值全部为1而已
    else
        isimage = 0;
        [MN,P] = size(patchs_pca.coefs);
        coefs = patchs_pca.coefs;
        iddico = patchs_pca.iddico;
    end
    % (49,49) -> 49
    P = size(patchs_pca.dicos{1}.axis, 1);
    % 1
    L = length(patchs_pca.dicos);
    % 初始化待重构的图片所有像素值为0
    patchs = zeros(MN, P);
    for i = 1:L
    	% 【(256036,1*1,256036)的转置 】的转置=256036,1)的转置 =1,256036% 这个到底在说什么意思呢
    	% 其实就是获取有效的索引,这里的索引对应的是从原图片中提取出来的所有patch
        idx = ((iddico == i) .* (1:(MN))')';
        % ?应该都是大于0的吧
        idx = idx(idx > 0);

        K = size(idx, 2); % 有效索引的个数
        subK = min(K, 10000);
        k = 1;
        % 迭代重建过程:疑问的是这里为什么是从2开始的?
        %11000) + 1 ==> (2: 1001)
        %  (1: 1000) + 246035(K - subK - 1) ==> (K - subK, K - 1)
        while k < K - subK
            % (1,1000)
            range = (1:subK) + k;
            % 按顺序依次获取一部分patch, 这里对coefs是进行行索引
            subcoefs = coefs(idx(range), :);
            % 个人理解应该是将新坐标变换回旧坐标 并加上相应的均值
            patchs(idx(range), :) = ...
                subcoefs * patchs_pca.dicos{i}.axis' + ... % (1000,49) * (49, 49)
                ones(size(range, 2), 1) * patchs_pca.dicos{i}.mean; % (1000,1) * (1,49)
            %repmat(patchs_pca.dicos{i}.mean, size(range, 2), 1);
            clear subcoefs;
            k = k + subK;
        end
        % 对剩下长度不足subKd的所有patch来进行重建
        if k <= K
            range = (k:K);
            subcoefs = coefs(idx(range), :);
            patchs(idx(range), :) = ...
                subcoefs * patchs_pca.dicos{i}.axis' + ...
                ones(size(range, 2), 1) * patchs_pca.dicos{i}.mean;
            %repmat(patchs_pca.dicos{i}.mean, size(range, 2), 1);
            clear subcoefs;
        end
    end
    clear coefs;
    clear iddico;
    if isimage
        patchs = reshape(patchs, M, N, P); %506,506,49(
    end

[4]重点是叠加像素值然后求平均值

function ima = reprojection_UWA(ima_patchs,xg,yg)

%reprojection_UWA  compute uniform weight reprojections from  
% 		 patches to pixels values 
%                 
%   INPUT:
%     ima_patchs      : values of the collection of patches
%     xg, yg          : Initial values of the image/normalization, default are
%				 xg = zeros(M,N);
%  			         yg = zeros(M,N);
%   OUTPUT:
%     ima             : image in the original domain 
%   
%ima = reprojection_UWA(ima_patchs,xg,yg)
%   computes the pixels values knowing the values of the whole collections of  
%   the patches of an image. The reprojection is uniform.  
%
%
%   Copyright (C) 2011 GP-PCA project
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)
%
%---------------------------------------------------------------------
%
%   This file is part of GP-PCA.
%
%   GP-PCA is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as
%   published by the Free Software Foundation, either version 3 of
%   the License, or (at your option) any later version.
%
%   GP-PCA is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public
%   License along with GP-PCA.  If not, see
%   <http://www.gnu.org/licenses/>.
%   Charles Deledalle, Joseph Salmon, Arnak S. Dalalyan
%
%   See The GNU Public License (GPL)

    [M, N, P] = size(ima_patchs);
    if nargin <= 2  %判断有几个输入参数
    % 感觉这里xg和yg都没有什么用,因为后面根本用不到,就算用到了值也全部是0,没有发生任何更新
        xg = zeros(M,N);
        yg = zeros(M,N);
    end
    p = sqrt(P);
    ima = zeros(M+p-1, N+p-1);
    norm = zeros(M+p-1, N+p-1);
    % 就是把去噪后的patch依次叠加在同原始图片一样大的图片画布上
    % 并且统计每个像素点的值是来自多少个patch的叠加
    % 然后进行一个平均就可以了
    for i = 1:M
        for j = 1:N
            xrange = mod(round((i-1)+(1:p)+xg(i,j))-1,M+p-1)+1;%(i,i+p-1)
            yrange = mod(round((j-1)+(1:p)+yg(i,j))-1,N+p-1)+1;% (j, j+p-1)
            ima(xrange, yrange) = ...
                ima(xrange, yrange) + ...
                reshape(ima_patchs(i,j,:), p, p);
            norm(xrange, yrange) = ...
                norm(xrange, yrange) + ...
                1;
        end
    end
    ima = ima ./ norm;
    clear norm;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值