split label matrix into cells according to their lables including boundary
1 次查看(过去 30 天)
显示 更早的评论
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
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
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!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!