void main(){
Mat src = imread("e:/sandbox/contours.png",0);
threshold(src,src,100,255,THRESH_OTSU);
vector<vector<Point> > contours,contours2;
vector<Vec4i> hierarchy;
findContours( src, contours, hierarchy, CV_RETR_TREE, CHAIN_APPROX_NONE, Point(0, 0) );
waitKey();
}
核心函数是findContours,它的几个参数是什么意思了?第一个参数是输入图像,必须是8通道的,并且应该被转化为二值(所有我threshold了预先), 已经说明了,用于Contours寻找的图片会被直接修改,如果需要它用,需要先复制一份后再保存。第二个参数是内存存储器,FindContours找到的轮廓放到内存里面。这里只是一个指针;第三个参数是mode,可以为/* Contour retrieval modes */
enum
{
CV_RETR_EXTERNAL=0,
CV_RETR_LIST=1,
CV_RETR_CCOMP=2,
CV_RETR_TREE=3,
CV_RETR_FLOODFILL=4
};
向FindContours说明需要的轮廓类型和希望的返回形式。也就是内部轮廓的组织形式。其中显然CV_RETR_TREE模式是最全的模式最后一个参数是轮廓如何被近似//! the contour approximation algorithm
enum { CHAIN_APPROX_NONE = 1,
CHAIN_APPROX_SIMPLE = 2,
CHAIN_APPROX_TC89_L1 = 3,
CHAIN_APPROX_TC89_KCOS = 4
}
不过,如果我们是想获得准确的值的话,这可能更多采用的是NONE,也就是显示所有值。书本内容学完了,但是并没有解决问题。因为我们需要的是想一个办法,使得轮廓之间的关系能够显示出来。所以需要继续挖掘文档。http://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.htmlConsider an example image below :在这幅图中(白色区域为有数据的区域,黑色为无数据),0,1,2是第一层,然后里面是3,3的里面是4和5。(2a表示是2的内部)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.
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]**
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.
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.
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.
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.
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.
imageTake 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]]])来源: http://www.cnblogs.com/jsxyhelu/p/6579412.html