Navigate through hierarchy of contours found by FindContours method?

后端 未结 1 1116
南笙
南笙 2021-01-13 06:33

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

相关标签:
1条回答
  • 2021-01-13 07:19

    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 exist
    • hierachy[0,i,1] is the index of the previous contour at the same hierarchy level or - 1 if it doesn't exist
    • hierachy[0,i,2] is the index of the child of contour i or - 1 if it doesn't exist
    • hierachy[0,i,3] is the index of the parent of contour i or - 1 if it doesn't exist

    That's how you use the hierarchy object.

    The contours themselves are accessed through the outputResult object by using their indices.

    0 讨论(0)
提交回复
热议问题