识别开放和封闭的形状 opencv [英] Recognize open and closed shapes opencv

查看:32
本文介绍了识别开放和封闭的形状 opencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 opencv 中检测开放和封闭的形状.

how to detect open and closed shapes in opencv.

这些是我想要检测的简单样本形状.我已经使用 findContoursapproxPolyDP 检测到矩形,然后检查向量之间的角度.

These are simple sample shapes I want to detect. I have detected rectangle using findContours and approxPolyDP and than checking for angle between vectors.

现在我想检测开放形状,approxPolyDP 函数将闭合形状的 bool 设置为 true,并且在返回的点上检查 isCounterConvex,加上 contourArea 限制.

Now I want to detect the open shape, approxPolyDP function has bool for closed shape set to true, and also there is a check for isCounterConvex on the points returned, plus contourArea limitation.

我应该如何继续检测此类图像的任何想法.

Any ideas how should I go on detecting images of these kind.

推荐答案

只需使用 findContours() ,然后通过检查传递给 findContours() 函数的层次结构来决定轮廓是否闭合.从第二幅图中可以更清楚地看出,与第一幅图像相比,没有轮廓具有子轮廓,您将从层次参数中获得此数据,该参数是可选的输出向量,包含有关图像拓扑的信息.它的元素与轮廓的数量一样多.

Just use findContours() in your image, then decide whether the contour is closed or not by examining the hierarchy passed to the findContours() function. From the second figure it is clearer that no contour has child contour as compared to the first image, you will get this data from hierarchy parameter which is optional output vector, containing information about the image topology. It has as many elements as the number of contours.

这里我们将使用层次结构

Here we will use hierarchy as

vector< Vec4i > hierarchy

第 i 个轮廓的位置

hierarchy[i][0] = next contour at the same hierarchical level
hierarchy[i][1] = previous contour at the same hierarchical level
hierarchy[i][2] = denotes its first child contour
hierarchy[i][3] = denotes index of its parent contour

如果轮廓 i 没有下一个、上一个、父级或嵌套轮廓,则 hierarchy[i] 的相应元素将为负.有关详细信息,请参阅 findContours() 函数.

If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative. See findContours() function for more details.

所以通过检查值 hierarchy[i][2] 你可以决定轮廓是否属于封闭的,如果 hierarchy[i][2]= -1 那么没有孩子,它属于打开的.

So by checking the value hierarchy[i][2] you can decide the contour belongs to closed or not, that is for a contour if the hierarchy[i][2] = -1 then no child and it belongs to opened.

还有一件事是,在 findContours() 函数中,您应该使用 CV_RETR_CCOMP 来检索所有轮廓并将它们组织成两级层次结构.

And one more thing is that in findContours() function you should use CV_RETR_CCOMP which retrieves all of the contours and organizes them into a two-level hierarchy.

这是如何实现这一点的 C++ 代码.

Here is the C++ code how to implement this.

    Mat tmp,thr;
    Mat src=imread("1.png",1);
    cvtColor(src,tmp,CV_BGR2GRAY);
    threshold(tmp,thr,200,255,THRESH_BINARY_INV);

    vector< vector <Point> > contours; // Vector for storing contour
    vector< Vec4i > hierarchy;
    findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

    for( int i = 0; i< contours.size(); i=hierarchy[i][0] ) // iterate through each contour.
    {
        Rect r= boundingRect(contours[i]);
        if(hierarchy[i][2]<0) //Check if there is a child contour
          rectangle(src,Point(r.x-10,r.y-10), Point(r.x+r.width+10,r.y+r.height+10), Scalar(0,0,255),2,8,0); //Opened contour
        else
          rectangle(src,Point(r.x-10,r.y-10), Point(r.x+r.width+10,r.y+r.height+10), Scalar(0,255,0),2,8,0); //closed contour
    }

结果:

这篇关于识别开放和封闭的形状 opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆