Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate rotated rectangle size from known bounding box coordinates

Tags:

I read the Calculate Bounding box coordinates from a rotated rectangle to know how to calculate bounding box coordinates from a rotated rectangle. But in a special case as follow image:

http://i.stack.imgur.com/3UNfD.png

How to get the rotated rectangle size if had get the bounding box size, coordinates and rotate degree?

I try write code in javascript

//assume w=123,h=98,deg=35 and get calculate box size var deg = 35; var bw = 156.9661922099485; var bh = 150.82680201149986;  //calculate w and h var xMax = bw / 2; var yMax = bh / 2; var radian = (deg / 180) * Math.PI; var cosine = Math.cos(radian); var sine = Math.sin(radian); var cx = (xMax * cosine) + (yMax * sine)   / (cosine * cosine + sine * sine); var cy =  -(-(xMax * sine)  - (yMax * cosine) / (cosine * cosine + sine * sine)); var w = (cx * 2 - bw)*2; var h = (cy * 2 - bh)*2; 

But...the answer is not match w and h

like image 329
Liao San Kai Avatar asked Apr 02 '12 05:04

Liao San Kai


People also ask

Can bounding boxes be rotated?

Figure 7. Bounding boxes are constructed by first creating an axis-aligned box (left), and then rotating by theta (right). Many datasets (for example, COCO and ISPRS) come with segmentation masks. These masks can be converted into rotated bounding boxes by using a geometry package.

How do you find the bounding box of a rectangle?

The bottom of the rectangle is determined by the y-coordinate of the lowest point - point D. The left side of the rectangle is determined by the x-coordinate of the leftmost point - point A. The right of the rectangle is determined by the x-coordinate of the rightmost point - point C.

What is a rotated rectangle?

A rectangle is an example of a shape with rotation symmetry. A rectangle can be rotated about its center and it will look exactly the same and be in the same location. The only difference is the location of the named points. A rectangle has half-turn symmetry, and therefore is order 2.


1 Answers

enter image description here

Solution

Given bounding box dimensions bx by by and t being the anticlockwise rotation of rectangle sized x by y:

x = (1/(cos(t)^2-sin(t)^2)) * (  bx * cos(t) - by * sin(t)) y = (1/(cos(t)^2-sin(t)^2)) * (- bx * sin(t) + by * cos(t)) 

Derivation

Why is this?

First, consider that the length bx is cut in two pieces, a and b, by the corner of the rectangle. Use trigonometry to express bx in terms of x, y, and theta:

bx = b          + a bx = x * cos(t) + y * sin(t)            [1] 

and similarly for by:

by = c          + d by = x * sin(t) + y * cos(t)            [2] 

1 and 2 can be expressed in matrix form as:

[ bx ] = [ cos(t)  sin(t) ] * [ x ]     [3] [ by ]   [ sin(t)  cos(t) ]   [ y ] 

Note that the matrix is nearly a rotation matrix (but not quite - it's off by a minus sign.)

Left-divide the matrix on both sides, giving:

[ x ] = inverse ( [ cos(t)  sin(t) ]    * [ bx ]                        [4] [ y ]             [ sin(t)  cos(t) ] )    [ by ] 

The matrix inverse is easy to evaluate for a 2x2 matrix and expands to:

[ x ] = (1/(cos(t)^2-sin(t)^2)) * [ cos(t) -sin(t) ] * [ bx ]           [5] [ y ]                             [-sin(t)  cos(t) ]   [ by ] 

[5] gives the two formulas:

x = (1/(cos(t)^2-sin(t)^2)) * (  bx * cos(t) - by * sin(t))             [6] y = (1/(cos(t)^2-sin(t)^2)) * (- bx * sin(t) + by * cos(t)) 

Easy as pie!

like image 170
Li-aung Yip Avatar answered Sep 20 '22 18:09

Li-aung Yip