代码如下:
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);