I have two contours and I want to check the relation between them (if one of them is nested).
Normally, I would use the findContours function with CV_RETR_TREE retrieval mode. However, I obtained the contours from a different source (using MSER method). I actually not only have the contours, but also the region mask if that helps. For example, lets say I want to segment the letter ‘O’, then I would have the following masks or contours:
1)
0 0 0 0 0 0
0 1 1 1 1 0
0 1 0 0 1 0
0 1 0 0 1 0
0 1 1 1 1 0
0 0 0 0 0 0
2)
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 0 0
0 0 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
How can I easily check that the second one is inside the first contour? I thought about checking the relation between the bounding boxes, but this doesn’t cover all possible cases.
Use
cv::pointPolygonTest(InputArray contour, Point2f pt, bool measureDist)to know whether a point from a contour is inside the other.You have to check for border cases (the first point you pick is common to both polygons, etc)
The function determines whether the point is inside a contour, outside, or lies on an edge (or coincides with a vertex). It returns positive (inside), negative (outside), or zero (on an edge) value, correspondingly.
When
measureDist=false, the return value is +1, -1, and 0, respectively. Otherwise, the return value is a signed distance between the point and the nearest contour edge.