MATLAB Answers

Does bwboundaries always returned ordered, clockwise, boundaries?

12 views (last 30 days)
David
David on 25 Oct 2016
Commented: David on 27 Oct 2016
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

Chaya N
Chaya N on 25 Oct 2016
Edited: Chaya N on 25 Oct 2016
Hello David, Please look up " help bwboundaries " at the command line if you would like an explicit mention of this particular property (specifically, the last line of the first paragraph!)
The confirmation that you seek is also provided as a reference in the 'Algorithms' subsection of the documentation here, but not in as many words.
A part of this reference goes : "This function is based on the boundaries function presented in the first edition of Digital Image Processing Using MATLAB..."
The information in the book says that the boundaries are traced in the clockwise direction by default. I suppose you could verify this firmly enough by looking up page 434 (Chapter 11, Subsection 11.1.3 - Some Basic Utility M-Functions) in the book!
  3 Comments
David
David on 27 Oct 2016
No haha you are totally right, I deserved that :). I even thought of trying to look at the references before posting here but was too lazy I suppose.

Sign in to comment.

More Answers (1)

Amith Kamath
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?

Community Treasure Hunt

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

Start Hunting!

Translated by