官方文档:

官方文档对hierachy的解释
简单的使用(demo1):

def draw():image=np.zeros((300,300,1),dtype=np.uint8)cv2.rectangle(image,(10,10),(100,100),(255,255,255),10)cv2.rectangle(image, (100,10), (200,200), (255, 255, 255), 10)cv2.circle(image,(100,100),50,(255,255,255),10)mask=image.copy()mask=cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)image,contours,hierachy=cv2.findContours(image,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)cv2.drawContours(mask,contours,-1,(0,0,255),2)cv2.imshow('mask',mask)cv2.waitKey(0)
draw()

结果

关于findContours

 image,contours,hierachy=cv2.findContours(image,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)

第一个参数是寻找轮廓的图像;

第二个参数表示轮廓的检索模式,有四种(本文介绍的都是新的cv2接口):
cv2.RETR_EXTERNAL表示只检测外轮廓
cv2.RETR_LIST检测的轮廓不建立等级关系
cv2.RETR_CCOMP建立两个等级的轮廓,上面的一层为外边界,里面的一层为内孔的边界信息。如果内孔内还有一个连通物体,这个物体的边界也在顶层。
cv2.RETR_TREE建立一个等级树结构的轮廓。

第三个参数method为轮廓的近似办法
cv2.CHAIN_APPROX_NONE存储所有的轮廓点,相邻的两个点的像素位置差不超过1,即max(abs(x1-x2),abs(y2-y1))==1
cv2.CHAIN_APPROX_SIMPLE压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息cv2.CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似算法
返回值:
cv2.findContours()函数返回三个值,一个是图像,一个是轮廓本身,还有一个是每条轮廓对应的属性。

关于drawContours()

cv2.drawContours(mask,contours,-1,(0,0,255),2)#参数:图像、轮廓、轮廓序号(负数就画出全部轮廓)、颜色、粗细

还有一些参数,但是目前用这些参数就够了

关于hierachy

来段官方解释
So each contour has its own information regarding what hierarchy it is, who is its child, who is its parent etc. OpenCV represents it as an array of four values : [Next, Previous, First_Child, Parent]

“Next denotes next contour at the same hierarchical level.”

For eg, take contour-0 in our picture. Who is next contour in its same level ? It is contour-1. So simply put Next = 1. Similarly for Contour-1, next is contour-2. So Next = 2.

What about contour-2? There is no next contour in the same level. So simply, put Next = -1. What about contour-4? It is in same level with contour-5. So its next contour is contour-5, so Next = 5.

“Previous denotes previous contour at the same hierarchical level.”

It is same as above. Previous contour of contour-1 is contour-0 in the same level. Similarly for contour-2, it is contour-1. And for contour-0, there is no previous, so put it as -1.

“First_Child denotes its first child contour.”

There is no need of any explanation. For contour-2, child is contour-2a. So it gets the corresponding index value of contour-2a. What about contour-3a? It has two children. But we take only first child. And it is contour-4. So First_Child = 4 for contour-3a.

“Parent denotes index of its parent contour.”

It is just opposite of First_Child. Both for contour-4 and contour-5, parent contour is contour-3a. For contour-3a, it is contour-3 and so on.

翻译太累了。。。。上官方解释。。。

需要注意的关键点
在hierachy中的数值指的是在hierachy中的顺序而不是contours中的序号,比如在hierachy中某轮廓的子轮廓是2,则它的子轮廓的信息是在hierachy中下标为2的那个
1. RETR_LIST
This is the simplest of the four flags (from explanation point of view). It simply retrieves all the contours, but doesn’t create any parent-child relationship. Parents and kids are equal under this rule, and they are just contours. ie they all belongs to same hierarchy level.

So here, 3rd and 4th term in hierarchy array is always -1. But obviously, Next and Previous terms will have their corresponding values. Just check it yourself and verify it.

Below is the result I got, and each row is hierarchy details of corresponding contour. For eg, first row corresponds to contour 0. Next contour is contour 1. So Next = 1. There is no previous contour, so Previous = 0. And the remaining two, as told before, it is -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 good choice to use in your code, if you are not using any hierarchy features.
2. RETR_EXTERNAL
If you use this flag, it returns only extreme outer flags. All child contours are left behind. We can say, under this law, Only the eldest in every family is taken care of. It doesn’t care about other members of the family :).

So, in our image, how many extreme outer contours are there? ie at hierarchy-0 level?. Only 3, ie contours 0,1,2, right? Now try to find the contours using this flag. Here also, values given to each element is same as above. Compare it with above result. Below is what I got :

>>> hierarchy
array([[[ 1, -1, -1, -1],[ 2,  0, -1, -1],[-1,  1, -1, -1]]])

You can use this flag if you want to extract only the outer contours. It might be useful in some cases.
3. RETR_CCOMP
This flag retrieves all the contours and arranges them to a 2-level hierarchy. ie external contours of the object (ie its boundary) are placed in hierarchy-1. And the contours of holes inside object (if any) is placed in hierarchy-2. If any object inside it, its contour is placed again in hierarchy-1 only. And its hole in hierarchy-2 and so on.

Just consider the image of a “big white zero” on a black background. Outer circle of zero belongs to first hierarchy, and inner circle of zero belongs to second hierarchy.

We can explain it with a simple image. Here I have labelled the order of contours in red color and the hierarchy they belongs to, in green color (either 1 or 2). The order is same as the order OpenCV detects contours.

So consider first contour, ie contour-0. It is hierarchy-1. It has two holes, contours 1&2, and they belong to hierarchy-2. So for contour-0, Next contour in same hierarchy level is contour-3. And there is no previous one. And its first is child is contour-1 in hierarchy-2. It has no parent, because it is in hierarchy-1. So its hierarchy array is [3,-1,1,-1]

Now take contour-1. It is in hierarchy-2. Next one in same hierarchy (under the parenthood of contour-1) is contour-2. No previous one. No child, but parent is contour-0. So array is [2,-1,-1,0].

Similarly contour-2 : It is in hierarchy-2. There is not next contour in same hierarchy under contour-0. So no Next. Previous is contour-1. No child, parent is contour-0. So array is [-1,1,-1,0].

Contour - 3 : Next in hierarchy-1 is contour-5. Previous is contour-0. Child is contour-4 and no parent. So array is [5,0,4,-1].

Contour - 4 : It is in hierarchy 2 under contour-3 and it has no sibling. So no next, no previous, no child, parent is contour-3. So array is [-1,-1,-1,3].

Remaining you can fill up. This 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
And this is the final guy, Mr.Perfect. It retrieves all the contours and creates a full family hierarchy list. It even tells, who is the grandpa, father, son, grandson and even beyond… :).

For examle, I took above image, rewrite the code for cv2.RETR_TREE, reorder the contours as per the result given by OpenCV and analyze it. Again, red letters give the contour number and green letters give the hierarchy order.


Take contour-0 : It is in hierarchy-0. Next contour in same hierarchy is contour-7. No previous contours. Child is contour-1. And no parent. So array is [7,-1,1,-1].

Take contour-2 : It is in hierarchy-1. No contour in same level. No previous one. Child is contour-2. Parent is contour-0. So array is [-1,-1,2,0].

And remaining, try yourself. Below 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]]])

参考文章

基于的比较老版本的opencv

opencv-python API中的findContours、drawContours与hierachy解析相关推荐

  1. python枪_大疆机甲大师教育机器人Python API中文化之一:枪亮枪暗

    之前开始整理机甲的Python API,但纸上得来终觉浅,而且发现有些API与即使官方qq群的教程文档也有少许出入,于是打算逐个测试.这一系列将附上真机运行视频,以便以后直观看到最终演示效果. 先从灯 ...

  2. 本地化环境下ArcGIS Python API中的SSL及locale的bug修复过程

    本地化环境下ArcGIS Python API中的SSL及locale的bug修复过程 进来试用ArcGIS Pro和对应的ArcGIS Python API,遇见各种问题--新产品还是不成熟啊,特别 ...

  3. python调用usb相机_如何从OpenCV/Python/OSX中的PointGrey USB相机捕捉帧?

    PointGrey是机器视觉摄像头的领先制造商,但不幸的是,他们对Mac OS的支持非常有限.一次www搜索让我猜测我需要安装libusb和libdc1394才能识别摄像头,这是我用brew做的.这没 ...

  4. Appium Python API中文文档

    Appium版本:1.15.1 C:\Users\Admin\AppData\Local\Programs\Appium https://github.com/appium/appium/releas ...

  5. 如何在 Apache Flink 中使用 Python API?

    本文根据 Apache Flink 系列直播课程整理而成,由 Apache Flink PMC,阿里巴巴高级技术专家 孙金城 分享.重点为大家介绍 Flink Python API 的现状及未来规划, ...

  6. python flink_如何在 Apache Flink 中使用 Python API?

    原标题:如何在 Apache Flink 中使用 Python API? 导读:本文重点为大家介绍 Flink Python API 的现状及未来规划,主要内容包括:Apache Flink Pyth ...

  7. OpenCV转换PyTorch分类模型并使用OpenCV Python启动

    OpenCV转换PyTorch分类模型并使用OpenCV Python启动 转换PyTorch分类模型并使用OpenCV Python启动 目标 介绍 要求 实践 模型转换管道 模型评估 评估模式 测 ...

  8. 最佳实践 | 如何基于GitHub Actions创建 DolphinScheduler Python API的CI/CD?

    点亮 ⭐️ Star · 照亮开源之路 https://github.com/apache/dolphinscheduler 01. DolphinScheduler 和 Python API 介绍 ...

  9. 使用Python和OpenCV检测图像中的条形码

    使用Python和OpenCV检测图像中的条形码 1. 效果图 2. 算法的步骤 3. 源码 参考 这篇博客将介绍使用计算机视觉和图像处理技术进行条形码检测的必要步骤,并演示使用Python编程语言和 ...

  10. 使用Python,OpenCV从图像中删除轮廓

    使用Python,OpenCV从图像中删除轮廓 1. 效果图 2. 步骤 3. 源码 4. 参考 1. 使用Python.OpenCV计算轮廓的中心并标记 2. 使用Python.OpenCV检测轮廓 ...

最新文章

  1. 写给Java程序员的Java虚拟机学习指南
  2. Sql Server函数全解三数据类型转换函数和文本图像函数
  3. atoi()函数定义
  4. 沈向洋谈做研究的那些事儿
  5. 一千个不用 Null 的理由,你还用?
  6. 读《程序设计实践》之一 风格
  7. rxjs里使用from operator从一个generator里生成Observable
  8. jasper 获取当前日期_入侵Jasper以获取JSP页面的对象模型
  9. c++常用知识点,易错点,面试常问点
  10. 公司为什么宁愿花11K月薪招新人,也不愿意花9K的月薪留住老员工?
  11. 第二十七篇、使用MVVM布局页面
  12. 百度地图加载空白颜色_详细解析百度收录和百度排名关系
  13. unity3d协同不同设备的代码
  14. 怎么把安装包挂在HTML,怎么把安装包拷贝到u盘
  15. cve20190708补丁的kb名称_64位kb4499175补丁下载
  16. linux终端怎么设置monaco,[Linux]Vim设置Monaco字体Vim颜色模板
  17. 武汉理工大学-数值分析-2019年期末复习提纲
  18. 深蓝学院 浙江大学免费开源课程 !
  19. mysql怎么加载txt文本1148_如何解决MySQL导入数据之ERROR1148(42000)
  20. 天呐!java兼职接单

热门文章

  1. 服务器没有解压文件选项,右键没有好压菜单 怎么设置好压右键
  2. 万字Spring框架学习总结(附核心代码详细注释)
  3. gbdt python_GBDT回归的原理及Python实现
  4. C语言7大常见排序(详细图解)
  5. java cropper_Image Cropper 的 JAVA 支持
  6. leaflet加载谷歌影像地图、天地图影像地图、天地图影像注记
  7. 以阿尔兹海默症为例:深度解析AI+慢病商业模式
  8. 天猫精灵通过私有云控制WiFi设备
  9. mac 卸载java 9
  10. wgs84坐标转换,地图拾取wgs84坐标工具推荐