⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 erode.m

📁 有关matlab的电子书籍有一定的帮助希望有用
💻 M
字号:
function cout = erode(varargin)
%ERODE Perform erosion on binary image.
%   BW2 = ERODE(BW1,SE) performs erosion on the binary image
%   BW1 using the binary structuring element SE.  SE is a matrix
%   containing only 1's and 0's.
%
%   BW2 = ERODE(BW1,SE,ALG) performs erosion using the
%   specified algorithm.  ALG is a string that can have one of
%   these values:
%
%     'spatial'   (default) - processes the image in the spatial
%                 domain. This algorithm works well for
%                 relatively small images and structuring
%                 elements, but for large images and structuring
%                 elements it can be slow.
%
%     'frequency' - processes the image in the frequency
%                 domain. This algorithm is faster for large
%                 images and structuring elements than 'spatial',
%                 but uses much more memory. 
%
%   The spatial and frequency algorithms produce the same result,
%   but make different tradeoffs between speed and memory
%   usage. If you want to speed up erosion, specify the
%   'frequency' algorithm. If you receive "out of memory"
%   messages or if your computer slows down because of disk
%   paging, specify the 'spatial' algorithm.
%
%   BW2 = ERODE(BW1,SE,...,N) performs the erosion operation N
%   times.
%
%   Class Support
%   -------------
%   The input image BW1 can be of class double or uint8.  The
%   output image BW2 is of class uint8.
%
%   Example
%   -------
%       BW1 = imread('text.tif');
%       SE = ones(3,1);
%       BW2 = erode(BW1,SE);
%       imshow(BW1), figure, imshow(BW2)
%
%   See also BWMORPH, DILATE.

%   Clay M. Thompson 8-9-93
%   Revised Steven L. Eddins April 1996
%   Copyright 1993-1998 The MathWorks, Inc.  All Rights Reserved.
%   $Revision: 5.7 $  $Date: 1997/11/24 15:34:38 $

[u, b, method, n] = parse_inputs(varargin{:});

if (~isa(u,'uint8'))
    u = uint8(u);
end



if (strcmp(method,'erode') | strcmp(method,'skeleton') | ...
            strcmp(method,'shrink') | strcmp(method,'thin'))
    
    u = bwmorph(u, method, n);
    
else
    order = sum(b(:) ~=0);
    
    % Fix the origin if necessary.
    [num_rows, num_cols] = size(b);
    if (rem(num_rows, 2) == 0)
        b = [zeros(1, num_cols) ; b];
        num_rows = num_rows + 1;
    end
    if (rem(num_cols, 2) == 0)
        b = [zeros(num_rows, 1) b];
    end
  
    if (strcmp(method, 'spatial'))
        for i = 1:n
            u = ordfilt2(u, 1, b);
        end

    elseif (strcmp(method, 'frequency'))
        b = rot90(b,2);
        
        % Zero-pad the input and the structuring elements to the
        % appropriate power-of-2 dimensions.
        [Mu,Nu] = size(u);
        [Mb,Nb] = size(b);
        shiftRow = floor((Mb+1)/2);
        shiftCol = floor((Nb+1)/2);
        M = 2^(nextpow2(Mu+Mb+1));
        N = 2^(nextpow2(Nu+Nb+1));
        b(M,N) = 0;
        B = fft2(b);

        for i = 1:n
            % Perform the convolution using fft calls.
            u(M,N) = 0;
            u = uint8(round(real(ifft2(fft2(double(u)) .* B)))) == order;
            u = u(shiftRow:(shiftRow+Mu-1),shiftCol:(shiftCol+Nu-1));
        end
        
    else
        error('Method must be ''spatial'' or ''frequency''');
        
    end
    
end

u = logical(u);

if nargout==0, imshow(u,2), return, end
cout = u;


%%%
%%% Function parse_inputs
%%%
function [bw, se, method, n] = parse_inputs(varargin)

switch nargin
case 0
    error('Too few input arguments to ERODE');
    
case 1
    % ERODE(BW)  --- Grandfathered
    bw = varargin{1};
    se = [];
    method = 'thin';
    n = 1;
    
case 2
    if (isstr(varargin{2}))
        % ERODE(BW,method)  --- Grandfathered
        bw = varargin{1};
        se = [];
        method = varargin{2};
        n = 1;
        
    else
        % ERODE(BW,SE)
        bw = varargin{1};
        se = varargin{2};
        method = 'spatial';
        n = 1;
        
    end
    
case 3
    if (isstr(varargin{2}))
        % ERODE(BW,method,N)  --- Grandfathered
        bw = varargin{1};
        se = [];
        method = varargin{2};
        n = varargin{3};
        
    elseif (isstr(varargin{3}))
        % ERODE(BW,SE,alg)
        bw = varargin{1};
        se = varargin{2};
        method = varargin{3};
        n = 1;
        
    else
        % ERODE(BW,SE,N)
        bw = varargin{1};
        se = varargin{2};
        method = 'spatial';
        n = varargin{3};
        
    end
    
case 4
    % ERODE(BW,SE,alg,N)
    bw = varargin{1};
    se = varargin{2};
    method = varargin{3};
    n = varargin{4};
    
otherwise
    error('Too many input arguments to ERODE');
    
end

methodStrings = ['erode    '
                 'skeleton '
                 'shrink   '
                 'thin     '
                 'spatial  '
                 'frequency'];

idx = strmatch(method, methodStrings);
if (isempty(idx))
    error(sprintf(...
            '"%s" is not one of the allowed method strings for ERODE', ...
            method));
end
if (length(idx) > 1)
    error(sprintf(...
            '"%s" matches multiple method options for ERODE', method));
end

method = deblank(methodStrings(idx,:));

⌨️ 快捷键说明

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