function y = nldifc( u, lambda, sigma, m, stepsize, nosteps, varargin)
%
% NLDIFC Color Nonlinear Diffusion
%
% y = NLDIFC( u, lambda, sigma, m, stepsize, steps, verbose, drawstep ) returns the
% image y as the result of the application of the Non Linear diffusion
% to the image u (See NLDIF for details).
%
% NLDIFC performs an independent diffusion in each color channel of u. If the used
% parameters are scalars or row vectors, the same parameter will be used for every
% channel. To use different parameters for each channel, each parameter must be passed
% as a 1xCH cell array. String parameters cannot be defined for each channel individually
%
% Example: y = nldif(u, {3 4 5}, 1, 10, .2, 10) will diffuse ch 1 using lambda = 3
% ch 2 with lambda = 4 and ch3 with lambda = 5. Every other parameter will be the
% same for the three channels.
%
% y = NLDIFC( ..., 'alt1') computes the diffusivity based on the p-norm of the gradient
% in each channel and filters all channels at once instead of using each channel separately.
% By default p = 2. For other norms use : y = NLDIFC(..., 'alt1','norm',p). p can be any
% number or 'inf'.
%
% See also: NLDIF, PMDIF, EEDIF, CEDIF, DIFFUSIVITY, GSDPLOT, FLUXPLOT
%
Nch = size(u,3);
[verbose, drawstep, imscale, plotgrad, plotflux, dfstep, aos, alt1, pnorm] = parse_inputs(varargin{:});
% Check Inputs
[lambda, sigma, m, stepsize, nosteps, verbose, drawstep, dfstep] = check_inputs(Nch,lambda, sigma, m, stepsize, nosteps, verbose, drawstep, dfstep, alt1);
if alt1 == 0
y = zeros(size(u));
for ch = 1 : size(u,3)
% MAKEVAR
v = makevar(verbose{ch},drawstep{ch},imscale,plotgrad,plotflux,dfstep{ch},aos,ch);
disp( ['Processing channel : ', int2str(ch) , ' / ' , int2str(Nch) ] );
eval([ 'y(:,:,' , int2str(ch) , ') = nldif(u(:,:, ' , int2str(ch) , ' ),lambda{' , int2str(ch) ,'},sigma{' , int2str(ch) ,'},m{' , int2str(ch) ,'},stepsize{' , int2str(ch) ,'},nosteps{' , int2str(ch) ,'}, ' , v , ');' ]) ;
end
y = scale(y,[0 1]); % Scaling
%Plotting
if any( cat(1,verbose{:}) )
subplot(1,2,1)
imagesc(u);
title('Original Image')
subplot(1,2,2)
imagesc(y)
title('Non Linear Diffusion')
end
else % alt1 = 1 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% Variable initialization
dif_time = 0;
if strcmp(class(u),'double')
y = u;
else
y = double(u);
end
g = cell(1,Nch);
for j = 1 : Nch
g{j} = zeros( size(y(:,:,1)) );
end
% Verifying inputs
if alt1 == 0
for j = 1 : Nch
[lambda{j}, sigma{j}, stepsize{j}] = verify_inputs(lambda{j}, sigma{j}, stepsize{j}, nosteps{j});
end
else
for j = 1 : Nch
[lambda{j}, sigma{j}, stepsize] = verify_inputs(lambda{j}, sigma{j}, stepsize, nosteps);
end
end
% Initial Drawing
if verbose
figure(verbose);
subplot(1,2,1);
if ndims(y) == 3
image(scale(y,[0 1]));
else
if strcmp(imscale,'imscale')
imagesc(y);
else
image(y);
end
colorbar
end
title('Original Image'); drawnow;
difplot(y, 0, 0, 'Non Linear Diffusion', verbose, imscale);
end
% Calculate Cm constant
for j = 1 : Nch
Cm{j}= Cmcalc(m{j});
end
for i=1:nosteps
if mod(i-1,dfstep) == 0 % diffusivity recalc step
grad = zeros( size(y(:,:,1)) );
% Calculate gradient of smoothed image
for j = 1 : Nch
[gradx, grady] = gsderiv(y(:,:,j), sigma{j}(i), 1);
if strcmp(pnorm,'inf')
grad = max(grad, sqrt(gradx.^2 + grady.^2) );
elseif pnorm == 2
grad = grad + gradx.^2 + grady.^2;
elseif pnorm == 1
grad = grad + sqrt(gradx.^2 + grady.^2);
else
grad = grad + (sqrt(gradx.^2 + grady.^2)).^pnorm;
end
end
if ~strcmp(pnorm,'inf') & pnorm~=1
grad = grad.^(1/pnorm); % Not necessary to do anything if pnorm == 'inf' or 1.
end
if plotgrad
figure(verbose+1)
imagesc(grad);
title('Grad. Magnitude')
colorbar
axis off
end
% Calculate difusivity
if samedif(lambda,m,i) % If lambda and m are the same for all channels in this step
g{1} = 1 - exp(-Cm{1}./( (grad+eps)./lambda{1}(i)).^m{1});
for j = 2 : Nch
g{j} = g{1};
end
else
for j = 1 : Nch
g{j} = 1 - exp(-Cm{j}./( (grad+eps)./lambda{j}(i)).^m{j}); % grad + eps to avoid division by zero (eps -> 0)
end
end
end % diff. recalc
% Calculate dy/dt
if aos
if plotflux
yo = y;
end
for j = 1 : Nch
y(:,:,j) = aosiso(y(:,:,j),g{j},stepsize(i)); % updating
end
else
for i = 1 : Nch
dy(:,:,1) = isodifstep(y(:,:,1), g{j});
end
y = y + stepsize(i) * dy; % updating
end
% Calculate diffusion time
dif_time = dif_time + stepsize(i);
% Plot actualization
if verbose & ~mod(i,drawstep)
difplot(y, dif_time, i, 'Non Linear Diffusion', verbose, imscale);
if plotflux
figure(verbose+plotgrad+1)
if aos
fluxplot(g,grad, norm3(abs(y-yo)), 'axisoff')
else
fluxplot(g,grad, norm3( abs(stepsize(i).*(dy)) ), 'axisoff')
end
end
end % Plot actualization
end % i = 1 : nosteps
% Last plot
difplot(y, dif_time, i, 'Non Linear Diffusion', verbose, imscale);
if plotflux
figure(verbose+plotgrad+1)
if aos
fluxplot(g,grad, norm3(abs(y-yo)), 'axisoff')
else
fluxplot(g,grad, norm3( abs(stepsize(i).*(dy)) ), 'axisoff')
end
end
end % alt1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = cellcomp(x,n)
for i = 1 : n
y{i} = x;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [verbose, drawstep, imscale, plotgrad, plotflux, dfstep, aos, alt1, pnorm] = parse_inputs(varargin)
pnorm = 2;
pnormpos = -1;
alt1 = 0;
aos = 0;
dfsteppos = -1;
dfstep = 1;
verbose = -1;
drawstep = -1;
imscale = 'null';
plotgrad = 0;
plotflux = 0;
for i = 1 : length(varargin)
flag = 0;
if i == dfsteppos
flag = 1;
end
if i == pnormpos
flag = 1;
end
if strcmp(varargin{i},'hsv')
hsv = 1;
flag = 1;
elseif strcmp(varargin{i},'norm')
pnorm = varargin{i+1};
flag = 1;
pnormpos = i+1;
elseif strcmp(varargin{i},'alt1')
alt1 = 1;
flag = 1;
elseif strcmp(varargin{i},'imscale')
imscale = 'imscale';
flag = 1;
elseif strcmp(varargin{i},'grad')
plotgrad = 1;
flag = 1;
elseif strcmp(varargin{i},'flux')
plotflux = 1;
flag = 1;
elseif strcmp(varargin{i},'dfstep')
dfstep = varargin{i+1};
flag = 1;
dfsteppos = i+1;
elseif strcmp(varargin{i},'aos')
aos = 1;
flag = 1;
end
if flag == 0 & verbose == -1
verbose = varargin{i};
flag = 1;
end
if flag == 0 & drawstep == -1
drawstep = varargin{i};
flag = 1;
end
if flag == 0
error('Too many parameters !')
return
end
end
if verbose == -1
verbose = 0;
end
86120636.rar_图形/文字识别
版权申诉
69 浏览量
2022-07-14
03:39:14
上传
评论
收藏 151KB RAR 举报
朱moyimi
- 粉丝: 64
- 资源: 1万+
最新资源
- HTML+CSS制作的个人博客网页.zip
- IMG_20240521_094903.jpg
- 基于htmlde 爱心代码,但HTML网页源码.zip
- 51单片机实现LED流水灯
- 基于Python的obEspoir分布式游戏框架设计源码
- 基于python的机械设计实用计算器,可计算电动机,传动装置,V带轮,齿轮,轴,轴承的几何或者力,运动学参数数值+源码+开发文档
- 基于HTML +JavaScript的元旦倒计时代码.docx
- 【Unity资源免费分享】孩子益智小游戏unity 5x系列Baby Doll House Cleaning
- 【资源免费分享】集市游戏(uniyt案例)
- 数据整理结果 2023-12-7 192544 6.dta
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈