我使用过FindContours,而且知道有能够直接寻找联通区域的函数。但是我使用的大多只是“最大轮廓”或者"轮廓数目“这些数据。其实轮廓还有另一个很重要的性质,那就是轮廓的相互包含特性。

比如典型的运用在二维码上面
    对于它的3个定位点,认为构造了相互包含的轮廓区域,这种特性,在图上只有三处,而且在自然图片中是不容易重复的(当然二维码内部还有校验机制)。那么基于此就能够很容易地获取准确定位。将二维码的这种机制进行扩展,能够得到对实际物体的定位方法。比如如果我将笔记本的内页设计成这样(当然也可以是答题卡之类),那么就能够为准确的定位、透视变换打下好的基础。
     而首先,就是要能够把轮库这块吃透。最原始和基本的资料就是《学习OpenCV》
 
对于这幅图的解释:
 
直接使用代码进行学习
);
    threshold(src,src,,,THRESH_OTSU);
    vector, ) );
    waitKey();
}
核心函数是findContours,它的几个参数是什么意思了?
第一个参数是输入图像,必须是8通道的,并且应该被转化为二值(所有我threshold了预先), 已经说明了,用于Contours寻找的图片会被直接修改,如果需要它用,需要先复制一份后再保存。
第二个参数是内存存储器,FindContours找到的轮廓放到内存里面。这里只是一个指针;
第三个参数是mode,可以为
,
    CV_RETR_LIST,
    CV_RETR_CCOMP,
    CV_RETR_TREE,
    CV_RETR_FLOODFILL
};
 
 
向FindContours说明需要的轮廓类型和希望的返回形式。也就是内部轮廓的组织形式。其中显然CV_RETR_TREE模式是最全的模式
 
最后一个参数是轮廓如何被近似
 
,
       CHAIN_APPROX_SIMPLE    ,
       CHAIN_APPROX_TC89_L1   ,
       CHAIN_APPROX_TC89_KCOS 
}
 
不过,如果我们是想获得准确的值的话,这可能更多采用的是NONE,也就是显示所有值。
书本内容学完了,但是并没有解决问题。因为我们需要的是想一个办法,使得轮廓之间的关系能够显示出来。所以需要继续挖掘文档。
 
http://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.html
Also, in the output, we got three arrays, first is the image, second is our contours, and one more output which we named as hierarchy (Please checkout the codes in previous articles). But we never used this hierarchy anywhere. Then what is this hierarchy and what is it for ? What is its relationship with the previous mentioned function argument ?
Consider an example image below :
在这幅图中(白色区域为有数据的区域,黑色为无数据),0,1,2是第一层,然后里面是3,3的里面是4和5。(2a表示是2的内部)

 

In this image, there are a few shapes which I have numbered from 0-5. 2 and 2a denotes the external and internal contours of the outermost box.

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
对于Tree这种模式,这样解释很清楚
 

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.

image

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:

1 >>> hierarchy
2 array([[[ 7, -1, 1, -1],
3  [-1, -1, 2, 0],
4  [-1, -1, 3, 1],
5  [-1, -1, 4, 2],
6  [-1, -1, 5, 3],
7  [ 6, -1, -1, 4],
8  [-1, 5, -1, 4],
9  [ 8, 0, -1, -1],
10  [-1, 7, -1, -1]]])
 
 
 

OpenCV使用FindContours进行二维码定位的更多相关文章

  1. 基于Opencv识别,矫正二维码(C++)

    参考链接 [ 基于opencv 识别.定位二维码 (c++版) ](https://www.cnblogs.com/yuanchenhui/p/opencv_qr.html) OpenCV4.0.0二 ...

  2. zbar+opencv检测图片中的二维码或条形码

    zbar本身自带检测二维码条形码功能,这里使用opencv只是做一些简单的读取图片,灰度图片以及显示条形码和二维码时用到一些绘制 // barcode-qrcodescanner.cpp: 定义控制台 ...

  3. 基于opencv 识别、定位二维码 (c++版)

    前言 因工作需要,需要定位图片中的二维码:我遂查阅了相关资料,也学习了opencv开源库.通过一番努力,终于很好的实现了二维码定位.本文将讲解如何使用opencv定位二维码. 定位二维码不仅仅是为了识 ...

  4. iOS原生实现二维码拉近放大

    http://www.cocoachina.com/ios/20180416/23033.html 2018-04-16 15:34 编辑: yyuuzhu 分类:iOS开发 来源:程序鹅 8 300 ...

  5. Qt+QZXing编写识别二维码的程序

    本人最近在用Qt编写程序,需要用编写二维码识别功能.在网上搜寻一番,找到了QZXing.配置过程中确实出了一大把汗,这里我写这篇文章记录配置方法,替后人省一把汗吧!我的开发环境:MSVC2010 + ...

  6. python二维码模块(qrcode)

    qrcode模块安装 运行命令行工具(cmd),使用pip安装工具分别安装qrcode. pip install qrcode 先来个简单的例子 import qrcode # 二维码内容 data ...

  7. Android利用zxing生成二维码

    感谢大佬:https://blog.csdn.net/mountain_hua/article/details/80646089 **gayhub上的zxing可用于生成二维码,识别二维码 gayhu ...

  8. Opencv+Zbar二维码识别(标准条形码/二维码识别)

    使用Opencv+Zbar组合可以很容易的识别图片中的二维码,特别是标准的二维码,这里标准指的是二维码成像清晰,图片中二维码的空间占比在40%~100%之间,这样标准的图片,Zbar识别起来很容易,不 ...

  9. 基于opencv+python的二维码识别

    花了2天时间终于把二维码识别做出来了,不过效果一般,后面会应用在ROS辅助定位上,废话少说先上图: 具体过程参考了这位大神的博客:http://blog.csdn.net/qq_25491201/ar ...

随机推荐

  1. 关于在官网上下载Eclipse遇到的问题!!

    首先Eclipse是什么? Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境. 幸运的是,Eclipse 附带了一 ...

  2. rf对时间控件的操作

    1.如何去掉readonly属性 前端对于时间控件的设置,有时是为了限制用户不能进行手动输入方式进行选择时间,避免在手动输入的时候超限或者输入格式不正确,导致一些不必要的验证麻烦,这是前端开发工程师就 ...

  3. [html5] 学习笔记-服务器推送事件

    1.HTML5服务器推送事件介绍 服务器推送事件(Server-sent Events)是Html5规范的一个组成部分,可以用来从服务端实时推送数据到浏览器端. 传统的服务器推送技术----WebSo ...

  4. .Net程序员学用Oracle系列(12):增删改查

    1.插入语句 1.1.INSERT 1.2.INSERT ALL 2.删除语句 2.1.DELETE 2.2.TRUNCATE 3.更新语句 3.1.UPDATE 3.2.带子查询的 UPDATE 3 ...

  5. 地图学与GIS制图的基础理论(二)

    利用GIS技术进行地图制图,其最终目标还是需要回到地图学中去.地图学中关于地图制作的经典要求,有以下几点: 地图必须要与现实相符,符合人类的感知 这点是地图最基本的一条,地图的每一个要素展现的都是跟现 ...

  6. Giraph入门

    概要 这是一个Giraph的入门教程,主要用来运行少量输入的Giraph程序,并不能用于生产环境. 在这个教程中,我们将会在一个物理机器行部署一个单节点,伪分布的Hadoop集群.这个节点既是mast ...

  7. git链接GitHub命令及基本操作

    Git是一款不错的代码管理工具,下面引用百科的一段话:  Git是用于Linux内核开发的版本控制工具.与CVS.Subversion一类的集中式版本控制工具不同,它采用了分布式版本库的作法,不需要服 ...

  8. android学习6——canvas的save,restore作用

    先看如下代码 public class SaveRestoreActivity extends Activity { @Override public void onCreate(Bundle sav ...

  9. WP8.1应用双击返回键退出程序。

    #region 双击退出程序代码 //双击HardwareButtons.BackPressed: //出现退出提示窗口: //“确定”退出,“取消”返回什么也不做: private async vo ...

  10. Oracle11G卸载教程

    用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,那么怎么才能完全卸载Oracle呢?那就是直接注册表清除,步骤如下: 1. 开始->设置->控制面板-& ...