当前位置:网站首页>Contour detection based on OpenCV (3)
Contour detection based on OpenCV (3)
2022-07-28 03:17:00 【Ask for it, get it, give it up, lose it】
1. The goal is
This time, , We learn the hierarchical outline of outline , That is, the father son relationship of the outline .
2. principle
In the last few articles on outline , We used OpenCV Several contour related functions provided . But when we use cv.findContours() When the function finds the outline of an image ,
We pass a parameter , Contour retrieval mode . We usually pass cv.RETR_LIST or cv.RETR_TREE, It works well . But what does it mean ?
Again , In the output , We get three arrays , The first is the image , The second is the outline , There is also an output , We name it hierarchy .
But we have never used this level anywhere . So what is this level ?
This is the problem we discuss in this article .
2.1 hierarchy
Usually we use cv.findContours() Function to detect objects in an image , Am I right? ? Sometimes objects are in different positions . But in some cases , Some shapes are inside other shapes .
Like nested data . under these circumstances , We call the external father , Call the inner child . such , There is a certain connection between the contours in the image .
We can specify how a profile is connected to each other , such as , It's a sub contour of other contours , Or the father's profile , wait . The representation of this relationship is called a hierarchy .
In this picture , There are several shapes , I have 0 - 5 Number .2 and 2a Represents the outer and inner contours of the outermost border .
here , outline 0,1,2 Is the outermost . We can say , They are in the hierarchy 0 in , Or to put it simply , They are at the same level of hierarchy .
Next is the outline 2a. It can be thought of as a contour 2 Sub contour of ( perhaps , outline 2 It's the outline 2a Parent outline of ). Let it be in grade 1 in . Same contour 3 yes
outline 2 Sub contour of , Next level . Last , Isocontour 4 and 5 It's the outline 3a Sub contour of , They are in the last level . From the way I number the borders ,
I can say outline 4 It's the outline 3a The first sub outline of ( It can also be an outline 5).
2.2 OpenCV The hierarchy in represents
So every outline has its own information , About what level it is , Who is its sub contour , Who is its father's profile and so on .OpenCV Express it as an array of four values :
[Next, Previous, First_Child, Parent]
Next Represents the next contour at the same level .
Such as the outline in our photos 0. Who is the next outline of the same level ? This is the outline 1. So simply let Next = 1. Similarly , outline 1 Next is the outline 2. therefore Next = 2.
outline 2 Well ? There is no next outline on the same level . So it's simple , hold Next=-1. outline 4 Well ? It is related to the outline 5 At the same level . Its next outline is outline 5, therefore next = 5.
Previous Represents the previous contour on the same level .
Same as above . outline 1 The previous contour is at the same level 0. Empathy , outline 2 The previous contour is at the same level 1. outline 0, There is no previous outline on the same level , So set to -1.
First_Child Represents its first sub outline .
No explanation is needed . For the outline 2,child It's the outline 2a. So it gets the outline 2a The corresponding index value . outline 3a Well ? It has two child.
But we only need the first child. This is the outline 4. So the outline 3a First_Child = 4.
Parent Index representing its parent contour .
It happens to match First_Child contrary . For the outline 4 And the outline 5, The father's outline is the outline 3a. For the outline 3a, The parent contour is the contour 3, And so on .
If there is no child or parent contour , The child contour or parent contour of this contour will be considered -1
image cv.RETR_LIST,cv.RETR_TREE,cv.RETR_CCOMP,cv.RETR_EXTERNAL What does the contour retrieval mode mean ?
3. Contour retrieval mode
3.1 RETR_LIST
This is the simplest of the four symbols ( From the perspective of interpretation ). It just retrieves all the contours , But no parent-child relationship is created . Under this rule , The parent contour and the child contour are equal , They're just outlines . They all belong to the same level .
[Next, Previous, First_Child, Parent] The third and fourth items in are always -1. But apparently ,Next and Previous There will be corresponding values .
Here's what I got , Each line is the level of detail corresponding to the contour . for example , The first line corresponds to the outline 0. The next contour is contour 1. therefore Next = 1. There is no front outline , therefore previous = -1. The other two , Like I said before , yes -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]]])
If you don't use any hierarchical features , This is a good choice .
3.2 RETR_EXTERNAL
Keep only the outermost outline , Any sub contour is discarded
In our image , How many outer contours ? That is to say 0 Outline of hierarchy ? Only 3, That is, contour 0,1,2, Am I right? ? Now try to use this search mode to find the outline . Here's what I got :
# >>> hierarchy
# array([[[ 1, -1, -1, -1],
# [ 2, 0, -1, -1],
# [-1, 1, -1, -1]]])
If you only want to extract the external contour , You can use this logo . It may be useful in some cases .
3.3 RETR_CCOMP
This flag retrieves all contours , And arrange them into a 2 Level hierarchy . That is, the external outline of the object ( That is the boundary ) Be placed in a hierarchy 1 in . And the outline of the hole inside the object
( If any ) Placed in the hierarchy 2 in . If there is anything in it , Its outline is placed again in the hierarchy 1 Middle and so on .
We can use a simple picture to explain . ad locum , I marked the order of the outline in red and green (1 or 2) Indicates the level they belong to . Order and OpenCV The order of detecting the contour is the same .
Consider the first outline , That is, contour 0. This is the level 1. It has two holes , outline 1 and 2, They belong to grade 2. For the outline 0, The next contour at the same level is contour 3.
And not before . The first is child, It's hierarchy 2 The outline of 1. It has no parent node , Because it is at the level 1 in . Its hierarchical array is [3,-1,1,-1]
For the outline 1. It belongs to grade 2. Next on the same level is the outline 2. There is no previous one . No sub contour , But the parent outline is the outline 0. An array is [2,-1,-1,0]
Similarly , outline 2: It is at the level 2 in . In the outline 0 There is no next contour of the same level . So there is no next outline of the same level . The previous one is the outline 1.
No sub contour , The parent contour is the contour 0. So the array (1,1,1,0).
outline 3: At the level 1 Next in is the outline 5. The previous one is the outline 0. Sub contour is contour 4, There is no parent profile . An array is [5,0,4,-1].
outline 4: It's in the outline 3 The next level is 2, It has no brothers . So the next one =-1, the previous =-1, Sub contour =-1, The father's outline is the outline 3. So the array is [-1,-1,-1,3]
# >>> 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]]])

3.4 RETR_TREE
It retrieves all profiles and creates a complete list of family hierarchies . It even tells who is Grandpa , Who is the father , Who is the son , Who is the grandson , Even further …….
for example , I use the picture above , Use cv.RETR_TREE, according to OpenCV The given results reorder the contours and analyze . Again , Red letters indicate the number of outlines , The green letters indicate the rank order .
In outline 0 For example : It is located in the hierarchy 0 in . The next outline is the outline 7. There is no previous outline . Sub contour is contour 1. And no parent contour . An array is [7,-1,1,-1]
In outline 2 For example : It is at the level 1 in . There is no outline on the same level . There is no previous one . The sub outline is c outline 3. The parent contour is the contour 1. An array is [-1,-1,3,1]
# >>> 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]]])

Refer to the directory
https://docs.opencv.org/4.x/d9/d8b/tutorial_py_contours_hierarchy.html
边栏推荐
- QML使用Layout布局时出现大量<Unknown File>: QML QQuickLayoutAttached: Binding loop detected for property循环绑定警告
- Random forest and integration method learning notes
- Raspberry pie development relay control lamp
- [stream] parallel stream and sequential stream
- [email protected] Annotation usage
- 阿里云国际版邮件服务套餐购买流程
- "Introduction to engineering electromagnetic field" after class exercises with answers
- 【Codeforces Round #806 (Div. 4)(A~F)】
- 基于JSP&Servlet实现的众筹平台系统
- C#设置Textbox控件不可编辑
猜你喜欢

【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法

Comprehensive comparative study of image denoising

Data Lake (XVII): Flink and iceberg integrate datastream API operations

ECCV 2022 | open source for generative knowledge distillation of classification, detection and segmentation

综合 案例
![[stream] basic knowledge of stream](/img/33/0bd85f7e029c81d0c20f463ebb972a.png)
[stream] basic knowledge of stream

静态博客搭建工具汇总
[email protected] Annotation usage"/>[email protected] Annotation usage

exness:日本物价上涨收入下降,英镑/日元突破 165

Raspberry pie development relay control lamp
随机推荐
mysql存储过程 使用游标实现两张表数据同步数据
每日刷题巩固知识
静态博客搭建工具汇总
基于OpenCV的轮廓检测(3)
Stop paging with offset and limit. The performance is too poor!
WEB安全基础 - - -命令执行漏洞
[email protected] Annotation usage
ELS displays a random square
Which of the four solutions of distributed session do you think is the best?
【ACwing 1064 小国王】状压dp
Games101 review: ray tracing
Design of the multi live architecture in different places of the king glory mall
MySQL essay
stm32F407-------FPU学习
树莓派开发继电器控制灯
c#——switch case语句
vi命令详解
Note that these regions cannot take the NPDP exam in July
工程电磁场复习基本知识点
The digital twin smart building visualization platform realizes the integration of enterprise and public services in the park