Combine the hole and its parent boundary extracted from bwboundaries
2 次查看(过去 30 天)
显示 更早的评论
I have a binary image and I used the bwboundaries to extracted several boundaries from the image.
However, bwboundaries not only gave me hole boundaries, but also their parents.
For example, the output from this code:
B = bwboundaries(mask, 'holes');
returns multiple cells and each cell correspond to a collection of boundary points for an object.
Cell 1 may be a parent object and Cell 2 may be a hole object reside within the parent object. What I want is to combine these two cells so that the output become a new object and is equivalent to the parent object substracted by the hole.
Right now, if I output the boundary and visualize using R, the result be like:
You can see the holes will be overlapped by their parent polygons.
Of course I can do it manually but I have hundreds of images to process and it is very time consuming to manually process them all.
Is there any computational way for batch processing? Thank you.
0 个评论
采纳的回答
Uday Pradhan
2021-5-24
编辑:Uday Pradhan
2021-5-24
Hello,
It is my understanding that you would like to remove the child-hole objects from the output of "bwboundaries". For this, you can utilise the below mentioned script which I adapted from the documentation page of this function (link):
[B,L,N,A] = bwboundaries(BW);
figure; imshow(BW); hold on;
% Loop through object boundaries
for k = 1:N
% Boundary k is the parent of a hole if the k-th column
% of the adjacency matrix A contains a non-zero element
if (nnz(A(:,k)) > 0)
% Loop through the children of boundary k and remove them
for l = find(A(:,k))'
B{l} = {};
end
end
end
Boundaries before removal of holes:
After removal of child-holes:
NOTE: Using bwboundaries with 'noholes' option also gives the same result as above. You might try that as well. I hope this helps!
3 个评论
Uday Pradhan
2021-5-24
Hello, if I understand correctly, you can take the parent cell array and find its children through the same technique given in the code above and then combine the cell arrays. See: https://www.mathworks.com/help/matlab/matlab_prog/combine-cell-arrays.html
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!