3
votes

I know how to find minimum bounding box using regionprops() in Matlab and boundingRect() using OpenCV. How to find a bounding box of points (not minimum bounding box) given the angle of orientation? For example in image below where the line is the major axis of the rectangle. enter image description here

2

2 Answers

1
votes

Well, there are many possible solutions. I'll mention three of these:

  1. rotate all points so that the red line becomes parallel to the x (or y) axis; then, find the minimum axis oriented bounding box of transformed points; then, rotate back the box to find the desired one.

  2. find the projection of every point on the red line and on a blue line (perpendicular to the red one), storing minimum and maximum values per coordinate.

  3. find the distance of all points from the red line; then, with some consideration you should easily find two coords of the box; then you can repeat the computation taking a line perpendicular to the red one to find the missing two coords for the box

1
votes

You can transform the points such that axis matches with the x-axis or y-axis. Find the bounding box of the transformed points and transform the bounding box back to original space by inverse transform.
Details of transformation : https://en.wikipedia.org/wiki/Rotation_(mathematics) You can find the bounding box of transformed points like this.

double xmin = 1e+37,ymin = 1e+37;
double xmax = -1e+37,ymax = -1e+37;
for(size_t i = 0; i < numPoints; ++i)
{
  if( x[i] >= xmin ) xmin = x[i];
  if( y[i] >= ymin ) ymin = y[i];
  if( x[i] <= xmax ) xmax = x[i];
  if( y[i] <= ymax ) ymax = y[i];
}

You can replace 1e+37 with any large number or you use the BIG FLOAT

More details of oriented bounding box implementation of Stefan Gottschalk idea can also be found at this blog Oriented bounding box