Ask Your Question

# Check if a Point is inside a RotatedRect ?

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 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/

( 2014-03-22 11:22:17 -0500 )edit

## 2 answers

Sort by » oldest newest most voted

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;
}

more

## Comments

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

( 2018-05-03 05:36:17 -0500 )edit
1

the code works,thanks

( 2018-10-03 16:24:03 -0500 )edit

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

more

Official site

GitHub

Wiki

Documentation

## Stats

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

Seen: 5,634 times

Last updated: May 03 '18