当前位置:网站首页>详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
2022-07-02 23:03:00 【昊虹图像算法】
这篇博文是对博文https://blog.csdn.net/wenhao_ir/article/details/125537919和博文https://blog.csdn.net/wenhao_ir/article/details/51798533的延伸。
为了搞清楚轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)的含义,我们用下面的两个例子来实际测试,并对结果进行分析就能完全搞清楚其意义了。
在运行例子前我们先回顾一下我在博文https://blog.csdn.net/wenhao_ir/article/details/51798533中对函数findContours()的参数hiararchy的介绍。
hiararchy—这是一个可选输出参数,它用来存储检测到的轮廓的拓扑结构信息。检测到的每个轮廓都对应一个拓扑结构信息。对于每个轮廓contours[i], hierarchy[i][0]、 hiearchy[i][1] 、 hierarchy[i][2] 、hiearchy[i][3]的意义分别如下:
- hierarchy[i][0]:与轮廓contours[i]同一个父轮廓的下一个轮廓的索引值;
- hierarchy[i][1]:与轮廓contours[i]同一个父轮廓的上一个轮廓的索引值;
- hierarchy[i][2]:轮廓contours[i]的第一个子轮廓的索引值;
- hierarchy[i][3]:轮廓contours[i]的父轮廓的索引值;
好,回顾完上面这个知识点之后我们开始用实例来说明。
首先是下面这张图片:
上面这张图片的名字为ring_03.bmp,
百度网盘下载链接:https://pan.baidu.com/s/1OfFYBY92tN1I6Bf8SUHlNg?pwd=cb3p
检测其轮廓的代码如下:
# 博主微信/QQ 2487872782
# 有问题可以联系博主交流
# 有图像处理需求也请联系博主
# 图像处理技术交流QQ群 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV的版本为4.1
import cv2 as cv
import sys
image = cv.imread('F:/material/images/2022/2022-06/ring_03.bmp')
if image is None:
print('Error: Could not load image')
sys.exit()
# cv.imshow('Source Image', image)
# 原图像转化为灰度图
img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# cv.imshow('img_gray', img_gray)
# 灰度图进行二值化处理,并不是函数findContours要求输入图像为二值图像,
# 而是函数findContours在进行轮廓提取前会把原图中的非0值全部当成1处理。
_, img_B = cv.threshold(img_gray, 71, 255, cv.THRESH_BINARY)
# cv.imshow('img_B', img_B)
# 轮廓检测
cnts, harch = cv.findContours(img_B, mode=cv.RETR_TREE, method=cv.CHAIN_APPROX_SIMPLE)
我利用博文https://blog.csdn.net/wenhao_ir/article/details/125573892中的代码将各轮廓单独输出成图像,结果如下:
我把上面几张图进行了打包,并上传到百度网盘,方便大家查看,链接 https://pan.baidu.com/s/1pAmwa-KCBMkx4yMX8EvXRg?pwd=qqet
回到正题,上面的代码检测到的轮廓拓扑结构如下:
这个轮廓拓扑结构的每一行代表一个轮廓的拓扑结构信息,分别表示与其同一个父轮廓的前一个轮廓的索引、与其同一个父轮廓的后一个轮廓的索引、子轮廓的索引、父轮廓的索引。
根据上面的轮廓拓扑结构我可以绘制出其轮廓拓扑结构图如下:
怎么绘制出来的呢?博主录了个视频把这个过程进行详细讲述,视频在线观看和下载链接:
https://pan.baidu.com/s/1mBWeDhYw6tcBBOdc-lvH5Q?pwd=2mh0
通过上面这个视频讲解,相信大家就能掌握怎么样根据轮廓拓扑结构矩阵绘制轮廓拓扑结构图了。有了这个基础之后,我们就可以来更具体的探究轮廓拓扑结构矩阵的意义了。
在上面的简单示例中,第二层的轮廓①、③、⑤虽然处于是同一层,但因为没有共同的父轮廓,所以它们的轮廓矩阵结构中的上一个轮廓和下一个轮廓的值都为-1。在轮廓结构图上体现出来为轮廓①、③、⑤的左右两边都没有轮廓。
仔细点的朋友要问了,那0、2、4轮廓也没有共同的父轮廓啊,怎么它们的共同父轮廓的下一个索引值和上一个索引值不为-1呢?答案是这样的:因为它们处于第一层,我们可以想像它们有共同的根(root),所以它们有共同的父轮廓root,而轮廓①、③、⑤的父轮廓就很明显是不同的了。
接下来我们看拓扑结构图中第二层(包括第二层)以下的层级轮廓存在共同父轮廓的例子。
原图如下:
上面这张图的名字为:img_300_320.jpg,百度网盘下载链接:https://pan.baidu.com/s/1IaJ8nrQzGuHt3RA8jbu0GQ?pwd=bjkm
检测其轮廓的代码如下:
# 博主微信/QQ 2487872782
# 有问题可以联系博主交流
# 有图像处理需求也请联系博主
# 图像处理技术交流QQ群 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV的版本为4.1
import cv2 as cv
import sys
image = cv.imread('F:/material/images/2022/2022-06/img_300_320.jpg')
if image is None:
print('Error: Could not load image')
sys.exit()
# cv.imshow('Source Image', image)
# 原图像转化为灰度图
img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# cv.imshow('img_gray', img_gray)
# 灰度图进行二值化处理,并不是函数findContours要求输入图像为二值图像,
# 而是函数findContours在进行轮廓提取前会把原图中的非0值全部当成1处理。
_, img_B = cv.threshold(img_gray, 71, 255, cv.THRESH_BINARY)
# cv.imshow('img_B', img_B)
# 轮廓检测
cnts, harch = cv.findContours(img_B, mode=cv.RETR_TREE, method=cv.CHAIN_APPROX_SIMPLE)
博文https://blog.csdn.net/wenhao_ir/article/details/125573892中有这幅图像轮廓检测结果的每个轮廓的单独输出图像。
检测结果如下:
我们可以根据上面这个拓扑结构矩阵画出下面的拓扑结构图:
至于怎么画出来的,我想大家看了第一幅的绘制讲解视频,是很容易画出来的,所以这里就不再录演示视频了。
从上面这个轮廓拓扑结构图我们可以清晰的看出:
- 整个轮廓拓扑结构共有五层;
- 第0号轮廓为整个轮廓拓扑结构的最高层(最外层),它有一个索引号为1的子轮廓;
- 第1号轮廓有12个子轮廓,索引号分别为2、3、4、5、6、7、8、9、10、11、12、25
- 第12号轮廓有11个子轮廓, 索引号分别为13、15、16、17、18、19、20、21、22、23、24
- 第13号轮廓有1个子轮廓,这个子轮廓的索引号为14
- 第14号轮廓是整个轮廓拓扑结构的最低层(最里层)。
这个例子的第1号轮廓有12个子轮廓,索引号分别为2、3、4、5、6、7、8、9、10、11、12、25,即这些轮廓有共同的父轮廓1,所以轮廓2、3、4、5、6、7、8、9、10、11、12、25的结构矩阵中的上一个和下一个轮廓的值不为-1,在轮廓结构中体现出来为它们处于同一层并且左右两边有轮廓。
这里就有一个问题,1号轮廓有12个子轮廓,那么其轮廓结构中的第3个表示子轮廓索引的值为多少呢?我们看到,是2,即其第1个子轮廓。同样,12号轮廓有11个子轮廓,那么其轮廓结构中的第3个表示子轮廓索引的值为多少呢?我们看到,是13,也是其第1个子轮廓。所以通过这个例子,我们就明白了当一个轮廓有多个子轮廓值时,其子轮廓的索引值填的是哪个子轮廓。
至此,通过这两个例子,我们就把轮廓拓扑结构(hiararchy)矩阵的意义彻底搞清楚了。
边栏推荐
- yolov5test. Py comment
- SQL query statement parameters are written successfully
- Additional: token; (don't read until you finish writing...)
- Judge whether the binary tree is full binary tree
- 開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
- 开发知识点
- 接口差异测试——Diffy工具
- Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
- Leetcode relaxation question - day of the week
- Cmake basic use
猜你喜欢

容器运行时分析

请问大家在什么网站上能查到英文文献?
![MATLAB signal processing [Q & a notes-1]](/img/53/ae081820fe81ce28e1f04914678a6f.png)
MATLAB signal processing [Q & a notes-1]

Which websites can I search for references when writing a thesis?

130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth

Optimization of streaming media technology
![[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)](/img/f7/a8eb8e40b9ea25021751d7150936ac.jpg)
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)

In February 2022, the ranking list of domestic databases: oceanbase regained its popularity with "three consecutive increases", and gaussdb is expected to achieve the largest increase this month

35 pages dangerous chemicals safety management platform solution 2022 Edition

Xcode real machine debugging
随机推荐
Implement the foreach method of array
经济学外文文献在哪查?
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Interface automation coverage statistics - used by Jacobo
Bypass AV with golang
What are the projects of metauniverse and what are the companies of metauniverse
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
In February 2022, the ranking list of domestic databases: oceanbase regained its popularity with "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
监控容器运行时工具Falco
Top Devops tool chain inventory
CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
MFC file operation
Realization of mask recognition based on OpenCV
Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs
Basic 10 of C language: array and pointer
接口自动化覆盖率统计——Jacoco使用
Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
67 page overall planning and construction plan for a new smart city (download attached)
Which software can translate an English paper in its entirety?
yolov5test. Py comment