How can I sample the parametric boundary of an object by N points resulting in equal arc-length parts?
The parametric boundary of an object can be extract in Matlab by using the bwtraceboundary function. It returns a Q-by-2 matrix B, where Q is the number of boundary pixels for the object and the first and second columns stores the row and column coordinates of the boundary pixels respectively.
What I want to do is to sample this boundary of Q elements by N points that divide the original boundary in segments of equal arch length.
A straightfoward solution that I thought consists in computing the length L of the boundary by summing the distance o开发者_Go百科f all two consecutive boundary pixels. Those distances are either 1 or sqrt(2). Then I divide L by N to find the desired length of the arcs. Finally, I iterate over the boundary again summing the distance of all two consecutive boundary pixels. When the sum is greater or equal the desired arc-length, the current boundary pixel is chosen as one of the N that will compose the sampled boundary.
Is that a good solution? Is there a more efficient/simple solution?
Over the years, I have seen this question a seemingly vast number of times. So I wrote a little tool that will do exactly that. Sample a piecewise linear or even a curvilinear (spline) arc in a general number of dimensions so that the successive points are at a uniform or specified distance along that arc.
In the case of the use of merely piecewise linear arcs, this is rather easy. You sum up the total arc length of the curve, then do an interpolation in arc length, but since that is known to be piecewise linear, it only requires linear interpolation along that length as a function of the cumulative arc length.
In the case of a curved arc, it is most easily done as the solution of a system of ordinary differential equations, watching for events along the way. ODE45 does this nicely.
You can use interparc, as found on the MATLAB Central File Exchange to do this for you, or if you wish to learn to do it yourself for the simple piecewise linear case, read through the first part of the code where I do the piecewise linear arc length interpolation. A nice thing is the linear case is done in a fully vectorized form, so no explicit loops are necessary.
精彩评论