Intersection between contour and rectangle OPENcv c++
c++, intersection, opencv
Solution
If you are sure that your rectangle cross the shape at only 2 points, you can iterate through your contour point, and check if these points are in your rectangle border.
std::vector<cv::Point> shape; // computed with FindContours
cv::Rect myRect; //whatever
const int NUMPOINTS = 2;
int found = 0;
for(std::vector<cv::Point>::iterator it = shape.begin(); it != shapes.end() && found < NUMPOINTS; ++it) {
if (it->y == myRect.y && it->x >= myRect.x && it->x < myRect.x + width)
// that point cross the top line of the rectangle
found++; // you might want to store the point
else if (// ... add the other checks here)
}
Problem
I have drawn a rectangle using cv.rectangle and have a contours shape (from FindContours) which the rectangle is drawn over . The rectangle intersects the the complete contour at two points. How can I find these points of intersection between the rectangle and the contour outline. I could add the two images together and look for maxima but I do know how the rectangle vertices are stored , as I would need a line type vector filled with a set of points Thanks