Ask Your Question
2

Check if a Point is inside a RotatedRect ?

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

yes123 gravatar image

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

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

I have a Point

  Point2f(15,20);

and a RotatedRect

  RotatedRect(center,size,angle);

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

edit retag flag offensive close merge delete

Comments

You could convert the rect into two triangles and then check to see if the point is inside the triangles. http://www.blackpawn.com/texts/pointinpoly/

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

2 answers

Sort by » oldest newest most voted
2

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

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

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];
    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;
}
edit flag offensive delete link more

Comments

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 -0600 )edit
1

the code works,thanks

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

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

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

Question Tools

1 follower

Stats

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

Seen: 7,938 times

Last updated: May 03 '18