split label matrix into cells according to their lables including boundary

2 ビュー (過去 30 日間)
Katherine Zheng
Katherine Zheng 2022 年 10 月 3 日
回答済み: Sufiyan 2023 年 5 月 25 日
Hi, I have a label matrix and want to split the area into individual matrix for post processing. I need the boundary accosiated with the label as well. I have tried label2idx but it does not contain the boundary (labelled as 0). Is there any easy way to do this?
  1 件のコメント
dpb
dpb 2022 年 10 月 4 日
Attach example data of what you are starting with and what you would want the result to be; I don't know what a "label matrix" is by that description.

サインインしてコメントする。

回答 (1 件)

Sufiyan
Sufiyan 2023 年 5 月 25 日
You can follow the procedure shown in the below code to split the area in to individual matrix.
% sample label matrix
label_matrix = zeros(50,50);
label_matrix(10:20,10:20) = 1;
label_matrix(30:40,10:20) = 2;
label_matrix(10:20,30:40) = 3;
label_matrix(30:40,30:40) = 4;
% get the boundaries of the labeled regions
boundaries = bwboundaries(label_matrix, 'noholes');
% extract the individual regions and store them in a cell array
regions = cell(length(boundaries), 1);
for i = 1:length(boundaries)
region = boundaries{i};
x_min = min(region(:,2));
x_max = max(region(:,2));
y_min = min(region(:,1));
y_max = max(region(:,1));
region_matrix = label_matrix(y_min:y_max, x_min:x_max);
regions{i} = region_matrix;
end
Hope this helps!

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by