Dictionary based non-local mean implementation in Matlab

Given a dictionary including multiple X-Y pairs where X, Y are both three dimensional structure. dictionaryBasedNonlocalMean function return a output which is weighted sum of each Y in the dictionary. Each weight of Y are based on the Manhattan distance between the input of dictionaryBasedNonlocalMean function and the corresponding X in the dictionary. In mathematical form, output is calculated with the following way.

$$output = K\sum_{{i = 1}}^{{N}_{D}} \left[ G_{\mu = 0, \sigma} (\left\|input - X(i)\right\|_{1}) \cdot Y(i) \right]$$

where ND is the count of X-Y pairs (the cardinality of set X and set Y) in the dictionary and the Gaussian function

$$G_{\mu = 0, \sigma} (\left\|input - X(i)\right\|_{1}) = \left.e^{\frac{-(\left\|input - X(i)\right\|_{1} - \mu)^2}{2 {\sigma}^{2}}}\right|_{\mu=0}$$

Moreover, K is a normalization factor

$$K = \frac{1}{\sum_{{i = 1}}^{{N}_{D}} G_{\mu = 0, \sigma} (\left\|input - X(i)\right\|_{1})}$$ The experimental implementation

• ManhattanDistance function

function output = ManhattanDistance(X1, X2)
output = sum(abs(X1 - X2), 'all');
end

• dictionaryBasedNonlocalMean function

function [output] = dictionaryBasedNonlocalMean(Dictionary, input)
gaussian_sigma = 0.1;
gaussian_mean = 0;
if size(Dictionary.X) ~= size(Dictionary.Y)
disp("Size of data in dictionary incorrect.");
output = [];
return
end
[X, Y, Z, DataCount] = size(Dictionary.X);
weightOfEach = zeros(1, DataCount);
for i = 1:DataCount
%   Gaussian of distance between X and input
weightOfEach(i) = gaussmf(ManhattanDistance(input, Dictionary.X(:, :, :, i)), [gaussian_sigma gaussian_mean]);
end
sumOfDist = sum(weightOfEach, 'all');
output = zeros(X, Y, Z);
%%% if sumOfDist too small
if (sumOfDist < 1e-160)
fprintf("sumOfDist = %d\n", sumOfDist);
return;
end
for i = 1:DataCount
output = output + Dictionary.Y(:, :, :, i) .* weightOfEach(i);
end
output = output ./ sumOfDist;
end

Test case

ND = 10;
xsize = 8;
ysize = 8;
zsize = 1;
Dictionary = CreateDictionary(ND, xsize, ysize, zsize);
output = dictionaryBasedNonlocalMean(Dictionary, ones(xsize, ysize, zsize) .* 0.66)

function Dictionary = CreateDictionary(ND, xsize, ysize, zsize)
Dictionary.X = zeros(xsize, ysize, zsize, ND);
Dictionary.Y = zeros(xsize, ysize, zsize, ND);
for i = 1:ND
Dictionary.X(:, :, :, i) = ones(xsize, ysize, zsize) .* (i / ND);
Dictionary.Y(:, :, :, i) = ones(xsize, ysize, zsize) .* (1 + i / ND);
end
end

The output result of testing code above:

output =

1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622
1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622
1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622
1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622
1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622
1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622
1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622
1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622    1.6622

All suggestions are welcome.