roifilt2.m

来自「有关matlab的电子书籍有一定的帮助希望有用」· M 代码 · 共 99 行

M
99
字号
function y = roifilt2(varargin)
%ROIFILT2 Filter region of interest.
%   J = ROIFILT2(H,I,BW) filters the data in I with the 2-D
%   linear filter H. BW is a binary image the same size as I that
%   is used as a mask for filtering. ROIFILT2 returns an image
%   that consists of filtered values for pixels in locations
%   where BW contains 1's, and unfiltered values for pixels in 
%   locations where BW contains 0's. For this syntax, ROIFILT2
%   calls FILTER2 to implement the filter.
%
%   J = ROIFILT2(I,BW,FUN) processes the data in I using the
%   function FUN. The result J contains computed values for
%   pixels in locations where BW contains 1's, and the actual
%   values in I for pixels in locations where BW contains 0's.
%
%   FUN can be a string containing the name of a function, a
%   string containing an expression, or an inline function
%   object. FUN should take a matrix as a single argument and
%   return a matrix of the same size:
%
%       y = FUN(x)
%
%   J = ROIFILT2(I,BW,FUN,P1,P2,...) passes the additional
%   parameters P1,P2,..., to FUN.
%
%   Class Support
%   -------------
%   For the syntax that includes a filter H, the input image can
%   be of class double or uint8, and the output array J is of
%   class double. For the syntax that includes a function, I can
%   be of any class supported by FUN, and the class of J depends
%   on the class of the output from FUN.
%
%   Example
%   -------
%       I = imread('eight.tif');
%       c = [222 272 300 270 221 194];
%       r = [21 21 75 121 121 75];
%       BW = roipoly(I,c,r);
%       h = fspecial('unsharp');
%       J = roifilt2(h,I,BW);
%       imshow(I), figure, imshow(J)
%
%   See also FILTER2, ROIPOLY.

%   Clay M. Thompson 2-10-93
%   Copyright 1993-1998 The MathWorks, Inc.  All Rights Reserved.
%   $Revision: 1.8 $  $Date: 1997/11/24 15:36:21 $

if (nargin < 3)
   error('At least three inputs required');
end

if (isstr(varargin{3}) | isa(varargin{3},'inline'))
   
   % J = ROIFILT2(I,BW,'fun')
   y = varargin{1};      
   if (ndims(y) > 2)
       error('I must be a 2-d matrix.');
   end
   if islogical(varargin{2})
      d = varargin{2};
   else
      d = varargin{2} ~= 0;
   end
   fun = varargin{3};
   params = varargin(4:end);
   fun = fcnchk(fun,length(params));
   c = feval(fun, y, params{:});
   if any(size(c)~=size(y)), 

   end
   
else
   
   % J = ROIFILT2(H,I,BW)
   h = varargin{1};
   y = varargin{2};       
   if (ndims(y) > 2)
       error('I must be a 2-d matrix.');
   end
   if islogical(varargin{3})
      d = varargin{3};
   else
      d = varargin{3} ~= 0;
   end
   c = filter2(h,y);    
   
end


% See if we will return a uint8
if isa(y, 'uint8') & isa(c, 'double')
   % Make sure C lies in the proper range
   c = max(0, min(255,c));
   c = uint8(round(c));  % c is always a double
end
y(d) = c(d);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?