当前位置:网站首页>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
边栏推荐
- Uniapp——拨打电话、发送短信
- 对象数组转成strin再进行,隔开的字符串,包括赛选某个字段的子,或者求和,
- Data Lake: database data migration tool sqoop
- “讳疾忌医”的开源走不远
- Full of dry goods, hurry in!!! Easy to master functions in C language
- MySQL essay
- ELS keyboard information
- What kind of job is social phobia suitable for? Can you do we media?
- 数据湖:各模块组件
- Alibaba cloud international email service package purchase process
猜你喜欢

CAD创建组却没有组合在一起?

Exness: Japanese prices rose and incomes fell, with the pound / yen breaking 165

Stm32f407 ------- FPU learning

嵌入式分享合集22

Development and design logic of rtsp/onvif protocol easynvr video platform one click upgrade scheme

图像去噪综合比较研究

Data Lake: flume, a massive log collection engine
![[stream] parallel stream and sequential stream](/img/e1/b8728962c14df56241aa6973c0c706.png)
[stream] parallel stream and sequential stream

Stm32f407 ------- DSP learning

R 笔记 MICE
随机推荐
My approval of OA project (meeting inquiry & meeting signature)
Engineering Geology Practice - engineering geology problem set
NPDP candidates! The exam requirements for July 31 are here!
Games101 review: ray tracing
基于OpenCV的轮廓检测(3)
Kubernetes-----介绍
Comprehensive case
【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法
style=“width: ___“ VS width=“___“
"Introduction to engineering electromagnetic field" after class exercises with answers
阿里云国际版邮件服务套餐购买流程
从硬件编程到软件平台的ci/cd
Qt官方示例:Fridge Magnets Example(冰箱贴)
【ACwing 1064 小国王】状压dp
Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
并发编程面试题总结
静态博客搭建工具汇总
Niuke-top101-bm340
关于权重衰退和丢弃法
Is the securities account given by qiniu safe? Can qiniu open an account and buy funds