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

📄 dilate.m

📁 有关matlab的电子书籍有一定的帮助希望有用
💻 M
字号:
function cout = dilate(varargin)
%DILATE Perform dilation on binary image.
%   BW2 = DILATE(BW1,SE) performs dilation on the binary image
%   BW1, using the binary structuring element SE.  SE is a matrix
%   containing only 1's and 0's.  
%
%   BW2 = DILATE(BW1,SE,ALG) performs dilation using the
%   specified algorithm.  ALG is a string that can have one of
%   these values:
%
%     'spatial' - 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.
%                 This is the default algorithm.
%     'frequency' processes the image in the frequency
%                 domain. This algorithm is faster for large
%                 images than 'spatial', but uses much more
%                 memory. 
%
%   The spatial and frequency algorithms produce the same result,
%   but make different tradeoffs between speed and memory use. If
%   you want to speed up dilation, specify the 'frequency'
%   algorithm. If you receive "out of memory" messages when you
%   perform dilation, specify the 'spatial' algorithm.
%
%   BW2 = DILATE(BW1,SE,...,N) performs the dilation 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(6,2);
%       BW2 = dilate(BW1,SE);
%       imshow(BW1)
%       figure, imshow(BW2)
%
%   See also ERODE, BWMORPH.

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

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

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



if (strcmp(method,'dilate') | strcmp(method,'fatten') | ...
            strcmp(method,'thicken'))
    
    u = bwmorph(u, method, n);
    
else
    
    order = sum(b(:) ~= 0);

    if (order == 0)
        error('Structuring element must have at least one nonzero element');
    end

    % 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'))
        b = rot90(b,2);
    
        for i = 1:n
            u = ordfilt2(u, order, b);
        end

    elseif (strcmp(method, 'frequency'))
        % Zero-pad the input and the structuring elements to the
        % appropriate power-of-2 dimensions.
        [Mu,Nu] = size(u);
        [Mb,Nb] = size(b);
        shiftRow = (Mb+1)/2;
        shiftCol = (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)))) ~= 0;
            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 DILATE');
    
case 1
    % DILATE(BW)  --- Grandfathered
    bw = varargin{1};
    se = [];
    method = 'thicken';
    n = 1;
    
case 2
    if (isstr(varargin{2}))
        % DILATE(BW,method)  --- Grandfathered
        bw = varargin{1};
        se = [];
        method = varargin{2};
        n = 1;
        
    else
        % DILATE(BW,SE)
        bw = varargin{1};
        se = varargin{2};
        method = 'spatial';
        n = 1;
        
    end
    
case 3
    if (isstr(varargin{2}))
        % DILATE(BW,method,N)  --- Grandfathered
        bw = varargin{1};
        se = [];
        method = varargin{2};
        n = varargin{3};
        
    elseif (isstr(varargin{3}))
        % DILATE(BW,SE,alg)
        bw = varargin{1};
        se = varargin{2};
        method = varargin{3};
        n = 1;
        
    else
        % DILATE(BW,SE,N)
        bw = varargin{1};
        se = varargin{2};
        method = 'spatial';
        n = varargin{3};
        
    end
    
case 4
    % DILATE(BW,SE,alg,N)
    bw = varargin{1};
    se = varargin{2};
    method = varargin{3};
    n = varargin{4};
    
otherwise
    error('Too many input arguments to DILATE');
    
end

methodStrings = ['dilate   '
                 'thicken  '
                 'fatten   '
                 'spatial  '
                 'frequency'];

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

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

⌨️ 快捷键说明

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