Navigate through hierarchy of contours found by FindContours method?

雨燕双飞 提交于 2019-11-30 21:44:17

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!