Select 图像中第二大区域

Select second largest area in image

我用bwareaopen去除小物件。有删除大对象的功能吗?我正在尝试调整 bwareaopen,但到目前为止还没有成功。谢谢

参考:link bwareaopen 的帮助。

我找到了解决这个问题的简单方法 here:

"To keep only objects between, say, 30 pixels and 50 pixels in area, you can use the BWAREAOPEN command, like this:"

LB = 30;
UB = 50;
Iout = xor(bwareaopen(I,LB),  bwareaopen(I,UB));

如果你不想使用 bwareaopen 的另一种方法是使用 regionprops,特别是使用 AreaPixelIdxList 属性,过滤掉那些元素不符合你想要的区域范围,然后使用剩余的元素并创建一个新的蒙版。 Area 捕获每个形状的总面积,而 PixelIdxList 捕获图像内属于每个形状的位置的列主要线性索引。您将使用 Area 属性执行过滤,同时您将使用 PixelIdxList 属性创建新的输出图像并将这些位置设置为 true 在所需区域范围内:

% Specify lower and upper bounds
LB = 30;
UB = 50;

% Run regionprops
s = regionprops(I, 'Area', 'PixelIdxList');

% Get all of the areas for each shape
areas = [s.Area];

% Remove elements from output of regionprops
% that are not within the range
s = s(areas >= LB & areas <= UB);

% Get the column-major locations of the shapes
% that have passed the check
idx = {s.PixelIdxList};
idx = cat(1, idx{:});

% Create an output image with the passed shapes
Iout = false(size(I));
Iout(idx) = true;