Ask Your Question

Check if a Point is inside a RotatedRect ?

asked 2014-03-20 10:49:48 -0500

yes123 gravatar image

updated 2014-03-20 10:56:45 -0500

Does RotatedRect has some method like Rect.contains()?

I have a Point


and a RotatedRect


Without having to rotate the point, does OpenCV has some method?

edit retag flag offensive close merge delete


You could convert the rect into two triangles and then check to see if the point is inside the triangles.

GrumbleLion gravatar imageGrumbleLion ( 2014-03-22 11:22:17 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2018-05-03 05:33:39 -0500

updated 2018-05-03 05:41:51 -0500

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 DoesRectangleContainPoint(RotatedRect rectangle, Point2f point) {

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

    //Convert the point array to a vector.
    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;
edit flag offensive delete link more


Keep in mind that indicator == 0 when the point is on the rectangle edge.

Björn Larsson gravatar imageBjörn Larsson ( 2018-05-03 05:36:17 -0500 )edit

the code works,thanks

jsxyhelu gravatar imagejsxyhelu ( 2018-10-03 16:24:03 -0500 )edit

answered 2018-05-04 11:34:18 -0500

Hi, you better use the,Use pointPolygonTest function. And be careful with the rows and columns and they are confusing to some extent.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2014-03-20 10:49:48 -0500

Seen: 4,688 times

Last updated: May 03 '18