Documente Academic
Documente Profesional
Documente Cultură
if((min(I(:))<0)||(max(I(:))>1)),
warning('NLMF:inputs','Preferable data range [0..1]');
end
if((~isa(I,'double'))&&(~isa(I,'single')))
error('NLMF:inputs','Input data must be single or double');
end
is2D=size(I,3)<4;
% Process inputs
defaultoptions=struct('kernelratio',3,'windowratio',3,'filterstrength',0.05,'blocks
ize',150,'nThreads',2,'verbose',false,'enablepca',false,'pcaskip',10,'pcane',25);
if(is2D), defaultoptions.blocksize=150; else defaultoptions.blocksize=32; end
if(~exist('Options','var')), Options=defaultoptions;
else
tags = fieldnames(defaultoptions);
for i=1:length(tags), if(~isfield(Options,tags{i})), Options.
(tags{i})=defaultoptions.(tags{i}); end, end
if(length(tags)~=length(fieldnames(Options))),
warning('NLMF:unknownoption','unknown options found');
end
end
kernelratio=round(Options.kernelratio);
windowratio=round(Options.windowratio);
filterstrength=Options.filterstrength;
blocksize=round(Options.blocksize);
nThreads=round(Options.nThreads);
verbose=Options.verbose;
enablepca=Options.enablepca;
pcaskip=round(Options.pcaskip);
pcane=round(Options.pcane);
if(is2D)
Ipad = padarray(I,[kernelratio+windowratio
kernelratio+windowratio],'symmetric'); %,
else
Ipad = padarray(I,[kernelratio+windowratio kernelratio+windowratio
kernelratio+windowratio],'symmetric'); %,
end
% Separate the image into smaller blocks, for less memory usage
% and efficient cpu-cache usage.
block=makeBlocks(kernelratio,windowratio, blocksize, I, Ipad, is2D);
tic; erms='***';
J=zeros(size(I),class(Ipad));
for i=1:length(block);
if(verbose)
disp(['Processing Block ' num2str(i) ' of ' num2str(length(block)) '
estimated time remaining ' erms]);
end
if(is2D)
Iblock=Ipad(block(i).x1:block(i).x2,block(i).y1:block(i).y2,:);
else
Iblock=Ipad(block(i).x1:block(i).x2,block(i).y1:block(i).y2,block(i).z1:block(i).z2
);
end
if(isa(Ipad,'double'))
% Get the local patches of every pixel-coordinate in the block
V=image2vectors_double(double(Iblock),double(kernelratio),double(nThreads));
else
V=image2vectors_single(single(Iblock),single(kernelratio),single(nThreads));
end
if(enablepca)
% Do PCA on the block
[Evalues, Evectors, x_mean]=PCA(V(:,1:pcaskip:end),pcane);
% Project the block to the reduced PCA feature space
V = Evectors'*(V-repmat(x_mean,1,size(V,2)));
end
Iblock_filtered=vectors_nlmeans_double(double(Iblock),double(V),double(kernelratio)
,double(windowratio),double(filterstrength),double(nThreads));
else
Iblock_filtered=vectors_nlmeans_single(single(Iblock),single(V),single(kernelratio)
,single(windowratio),single(filterstrength),single(nThreads));
end
if(is2D)
J(block(i).x3:block(i).x4,block(i).y3:block(i).y4,:)=Iblock_filtered;
else
J(block(i).x3:block(i).x4,block(i).y3:block(i).y4,block(i).z3:block(i).z4)=Iblock_f
iltered;
end
if(verbose)
t=toc; erm=(t/i)*(length(block)-i); erms=num2str(erm);
end
end
toc;
% Do the SVD
[U2,S2] = svds(x2,ne,'L',struct('tol',1e-4));
Evalues=diag(S2).^2;
Evectors=U2;