nlfilter.m
来自「有关matlab的电子书籍有一定的帮助希望有用」· M 代码 · 共 134 行
M
134 行
function b = nlfilter(varargin)
%NLFILTER Perform general sliding-neighborhood operations.
% B = NLFILTER(A,[M N],FUN) applies the function FUN to
% each M-by-N sliding block of A. FUN may be a string
% containing the name of a function, a string containing an
% expression, or an inline function object. FUN should accept
% an M-by-N block as input, and return a scalar result:
%
% C = FUN(X)
%
% C is the output value for the center pixel in the M-by-N
% block X. NLFILTER calls FUN for each pixel in A. NLFILTER
% zero pads the M-by-N block at the edges, if necessary.
%
% B = NLFILTER(A,[M N],FUN,P1,P2,...) passes the additional
% parameters P1,P2,..., to FUN.
%
% B = NLFILTER(A,'indexed') processes A as an indexed image,
% padding with ones if A is of class double and zeros if A is
% of class uint8.
%
% Class Support
% -------------
% The input image A can be of any class supported by FUN. The
% class of B depends on the class of the output from FUN.
%
% Remarks
% -------
% NLFILTER can take a long time to process large images. In
% some cases, the COLFILT function can perform the same
% operation much faster.
%
% Example
% -------
% This call produces the same result as calling MEDFILT2 with a
% 3-by-3 neighborhood:
%
% B = NLFILTER(A,[3 3],'median(x(:))');
%
% See also BLKPROC, COLFILT.
% Clay M. Thompson 1-25-93
% Copyright 1993-1998 The MathWorks, Inc. All Rights Reserved.
% $Revision: 5.11 $ $Date: 1997/11/24 15:35:59 $
[a, nhood, fun, params, padval] = parse_inputs(varargin{:});
% Expand A
[ma,na] = size(a);
if (isa(a,'uint8'))
% padval should always be 0 for uint8 inputs
aa = repmat(uint8(0),size(a)+nhood-1);
else
if (padval == 1)
aa = ones(size(a)+nhood-1);
else
aa = zeros(size(a)+nhood-1);
end
end
aa(floor((nhood(1)-1)/2)+[1:ma],floor((nhood(2)-1)/2)+[1:na]) = a;
% Find out what output type to make.
rows = [0:nhood(1)-1]; cols = [0:nhood(2)-1];
if (isa(feval(fun,aa(1+rows,1+cols),params{:}), 'uint8'))
b = repmat(uint8(0), size(a));
else
b = zeros(size(a));
end
% Apply m-file to each neighborhood of a
f = waitbar(0,'Applying neighborhood operation...');
for i=1:ma,
for j=1:na,
x = aa(i+rows,j+cols);
b(i,j) = feval(fun,x,params{:});
end
waitbar(i/na)
end
close(f)
%%%
%%% Function parse_inputs
%%%
function [a, nhood, fun, params, padval] = parse_inputs(varargin)
switch nargin
case 0
error('Too few inputs to NLFILTER');
case 1
error('Too few inputs to NLFILTER');
case 2
error('Too few inputs to NLFILTER');
case 3
if (strcmp(varargin{2},'indexed'))
error('Too few inputs to NLFILTER');
else
% NLFILTER(A, [M N], 'fun')
a = varargin{1};
nhood = varargin{2};
fun = varargin{3};
params = cell(0,0);
padval = 0;
end
otherwise
if (strcmp(varargin{2},'indexed'))
% NLFILTER(A, 'indexed', [M N], 'fun', P1, ...)
a = varargin{1};
nhood = varargin{3};
fun = varargin{4};
params = varargin(5:end);
padval = 1;
else
% NLFILTER(A, [M N], 'fun', P1, ...)
a = varargin{1};
nhood = varargin{2};
fun = varargin{3};
params = varargin(4:end);
padval = 0;
end
end
if (isa(a,'uint8'))
padval = 0;
end
% If we have been given an eval-able expression, remake it as an
% inline function object.
if (isstr(fun) & (any(fun<48) | any(fun>=91 & fun<=94)))
fun = inline(fun,length(params));
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?