栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

OpenCV的findContours源码与原理

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OpenCV的findContours源码与原理

1、源码

https://github.com/opencv/opencv/blob/master/modules/imgproc/src/contours.cpp

opencvbuildincludeopencv2imgprocimgproc.hpp

CV_EXPORTS_W void findContours( InputArray image, OutputArrayOfArrays contours,
                              OutputArray hierarchy, int mode,
                              int method, Point offset = Point());


CV_EXPORTS void findContours( InputArray image, OutputArrayOfArrays contours,
                              int mode, int method, Point offset = Point());

2、论文

根据opencv官方文档,OpenCV: Contours Hierarchy,采用的是:

Suzuki, S. and Abe, K.,Topological Structural Analysis of Digitized Binary Images by Border Following. CVGIP 30 1, pp 32-46 (1985)

说明:openCv的contours是分级的,其寻边理论依据(方式)参考suzuki的论文《Topological structural analysis of digitized binary images by border following》。

http://pdf-s3.xuebalib.com:1262/1ftg5E69C3uX.pdf

3、Contour 的寻边模式 Mode
openCV通过一个矩阵来管理等级,矩阵的元素表示方法是:[Next, Previous, First_Child, Parent]

RETR_LIST:列出所有的边,没有父子层级之分(全部边缘都为1级)。
在这种模式下,这8条边建立的等级关系为
[ 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]
例如边“0”,其同等级的Next是边“1”,前一个不存在(-1),没有First_Child, Parent,这两个参数也都设为-1,所以第0个元素是
[1,-1,-1,-1];边“1”,其同等级的Next是边“2”,前一个边是“0”,没有First_Child, Parent,两个-1,所以第1个元素是[2,0,-1,-1];依次类推。

RETR_EXTERNAL:列出最外面的边(如物体的外边框),不管被包围的内环或边(如物体的孔洞)。

RETR_CCOMP:只取2个层级的边,如下图,只把边(粉红色)分为两个层(绿色),标记为绿色的(1)顶层和(2)次层。

上面图中,这9条边建立的等级关系为
[ 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]
例如第"0"边,其相邻的Next是边"3", Previous不存在(-1),First-child=边"1",Parent不存在(-1),所以其相应的元素为
[3, -1, 1, -1],其余元素依此规则类推。

RETR_TREE:返回所有的边及层级关系,

这9条边建立的等级关系为
[ 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源码解析之findContours_tanmx219的博客-CSDN博客

OpenCV: Contours Hierarchy

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/444062.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号