Question

New to OpenCV here. I'm trying to understand what a hierarchy vector is supposed to contain. I understand that for object tracking and when using the findCountours function, it is common to use vector, but I would like to understand what this means. Thanks in advance for the help!

Was it helpful?

Solution

A contour is a closed edge around an area of an image. This contour could contain contours so we need a way to store this hierarchy. The hierarchy vector contains all of the information to explain how contours are nested in each other.

From the OpenCV documentation it is a

Optional output vector, containing information about the image topology. It has as many elements as the number of contours. For each i-th contour contours[i] , the elements hierarchy[i][0] , hiearchy[i][1] , hiearchy[i][2] , and hiearchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.

You can think of this as a doubly linked list, but each item in the list points could possibly point to a parent and/or child. We can use the next and previous indices to find all of the contours that have the same parent. Each contour will point to a child linked list if they contain a child contour. A negative value is the same as a NULL pointer for a traditional linked list.

An example:
a
|
b,c,d,e,f
|     |
g     h,i

a points to b as one child and from b we know that b, c, d, e, and f are all contained at the same level in a. b also has a child contour g. e has two children as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top