当前位置:网站首页>Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
2022-07-03 00:24:00 【Haohong image algorithm】
This blog post is a response to the blog post https://blog.csdn.net/wenhao_ir/article/details/125537919 And blog https://blog.csdn.net/wenhao_ir/article/details/51798533 Extension of .
In order to understand the contour detection function findContours() The resulting contour topology (hiararchy) The meaning of , We use the following two examples to actually test , And the significance can be fully understood by analyzing the results .
Before running the example, let's review my blog https://blog.csdn.net/wenhao_ir/article/details/51798533 Middle pair function findContours() Parameters of hiararchy Introduction to .
hiararchy— This is an optional output parameter , It is used to store the topology information of the detected contour . Each detected contour corresponds to a topology information . For each profile contours[i], hierarchy[i][0]、 hiearchy[i][1] 、 hierarchy[i][2] 、hiearchy[i][3] The meanings of are as follows :
- hierarchy[i][0]: And contour contours[i] The same parent outline Index value of the next contour of ;
- hierarchy[i][1]: And contour contours[i] The same parent outline Index value of the previous contour of ;
- hierarchy[i][2]: outline contours[i] Index value of the first sub contour of ;
- hierarchy[i][3]: outline contours[i] Index value of the parent contour of ;
good , After reviewing the above knowledge points, we begin to illustrate with examples .
The first is the following picture :
The name of the picture above is ring_03.bmp,
Baidu online disk download link :https://pan.baidu.com/s/1OfFYBY92tN1I6Bf8SUHlNg?pwd=cb3p
The code for detecting its contour is as follows :
# Blogger WeChat /QQ 2487872782
# If you have any questions, you can contact the blogger
# Please contact the blogger if you need image processing
# Image processing technology exchange QQ Group 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV The version is 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)
# The original image is converted into a grayscale image
img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# cv.imshow('img_gray', img_gray)
# The gray image is binarized , It's not a function findContours The input image is required to be a binary image ,
# But the function findContours Before contour extraction, the non 0 Value all as 1 Handle .
_, img_B = cv.threshold(img_gray, 71, 255, cv.THRESH_BINARY)
# cv.imshow('img_B', img_B)
# Contour detection
cnts, harch = cv.findContours(img_B, mode=cv.RETR_TREE, method=cv.CHAIN_APPROX_SIMPLE)
I use blog https://blog.csdn.net/wenhao_ir/article/details/125573892 The code in will output each contour separately as an image , give the result as follows :
I packed the above pictures , And upload to Baidu disk , It's convenient for you to check , link https://pan.baidu.com/s/1pAmwa-KCBMkx4yMX8EvXRg?pwd=qqet
Back to the point , The contour topology detected by the above code is as follows :
Each line of the contour topology represents the topology information of a contour , Respectively represent the index of the previous contour with the same parent contour 、 Index of the next contour with the same parent contour 、 Index of sub contour 、 Index of the parent contour .
According to the above contour topology, I can draw its contour topology as follows :
How to draw it ? The blogger recorded a video to describe this process in detail , Video online viewing and download links :
https://pan.baidu.com/s/1mBWeDhYw6tcBBOdc-lvH5Q?pwd=2mh0
Explain through the above video , I believe you can master how to draw a contour topology map according to the contour topology matrix . With this foundation , We can explore the significance of contour topological structure matrix more specifically .
In the simple example above , The outline of the second floor ①、③、⑤ Although on the same floor , But because there is no common father Profile , Therefore, the values of the previous contour and the next contour in their contour matrix structure are -1. Reflected on the outline structure diagram is the outline ①、③、⑤ There is no outline on the left and right sides of .
Careful friends need to ask , that 0、2、4 The outline has no common parent outline , How come the next index value and the previous index value of their common parent contour are not -1 Well ? The answer is this : Because they are on the first floor , We can imagine that they have common roots (root), So they have a common parent outline root, And the outline ①、③、⑤ The father outline of is obviously different .
Next, let's look at the second layer in the topology diagram ( Including the second floor ) The following hierarchical contour has an example of a common parent contour .
The original picture is as follows :
The name of the picture above is :img_300_320.jpg, Baidu online disk download link :https://pan.baidu.com/s/1IaJ8nrQzGuHt3RA8jbu0GQ?pwd=bjkm
The code for detecting its contour is as follows :
# Blogger WeChat /QQ 2487872782
# If you have any questions, you can contact the blogger
# Please contact the blogger if you need image processing
# Image processing technology exchange QQ Group 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV The version is 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)
# The original image is converted into a grayscale image
img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# cv.imshow('img_gray', img_gray)
# The gray image is binarized , It's not a function findContours The input image is required to be a binary image ,
# But the function findContours Before contour extraction, the non 0 Value all as 1 Handle .
_, img_B = cv.threshold(img_gray, 71, 255, cv.THRESH_BINARY)
# cv.imshow('img_B', img_B)
# Contour detection
cnts, harch = cv.findContours(img_B, mode=cv.RETR_TREE, method=cv.CHAIN_APPROX_SIMPLE)
post https://blog.csdn.net/wenhao_ir/article/details/125573892 There is a separate output image of each contour with the contour detection result of this image .
The test results are as follows :
We can draw the following topological structure diagram according to the topological structure matrix above :
As for how to draw it , I think you have seen the first video of drawing and explanation , It's easy to draw , So we won't record demo videos here .
From the above outline topology, we can clearly see :
- The whole contour topology has five layers ;
- The first 0 No. contour is the highest level of the entire contour topology ( Outermost layer ), It has an index number of 1 Sub contour of ;
- The first 1 Outline No 12 Sub outline , The index numbers are 2、3、4、5、6、7、8、9、10、11、12、25
- The first 12 Outline No 11 Sub outline , The index numbers are 13、15、16、17、18、19、20、21、22、23、24
- The first 13 Outline No 1 Sub outline , The index number of this sub outline is 14
- The first 14 No. contour is the lowest layer of the whole contour topology ( Innermost layer ).
The second part of this example 1 Outline No 12 Sub outline , The index numbers are 2、3、4、5、6、7、8、9、10、11、12、25, That is, these contours have a common parent contour 1, So the outline 2、3、4、5、6、7、8、9、10、11、12、25 The values of the previous and next contours in the structure matrix of are not -1, In the outline structure, they are on the same layer and have outlines on the left and right sides .
There's a problem here ,1 Outline No 12 Sub outline , Then the second in its outline structure 3 What is the value of the sub contour index ? We see , yes 2, That is, its No 1 Sub outline . Again ,12 Outline No 11 Sub outline , Then the second in its outline structure 3 What is the value of the sub contour index ? We see , yes 13, It is also its second 1 Sub outline . So through this example , We can see clearly that when a contour has multiple sub contour values , The index value of its sub contour is filled in which sub contour .
thus , Through these two examples , Let's put the contour topology (hiararchy) The meaning of matrix is completely clear .
边栏推荐
- Shell脚本基本使用
- How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
- Interface difference test - diffy tool
- How do educators find foreign language references?
- form表单实例化
- yolov5test. Py comment
- 1380. Lucky numbers in the matrix
- Leetcode skimming - game 280
- collections. What is the purpose of chainmap- What is the purpose of collections. ChainMap?
- JVM foundation review
猜你喜欢
What are the recommended thesis translation software?
多进程编程(二):管道
Install docker and use docker to install MySQL
Chapter 3 of getting started with MySQL: database creation and operation
多进程编程(一):基本概念
监控容器运行时工具Falco
Explain in detail the process of realizing Chinese text classification by CNN
95 pages of smart education solutions 2022
请问大家在什么网站上能查到英文文献?
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
随机推荐
35 pages dangerous chemicals safety management platform solution 2022 Edition
Unique line of "Gelu"
带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
LeedCode1480.一维数组的动态和
TypeError: Cannot read properties of undefined (reading ***)
接口差异测试——Diffy工具
Xcode real machine debugging
Mutual exclusion and synchronization of threads
[shutter] image component (image component introduction | image constructor | image.network constructor | image.asset constructor)
Slf4j + logback logging framework
Feature Engineering: summary of common feature transformation methods
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
yolov5detect. Py comment
MFC gets the current time
教育学大佬是怎么找外文参考文献的?
Markdown使用教程
[target detection] r-cnn, fast r-cnn, fast r-cnn learning
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Matlab 信号处理【问答笔记-1】
FRP reverse proxy +msf get shell