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

matlab 实现二值图像孔洞填充函数imfill()代码如下:function[I2,locations]=imfill(varargin)[I,locations,conn,do_fillholes]=parse_inputs(varargin{:});ifdo_fillholesifislogical(I)mask=uint8(I);elsemask=I;endmask=padarray(mask,ones(1,ndims(mask)),

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

代码如下:

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);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/197277.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号