[opencv] contour layering ()

4_9_5_Contour layering - OpenCV Chinese official documentation

content

theory

Hierarchical representation in OpenCV

Contour search mode

1. RETR_LIST

2. RETR_EXTERNAL

3. RETR_CCOMP

4. RETR_TREE


1. RETR_LIST

This is the simplest of the four flags (from an interpretation standpoint). It just retrieves all contours, but doesn't create any parent-child relationships. Under this rule, parent and child contours are equal, they are just contours . They all belong to the same level.

2. RETR_EXTERNAL If this flag is used, it only returns the extreme external flag. All child silhouettes are left behind. We can say that according to this rule, only the eldest son of each family gets attention. It doesn't care about the rest of the family :) .

3. RETR_CCOMP This flag retrieves all contours and arranges them into a 2-level hierarchy. The outer contour of the object (ie the boundary of the object) is placed in hierarchy-1. The outline of the hole inside the object (if any) is placed in hierarchy-2.

4. RETR_TREE: It retrieves all contours and creates a complete family hierarchy list. It even tells, who is the grandpa, father, son, grandson and more... :).

 

theory

Several functions related to contours provided by OpenCV were discussed earlier. But when we use **cv.findcontour**() function to find the contour in the image, we have passed a parameter, the contour retrieval mode . We usually pass **cv.RETR_LIST ** or **cv.RETR_TREE ** , which works fine. But what exactly does this mean?

In the output, we got three arrays,

  1. The first is the image,
  2. The second is the outline,
  3. There is also an output we named **hierarchy** (check the code in the previous article).
    But we've never used this hierarchy anywhere. So what is this hierarchy? What is it used for? How does it relate to the aforementioned function parameters?

That's what we're going to discuss in this article.

What is the hierarchy?

  • Usually we use **cv.findcontour**() function to detect objects in the image, sometimes the objects are in different positions.
  • But in some cases some shapes are inside other shapes . Just like nested graphs. In this case, call the outer one **parent class** and the inner one **subclass** . In this way, the contours in the image have a certain relationship with each other.
  • You can specify how a contour is connected to each other, for example, is it a child contour of another contour, a parent contour, etc. The representation of this relationship is called ** hierarchy **.

Below is an example: 

In this picture, there are some shapes that I have numbered from **0-5**. *2* and *2a* represent the outer and inner contours of the outermost box .

  • Contours 0,1,2 are **outer or outermost**. We can say that they are in **level-0**, or simply, they are in **the same level**.
  • Next is **contour-2a**. It can be considered a child of contour-2 (or conversely, contour-2 is a parent of contour-2a). Let's say it's in **level-1**.
  • Similarly, contour-3 is a child of contour-2, which is in the next hierarchy.
  • Finally, contours 4,5 are children of contour-3a, they are in the last level. From the numbering of the boxes, I think contour-4 is the first child of contour-3a (it could also be contour-5).

I mention these to understand some terms like ** same level **, outer contour , child contour , parent contour , **first child contour**, etc. Now let's get into OpenCV.

Hierarchical representation in OpenCV

So each profile has it's own information about what level it is, who is its child, who is its parent, etc.

OpenCV represents it as an array with four values:[Next, Previous, First_Child, Parent]

"Next represents the next contour at the same level."

For example, take contour-0 in our image. Who is the next contour at the same level? This is contour-1. simply order Next = 1. Similarly, Contour-1 is also contour-2. So. Next = 2_ What about contour-2? There is no next contour line on the same horizontal line. Simply, let Next = -1. What about contour-4? It's on the same level as contour-5. Its next contour is contour-5, so next = 5.

" Previous represents a previous contour on the same level."

the same as above. The contour before contour-1 is contour-0 at the same level. Similarly, contour-2 is also contour-1. For contour-0, there is no preceding item, so it is set to -1.

"First_Child represents its first child contour."

No explanation is necessary. For contour-2, the child is contour-2a. Thus, the index value corresponding to contour-2a is obtained. What about contour-3a? It has two children. But we only focus on the first child. It is contour-4. Then First_Child = 4 for contour-3a.

"Parent represents the index of its parent contour."

It is the opposite of **First_Child**. For contour-4 and contour-5, the parent contour is contour-3a. For contour 3a, it is contour-3, and so on.

Note that  this field is treated as -1 if there are no child or parent elements

Now that we understand the hierarchical styles used in OpenCV, we can check the contour retrieval mode in OpenCV with the help of the same image given above. The meaning of some flags such as  cv.RETR_LISTcv.RETR_TREE , cv.RETR_CCOMP , **cv.RETR_EXTERNAL** and so on.

Contour search mode

1. RETR_LIST

This is the simplest of the four flags (from an interpretation standpoint).

  • It just retrieves all contours, but doesn't create any parent-child relationships.
  • Under this rule, parent and child contours are equal, they are just contours . They all belong to the same level.

Here, the 3rd and 4th items are always -1. But obviously, the next item and the previous item have corresponding values. Just check it yourself.

Below is the result I got, each row is the level of detail for the corresponding contour. For example, the first row corresponds to contour 0. The next contour is contour 1. So. Next = 1_ There is no previous contour, so Previous=-1. The remaining two, as mentioned, are -1.

>>> hierarchy
array([[[ 1, -1, -1, -1],
        [ 2,  0, -1, -1],
        [ 3,  1, -1, -1],
        [ 4,  2, -1, -1],
        [ 5,  3, -1, -1],
        [ 6,  4, -1, -1],
        [ 7,  5, -1, -1],
        [-1,  6, -1, -1]]])

This is the best option to use in your code if you are not using any hierarchy features.

2. RETR_EXTERNAL

If this flag is used, it only returns the extreme outer flag. All child silhouettes are left behind. We can say that according to this rule, only the eldest son of each family gets attention. It doesn't care about the rest of the family :) .

So in our image, how many extreme outer contours are there? At level 0? There are 3, i.e. the contour is 0 1 2, right? Now try to find the contour with this flag. Here, give each element the same value as above. and compared with the above results. Here is what I get:

>>> hierarchy
array([[[ 1, -1, -1, -1],
        [ 2,  0, -1, -1],
        [-1,  1, -1, -1]]])
You can use this flag if you only want to extract outer contours. It might be useful in some situations.

3. RETR_CCOMP

This flag retrieves all contours and arranges them into a 2-level hierarchy.

  • The outer contour of the object (ie the boundary of the object) is placed in hierarchy-1.
  • The outline of the hole inside the object (if any) is placed in hierarchy-2.

If there is any object in it, its outline is only repositioned in hierarchy 1. and its vulnerabilities in Tier 2 and so on.

Just consider a "white zero" image on a black background. The outer circle of zero belongs to the first level, and the inner circle of zero belongs to the second level.

We can explain it with a simple image. Here I marked the order of the contours and the level they belong to in red, and in green (1 or 2), in the same order as OpenCV detects the contours.

Consider the first contour, which is contour-0. This is hierarchy-1. It has two holes, Contours 1 and 2, which belong to the second level. So for contour-0, the next contour at the same level is contour-3. Neither did previous. In hierarchy-2, its first child is contour-1. It has no parent class because it is in hierarchy-1. So its hierarchical array is[3,-1,1,-1]

Now contour-1. It's in tier-2. The next one in the same hierarchy (under the parent of contour-1) is contour-2. There is no previous. No child, but parentcontour-0. So the array is[2,-1,-1,0]

Similar contour-2: it's in hierarchy-2. Under contour-0, there is no next contour in the same hierarchy. So no Next. previousis contour-1. No child, parentit's contour0. So the array is[-1,1,-1,0]

contour-3: Next to level-1 is contour-5. It used to be contour-0. childis contour4, no parent. So the array is[5,0,4,-1]

contour-4: It is in hierarchy 2 under contour-3, it has no siblings. No next, no previous, no child, parentit's contour-3. So the array is[-1,-1,-1,3]

You can add the rest. Here is the final answer I got:

>>> hierarchy
array([[[ 3, -1,  1, -1],
        [ 2, -1, -1,  0],
        [-1,  1, -1,  0],
        [ 5,  0,  4, -1],
        [-1, -1, -1,  3],
        [ 7,  3,  6, -1],
        [-1, -1, -1,  5],
        [ 8,  5, -1, -1],
        [-1,  7, -1, -1]]])

4. RETR_TREE

It retrieves all contours and creates a complete family hierarchy list. It even tells, who is the grandpa, father, son, grandson and more... :).

For example, I took the picture above and rewrote the code for cv. RETR_TREE, reorder contours and analyze them according to the results given by OpenCV. Again, the red letters indicate the number of contours, and the green letters indicate the hierarchy order.

Take contour-0: it is hierarchy-0in. The contour of the same hierarchy nextis contour-7. no previoussilhouette. childis contour-1, no parent. So the array is[7,-1,1,-1]

Take for contour-2example: it's in hierarchy-1. No contours are at the same level. previousNo. _ childYes contour-3. Parents are contour-1. So the array is[-1,-1,3,1]

For the rest, try it yourself. Here is the full answer:

>>> hierarchy
array([[[ 7, -1,  1, -1],
        [-1, -1,  2,  0],
        [-1, -1,  3,  1],
        [-1, -1,  4,  2],
        [-1, -1,  5,  3],
        [ 6, -1, -1,  4],
        [-1,  5, -1,  4],
        [ 8,  0, -1, -1],
        [-1,  7, -1, -1]]])

Guess you like

Origin blog.csdn.net/dujuancao11/article/details/122431371