matlab 实现二值图像孔洞填充函数imfill()

本文介绍了一个用于图像处理的填充算法实现细节,该算法能够处理不同的输入情况,包括逻辑型和数值型图像,支持交互式填充及指定位置填充,并提供多种连接方式。文章详细解释了如何解析输入参数并根据不同情况进行处理。
摘要由CSDN通过智能技术生成

代码如下:

function [I2,locations] = imfill(varargin)


[I,locations,conn,do_fillholes] = parse_inputs(varargin{:});

if do_fillholes
    if islogical(I)
        mask = uint8(I);
    else
        mask = I;
    end
    mask = padarray(mask, ones(1,ndims(mask)), -Inf, 'both');
    mask = imcomplement(mask);
    marker = mask;
    
    idx = cell(1,ndims(I));
    for k = 1:ndims(I)
        idx{k} = 2:(size(marker,k) - 1);
    end
    marker(idx{:}) = -Inf;
    
    I2 = imreconstruct(marker, mask, conn);
    I2 = imcomplement(I2);
    I2 = I2(idx{:});

    if islogical(I)
        I2 = logical(I2);
    end

else    
    mask = imcomplement(I);
    marker = false(size(mask));
    marker(locations) = mask(locations);
    marker = imreconstruct(marker, mask, conn);
    I2 = I | marker;
end

%%%
%%% Subfunction ParseInputs
%%%
function [IM,locations,conn,do_fillholes] = parse_inputs(varargin)
    
narginchk(1,3);

IM = varargin{1};
validateattributes(IM, {'numeric' 'logical'}, {'nonsparse' 'real','nonnan'}, ...
              mfilename, 'I1 or BW1', 1);

do_interactive = false;
do_fillholes = false;

conn = conndef(ndims(IM),'minimal');
do_conn_check = false;

locations = [];
do_location_check = false;

switch nargin
  case 1
    if islogical(IM)
        % IMFILL(BW1)
        do_interactive = true;
    else
        % IMFILL(I1)
        do_fillholes = true;
    end
    
  case 2
    if islogical(IM)
        if ischar(varargin{2})
            % IMFILL(BW1, 'holes')
            validatestring(varargin{2}, {'holes'}, mfilename, 'OPTION', 2);
            do_fillholes = true;
            
        else
            % IMFILL(BW1, LOCATIONS)
            locations = varargin{2};
            do_location_check = true;
        end
        
    else
        if ischar(varargin{2})
            % IMFILL(I1, 'holes')
            validatestring(varargin{2}, {'holes'}, mfilename, 'OPTION', 2);
            do_fillholes = true;
            
        else
            % IMFILL(I1, CONN)
            conn = varargin{2};
            do_conn_check = true;
            conn_position = 2;
            do_fillholes = true;
        end
        
    end
    
  case 3
    if islogical(IM)
        if ischar(varargin{3})
            % IMFILL(BW1,CONN,'holes')
            validatestring(varargin{3}, {'holes'}, mfilename, 'OPTION', 3);
            do_fillholes = true;
            conn = varargin{2};
            do_conn_check = true;
            conn_position = 2;
            
        else
            if isequal(varargin{2}, 0)
                % IMFILL(BW1,0,CONN)
                do_interactive = true;
                conn = varargin{3};
                do_conn_check = true;
                conn_position = 2;
                
            else
                % IMFILL(BW1,LOCATIONS,CONN)
                locations = varargin{2};
                do_location_check = true;
                conn = varargin{3};
                do_conn_check = true;
                conn_position = 3;
            end
            
        end
        
    else
        % IMFILL(I1,CONN,'holes')
        validatestring(varargin{3}, {'holes'}, mfilename, 'OPTION', 3);
        do_fillholes = true;
        conn = varargin{2};
        do_conn_check = true;
        conn_position = 2;
    end
end

if do_conn_check
    iptcheckconn(conn, mfilename, 'CONN', conn_position);
end

if do_location_check
    locations = check_locations(locations, size(IM));    
elseif do_interactive
    locations = get_locations_interactively(IM);
end

% Convert to linear indices if necessary.
if ~do_fillholes && (size(locations,2) ~= 1)
    idx = cell(1,ndims(IM));
    for k = 1:ndims(IM)
        idx{k} = locations(:,k);
    end
    locations = sub2ind(size(IM), idx{:});
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function locations = check_locations(locations, image_size)
%   Checks validity of LOCATIONS.  Converts LOCATIONS to linear index
%   form.  Warns if any locations are out of range.

validateattributes(locations, {'double'}, {'real' 'positive' 'integer' '2d'}, ...
              mfilename, 'LOCATIONS', 2);

num_dims = length(image_size);
if (size(locations,2) ~= 1) && (size(locations,2) ~= num_dims)
    error(message('images:imfill:badLocationSize', iptnum2ordinal( 2 )));
end

if size(locations,2) == 1
    bad_pix = (locations < 1) | (locations > prod(image_size));
else
    bad_pix = zeros(size(locations,1),1);
    for k = 1:num_dims
        bad_pix = bad_pix | ((locations(:,k) < 1) | ...
                             (locations(:,k) > image_size(k)));
    end
end
    
if any(bad_pix)
    warning(message('images:imfill:outOfRange'));
    locations(bad_pix,:) = [];
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function locations = get_locations_interactively(BW)
%   Display image and give user opportunity to select locations with the mouse.

if ~ismatrix(BW)
    error(message('images:imfill:badInteractiveDimension'))
end

if isempty(BW)
    error(message('images:imfill:emptyImage'))
end

imshow(BW)
[xi,yi] = getpts;
c = round(axes2pix(size(BW,2), [1 size(BW,2)], xi));
r = round(axes2pix(size(BW,1), [1 size(BW,1)], yi));
locations = sub2ind(size(BW),r,c);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值