Ask Your Question

Revision history [back]

A rotated rectangle consists of four points, the corners. Therefore it should be possible to convert it to a contour. Then I would suggest you follow the answer on StackOverflow, linked below, to see if your point is within that contour.

Determine if a point is inside or outside of a shape with opencv

That would make you end up with something like this:

bool DoesRectangleContainsPoint(RotatedRect rectangle, Point2f point) {

    //Get the corner points.
    Point2f corners[4];
    rectangle.points(corners);

    //Convert the point array to a vector.
    //https://stackoverflow.com/a/8777619/1997617
    Point2f* lastItemPointer = (corners + sizeof corners / sizeof corners[0]);
    vector<Point2f> contour(corners, lastItemPointer);

    //Check if the point is within the rectangle.
    double indicator = pointPolygonTest(contour, point, false);
    bool rectangleContainsPoint = (indicator >= 0);
    return rectangleContainsPoint;
}

A rotated rectangle consists of four points, the corners. Therefore it should be possible to convert it to a contour. Then I would suggest you follow the answer on StackOverflow, linked below, to see if your point is within that contour.

Determine if a point is inside or outside of a shape with opencv

That would make you end up with something like this:

bool DoesRectangleContainsPoint(RotatedRect DoesRectangleContainPoint(RotatedRect rectangle, Point2f point) {

    //Get the corner points.
    Point2f corners[4];
    rectangle.points(corners);

    //Convert the point array to a vector.
    //https://stackoverflow.com/a/8777619/1997617
    Point2f* lastItemPointer = (corners + sizeof corners / sizeof corners[0]);
    vector<Point2f> contour(corners, lastItemPointer);

    //Check if the point is within the rectangle.
    double indicator = pointPolygonTest(contour, point, false);
    bool rectangleContainsPoint = (indicator >= 0);
    return rectangleContainsPoint;
}