简体   繁体   中英

Opencv Contours Hierarchy

I am trying to access the contour that is nested within another contour to run a few tests on it, such as an area test and to see if the bounding rectangle is about square. I figured out how to test if there is an internal contour, but I do not know how to access it.

Relevant code

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
DEBUG_SHOW("binary image", binary);
findContours(binary, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<vector<Point> > polygons(contours.size());
for (size_t i = 0; i < contours.size(); i++) {
    vector<Point> contour = contours[i];
    double area = contourArea(contour);
    if (area < 300) {
        continue;
    }
    if(hierarchy[i][2] != -1){ 
        //test internal contour
    }

If I'm correct then you have a set of Points

 Vector<point>

and then you have a collection of those sets

 Vector<Vector<point>>

Now with that in mind. You iterate through the Vector(point) using

for (size_t i = 0; i < contours.size(); i++) {

& You are required to iterate through the vector within

You will need to:

for (size_t j=0; j = contours.at(i).size(); j++){
    //Do Your Thang
    i++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM