opencv - Do contours returned by cv::findContours have a consistent orientation? -
i using opencv's cv::findcontours function extract contours in binary image, in particular, i'm extracting hierarchy of contours (using cv_retr_ccomp flag). @ point in further processing of contours need rely on consistent vertex orientation of these contours (i.e. counter-clockwise vs. clockwise).
of course can determine orientation myself using sign of contour's area (as computed cv::contourarea(..., true)), wonder if necessary (besides that, won't work contours area of 0, i.e. thin lines in source image) or if cv::findcontours guarantees consistent orientation generated contours. did check few of generated contours , cv::contourarea indeed seem return negative values outer contours , positive values inner contours. however, couldn't find actual guarantee effect in opencv documentation.
so, is specifically guaranteed contours returned cv::findcontours have consistent orientation? documented anywhere? or vary version (mine 2.4.5 matter)? actual paper on algorithm referenced in documentation this? or maybe little more insight actual implementation of opencv can little more interface documentation can?
the contours returned cv:findcontours should have consistent orientation. outer contours should oriented counter-clockwise, inner contours clockwise. follows directly algorithm described in appendix 1 of suzuki's , abe's paper.
the image scanned line line top left bottom right. when pixel belonging border found, border followed looking @ neighbours of first pixel in counter-clockwise order (see step 3.3 in algorithm), until non-background pixel found. added contour , search continues pixel.
the important thing in first iteration neighbour first looked @ depends on whether inner or outer border. in case of outer border right-hand neighbour visited first; in case of inner border left-hand neighbour. in next search step search starts last pixel visited.
due scanning happing top left bottom right, on detection of outer border assured neighbouring pixels left , top of border pixel background pixels. inner border, opposite, neighbours left , top non-background pixels.
in combination different starting positions visiting neighbouring pixels results in predictable orientations of contours.
this algorithm implemented in icvfetchcontour function used internally cv:findcontour. there clear pixel added contour polygon in order in visited.
as documentation cv::findcontours says implemented algorithm suzuki et al. , in paper direction , order visiting pixels explicitely defined, think 1 can assume orientation kind of guaranteed.
Comments
Post a Comment