Professional Documents
Culture Documents
niter = options.niter;
else
niter = 20;
end
if isfield(options, 'Tmax')
Tmax = options.Tmax;
else
Tmax = 0.2*max(abs(M(:)));
end
if isfield(options, 'Tmin')
Tmin = options.Tmin;
else
Tmin = 0;
end
if isfield(options, 'threshold')
threshold = options.threshold;
else
threshold = 'hard';
end
if isfield(options, 'saverep')
saverep = options.saverep;
else
saverep = [];
end
if isfield(options, 'InpaintingMask')
InpaintingMask = options.InpaintingMask;
else
InpaintingMask = [];
end
s = length(components);
% init the residual
n = size(M,1);
ML = zeros(n,n,s);
if not(isempty(InpaintingMask))
q = sum(InpaintingMask(:)==Inf);
for k=1:s
Mk = ML(:,:,k);
Mk(InpaintingMask==Inf) = rand( q,1 ) * max( M(InpaintingMask~=Inf) );
ML(:,:,k) = Mk;
end
end
MLsave = [];
for i=1:niter
progressbar(i,niter);
% current threshold
T0 = Tmax + (i-1)/(niter-1)*(Tmin-Tmax);
for k=1:s
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% current component
cpt = components{k};
% callback function
callback = cpt.callback;
% options
clear opt; opt.null = 0;
if isfield(cpt, 'options')
opt = cpt.options;
end
% threshold factor amplification
threshold_factor = 1;
if isfield(cpt, 'threshold_factor')
threshold_factor = cpt.threshold_factor;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute residual
T = T0 * threshold_factor;
opt.T = T;
% compute residual
sel = 1:s; sel(k) = [];
R = M - sum( ML(:,:,sel), 3);
if not(isempty(InpaintingMask))
% enforce known values only outside
Mk = ML(:,:,k);
R(InpaintingMask==Inf) = Mk(InpaintingMask==Inf);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% perform forward transform
RW = feval(callback, R,+1,opt);
% perform thresholding
if strcmp(cpt.name, 'wav')
% do not threshold the low frequency
RW1 = perform_thresholding({RW{1:end-1}},T, threshold);
RW = {RW1{:}, RW{end}};
else
RW = perform_thresholding(RW,T, threshold);
end
% perform backward transform
ML(:,:,k) = feval(callback, RW,-1,opt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TV additional sparsening
if isfield(cpt, 'tv_correction') && cpt.tv_correction==1
if isfield(options, 'tv_weight')
tv_weight = options.tv_weight;
else
tv_weight = 2.5*max(abs(M(:)))/256;
end
ML(:,:,k) = perform_tv_correction(ML(:,:,k),tv_weight);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% save result
if not(isempty(saverep))
MLsave(:,:,:,i) = ML;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% save all the data
if not(isempty(saverep))
% normalize the evolution
vmax = max(max(max(MLsave,[],1),[],2),[],4);
vmax =
vmin =
vmin =
MLsave
repmat(vmax, [n n 1 niter]);
min(min(min(MLsave,[],1),[],2),[],4);
repmat(vmin, [n n 1 niter]);
= (MLsave-vmin) ./ (vmax-vmin);
for k=1:s
cpt = components{k};
for i=1:niter
if isfield(cpt, 'name')
str = cpt.name;
else
str = ['layer' num2str(k)];
end
str = [str '-iter' num2string_fixeddigit(i,2)];
warning off;
imwrite( MLsave(:,:,k,i), [saverep str '.png'], 'png' );
warning on;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = perform_hard_tresholding(x,t)
if iscell(x)
for i=1:length(x)
y{i} = perform_hard_tresholding(x{i},t);
end
return;
end
y = x .* (abs(x) > t);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = perform_soft_tresholding(x,t)
if iscell(x)
for i=1:length(x)
y{i} = perform_soft_tresholding(x{i},t);
end
return;
end
s = abs(x) - t;
s = (s + abs(s))/2;
y = sign(x).*s;