function bw2 = removeLargeArea(varargin)
%BWAREAOPEN Remove small objects from binary image.
% BW2 = BWAREAOPEN(BW,P) removes from a binary image all connected
% components (objects) that have fewer than P pixels, producing another
% binary image BW2. This operation is known as an area opening. The
% default connectivity is 8 for two dimensions, 26 for three dimensions,
% and CONNDEF(NDIMS(BW),'maximal') for higher dimensions.
%
% BW2 = BWAREAOPEN(BW,P,CONN) specifies the desired connectivity. CONN
% may have the following scalar values:
%
% 4 two-dimensional four-connected neighborhood
% 8 two-dimensional eight-connected neighborhood
% 6 three-dimensional six-connected neighborhood
% 18 three-dimensional 18-connected neighborhood
% 26 three-dimensional 26-connected neighborhood
%
% Connectivity may be defined in a more general way for any dimension by
% using for CONN a 3-by-3-by- ... -by-3 matrix of 0s and 1s. The
% 1-valued elements define neighborhood locations relative to the center
% element of CONN. CONN must be symmetric about its center element.
%
% Class Support
% -------------
% BW can be a logical or numeric array of any dimension, and it must be
% nonsparse.
%
% BW2 is logical.
%
% Example
% -------
% Remove all objects in the image text.png containing fewer than 50
% pixels.
%
% BW = imread('text.png');
% BW2 = bwareaopen(BW,50);
% imshow(BW);
% figure, imshow(BW2)
%
% See also BWCONNCOMP, CONNDEF, REGIONPROPS.
% Copyright 1993-2011 The MathWorks, Inc.
% $Revision: 1.10.4.9 $ $Date: 2011/11/09 16:48:52 $
% Input/output specs
% ------------------
% BW: N-D real full matrix
% any numeric class
% sparse not allowed
% anything that's not logical is converted first using
% bw = BW ~= 0
% Empty ok
% Inf's ok, treated as 1
% NaN's ok, treated as 1
%
% P: double scalar
% nonnegative integer
%
% CONN: connectivity
%
% BW2: logical, same size as BW
% contains only 0s and 1s.
[bw,p,conn] = parse_inputs(varargin{:});
CC = bwconncomp(bw,conn);
area = cellfun(@numel, CC.PixelIdxList);
idxToKeep = CC.PixelIdxList(area <= p);
idxToKeep = vertcat(idxToKeep{:});
bw2 = false(size(bw));
bw2(idxToKeep) = true;
%%%
%%% parse_inputs
%%%
function [bw,p,conn] = parse_inputs(varargin)
narginchk(2,3)
bw = varargin{1};
validateattributes(bw,{'numeric' 'logical'},{'nonsparse'},mfilename,'BW',1);
if ~islogical(bw)
bw = bw ~= 0;
end
p = varargin{2};
validateattributes(p,{'double'},{'scalar' 'integer' 'nonnegative'},...
mfilename,'P',2);
if (nargin >= 3)
conn = varargin{3};
else
conn = conndef(ndims(bw),'maximal');
end
iptcheckconn(conn,mfilename,'CONN',3)
水中望月932
- 粉丝: 670
- 资源: 45
最新资源
- Win11右键菜单功能强大Nilesoft Shell1.9.18.mp4
- WechatBakTool(聊天备份工具) v0.9.7.60.mp4
- Windows 10 2021 ltsc 适当精简优化版.mp4
- 800kV-VSC-HVDC直流输电仿真模型(Matlab) 流器拓扑:VSC两电平流器 电压等级:直流800kV,交流500kV 控制结构:逆变侧定有功控制与电流内环PI+前馈解耦,整流侧定直流电压
- Windows 11、10 轻松设置 1.10 正式版.mp4
- WPS Office 2023专业增强版_v12.8.2.18913.mp4
- WPS年终回馈抽超级会员月卡.mp4
- xb21cn精简office最新office2024绿色精简版.mp4
- wxid批量转微信号工具v3.5.mp4
- x64 dbg 调试工具 v20241201绿色增强版.mp4
- XRecorder 安卓屏幕录制工具.mp4
- 广州大学人工智能原理实验(三)代码包
- 爱卡聚合同款彩虹聚合登录首页简约模板.mp4
- 爱酷音乐好用的听音乐软件易语言编写.mp4
- 安兔兔AI大模型评测v1.0.0.1097评估工具.mp4
- 安卓Breezy Weather(微风天气}v5.3.1软件.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈