Does bwboundaries always returned ordered, clockwise, boundaries?
Show older comments
I am using bwboundaries to get the boundaries of an arbitrary assortment of objects. Because of some line integral calculations I plan to perform on the results, I need to know the handedness of the boundaries returned by bwboundaries. When I test bwboundaries with the following simple code, I find that it faithfully returns boundaries in an ordered clockwise manner, but I can't find any guarantee of this behavior in the documentation. To be clear, I don't care about the overall order of the boundary arraylist returned by bwboundaries, but the ordering and direction of each individual boundary in that list.
a = rand(401) > 0.992;
a = imdilate(a,strel('disk',5));
imtool(a)
b = bwboundaries(a);
figure;hold on;
for j=1:length(b)
bb = b{j};
for i = 1:length(bb)-1
plot(bb(i:i+1,1),bb(i:i+1,2))
pause(.02)
end
end
Can anyone confirm that bwboundaries does indeed returned ordered, clockwise boundaries, and provide a firm proof that this is so? If it is, I strongly recommend to Mathworks that this useful behavior be communicated in the documentation.
Thanks, David
Accepted Answer
More Answers (1)
Amith Kamath
on 25 Oct 2016
David,
Thanks for the detailed question, and sharing the cool demo with us. Here is what the help text for bwboundaries says:
>> help bwboundaries
B = bwboundaries(BW) traces the exterior boundary of objects, as well
as boundaries of holes inside these objects. It also descends into the
outermost objects (parents) and traces their children (objects
completely enclosed by the parents). BW must be a binary image where
nonzero pixels belong to an object and 0-pixels constitute the
background. B is a P-by-1 cell array, where P is the number of objects
and holes. Each cell contains a Q-by-2 matrix, where Q is the number of
boundary pixels for the corresponding region. Each row of these Q-by-2
matrices contains the row and column coordinates of a boundary pixel.
*The coordinates are ordered in a clockwise direction.*
The last line indicates that it is expected behavior that the coordinates are ordered clockwise. Is this the information you were looking for?
Categories
Find more on Object Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!