如何在 MATLAB 中为大型结构制作封闭组件标签?

How to make a closed component labeling for large structures in MATLAB?

bwlabel的帮助下,我知道如何为小型结构制作封闭组件标签。但是,我现在有以下图像:

并且在此图像上使用 bwlabel 只会产生两个 classes,边缘 - 以及它周围的一切。

我想知道在 matlab 中是否有一个简单的解决方案可以将 "circle" 的内部部分作为一个 class 而将外部部分作为另一个 class?边界可能是三分之一 class。

目前我的代码是用于测试 bwlabel 的 onyl

i = imread('apple.jpg')
labels = bwlabel(i)

应该更快更容易


简单:只需制作两次标签,一次填充。

% load
I=rgb2gray(imread('https://i.stack.imgur.com/nnJUn.png'));
I=I(:,1:end-2); %some artifacts in the corners of the SO image

labels = bwlabel(I);
filled=imfill(I,'holes'); % fill
labels2= bwlabel(filled);
labels=labels+labels2;

Matlab 的 bwlabel uses 8-connect 连接相邻像素。由于您的边界非常细,因此对角线连接会连接内部和外部像素,从而产生单个标签。
但是,如果您使用 4-connect 连接

 labels = bwlabel(~i, 4);

你应该得到你想要的输出。


顺便说一句,
最好是not to use i as a variable name in Matlab.