split label matrix into cells according to their lables including boundary

2 views (last 30 days)
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 Comment
dpb
dpb on 4 Oct 2022
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.

Sign in to comment.

Answers (1)

Sufiyan
Sufiyan on 25 May 2023
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!

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!