OpenCV使用FindContours进行二维码定位
我使用过FindContours,而且知道有能够直接寻找联通区域的函数。但是我使用的大多只是“最大轮廓”或者"轮廓数目“这些数据。其实轮廓还有另一个很重要的性质,那就是轮廓的相互包含特性。
threshold(src,src,,,THRESH_OTSU);
vector, ) );
waitKey();
}
CV_RETR_LIST,
CV_RETR_CCOMP,
CV_RETR_TREE,
CV_RETR_FLOODFILL
};
CHAIN_APPROX_SIMPLE ,
CHAIN_APPROX_TC89_L1 ,
CHAIN_APPROX_TC89_KCOS
}
Here, contours 0,1,2 are external or outermost. We can say, they are in hierarchy-0 or simply they are in same hierarchy level.
Next comes contour-2a. It can be considered as a child of contour-2 (or in opposite way, contour-2 is parent of contour-2a). So let it be in hierarchy-1. Similarly contour-3 is child of contour-2 and it comes in next hierarchy. Finally contours 4,5 are the children of contour-3a, and they come in the last hierarchy level. From the way I numbered the boxes, I would say contour-4 is the first child of contour-3a (It can be contour-5 also).
I mentioned these things to understand terms like same hierarchy level, external contour, child contour, parent contour, first child etc. Now let's get into OpenCV.
Hierarchy Representation in OpenCV (层级关系)
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.
- Note
- If there is no child or parent, that field is taken as -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:
最新文章
- Python’s SQLAlchemy vs Other ORMs[转发 1]SQLObject
- PopupWindow+ListView+OnItemClick点击无效
- 161103、Spring Boot 入门
- 安装cgdb
- bzoj 2595 斯坦纳树
- ASP.NET MVC之文件上传【一】
- PHP 布尔类型
- Bresenham算法画填充圆及SDL代码实现
- POJ 2001-Shortest Prefixes(Trie 入门)
- IBM developerWorks 的Ajax系列教程
- OleContainer操作Excel以二进制方式读写数据库
- 【bzoj2819】Nim
- Vue路由学习心得
- 在Winform开发框架中使用DevExpress的内置图标资源
- python生成数据后,快速导入数据库
- java学习(二)
- 20165234 《Java程序设计》第二周学习总结
- day 10 函数入门
- python 自然语言处理(六)____N-gram标注
- CentOS 6.3开机启动服务及自动联网