This must be simple for C++ developers using OpenCV directly. However what I\'m using is Emgu (an OpenCV wrapper for .NET) and in the latest version we have the method
To obtain the hierarchy of the contours, you must first pass a Mat
object to the function:
Mat hierarchy = new Mat() ;
CvInvoke.FindContours(inputImage, outputResult, hierarchy, RetrType.Tree,
ChainApproxMethod.ChainApproxSimple);
Then you can use the hierarchy
object as follows (see here for more details in Python OpenCV) :
hierarchy
will be a Mat
object of size 1 x size of outputResult
x 4.
So for the contour with index i
:
hierachy[0,i,0]
is the index of the next contour at the same hierarchy level (with the same parent) or - 1 if it doesn't existhierachy[0,i,1]
is the index of the previous contour at the same hierarchy level or - 1 if it doesn't existhierachy[0,i,2]
is the index of the child of contour i
or - 1 if it doesn't existhierachy[0,i,3]
is the index of the parent of contour i
or - 1 if it doesn't existThat's how you use the hierarchy object.
The contours themselves are accessed through the outputResult
object by using their indices.