lundi 5 décembre 2022

I want to find all children of a contour in OpenCV C++

I want to extract all children of a contour, but I only got one.Here's the final result(the right pic):enter image description here

As you can see, I only got one (innerRect), but I expected to get all three of them. Here's the code:

 findContours(midImage2, contours2, hierarchy2, RETR_TREE, CHAIN_APPROX_SIMPLE);

        for (int i = 0; i < contours2.size(); i++) {

            double areaSize = contourArea(contours2[i]);
            int rect_count;

            Point2f innerRectcornerPoints[4], largeRectCornerPoints[4];
            RotatedRect innerRect, largeRect;

            if (areaSize > 3000) {

                largeRect = minAreaRect(contours2[i]);
                largeRect.points(largeRectCornerPoints);

                for (int j = 0; j < 4; j++) {
                    line(frame, largeRectCornerPoints[j], largeRectCornerPoints[(j + 1) % 4], Scalar(255, 255, 0), 3, 8);
                }
                putText(frame, "largeRect", largeRectCornerPoints[1], 2, 1,  Scalar(255, 255, 0));

                for (int j = 0; j <= i; j++) {
                    if (hierarchy2[j][2] != -1) {
                        innerRect = minAreaRect(contours2[hierarchy2[i][2]]);
                        innerRect.points(innerRectcornerPoints);
                        rect_count++;
                    }
                }

                //if (rect_count < 4) {
                    for (int j = 0; j < 4; j++) {
                        line(frame, innerRectcornerPoints[j], innerRectcornerPoints[(j + 1) % 4], Scalar(0, 255, 255), 3, 8);
                    }
                    putText(frame, "innerRect", innerRectcornerPoints[2], 2, 1, Scalar(0, 255, 255));
                //}
            }

        }

I've been stuck there for almost a day, hope you can help me out.

Aucun commentaire:

Enregistrer un commentaire