将滤镜应用于图像修补程序并将每个像素设置为每个修补程序的结果的平均值

许多现代图像处理算法使用补丁是他们工作的基本要素。
例如,可以对贴片进行去噪(参见 BM3D 算法)。

然而,当从处理过的补丁构建图像时,我们对同一像素有很多结果。
处理它的一种方法是取同一像素的所有值的平均值(经验平均值)。

以下代码显示了如何将图像分解为修补程序,并使用 [accumarray()][1] 使用平均值从修补程序重建图像:

numRows = 5;
numCols = 5;

numRowsPatch = 3;
numColsPatch = 3;

% The Image
mI = rand([numRows, numCols]);

% Decomposing into Patches - Each pixel is part of many patches (Neglecting
% boundariwes, each pixel is part of (numRowsPatch * numColsPatch) patches).
mY = ImageToColumnsSliding(mI, [numRowsPatch, numColsPatch]);

% Here one would apply some operation which work on patches

% Creating image of the index of each pixel
mPxIdx = reshape(1:(numRows * numCols), [numRows, numCols]);

% Creating patches of the same indices
mSubsAccu = ImageToColumnsSliding(mPxIdx, [numRowsPatch, numColsPatch]);

% Reconstruct the image - Option A
mO = accumarray(mSubsAccu(:), mY(:)) ./ accumarray(mSubsAccu(:), 1);

% Reconstruct the image - Option B
mO = accumarray(mSubsAccu, mY(:), [(numRows * numCols), 1], @(x) mean(x));

% Rehsape the Vector into the Image
mO = reshape(mO, [numRows, numCols]);