# Square's angle of rotation using 4 corners

I need to calculate angle of rotation of an image from X and Y axis, what I'll have is 4 co-ordinates of the corners of the image (basically a perfect square image is rotated in all possible angle), using this co-ordinates I need to calculate the angle of rotation in X and Y axis.

For example if four corner makes a perfect square then the angle of rotation from all axis is 0, lets say if the top edge is shorter than the bottom edge or vice versa then it is rotated along X axis, or if the right edge is shorter that the left edge or vice versa then it is rotated along Y axis, or the combination of above 2 examples with rotation on X and Y axis.

And what if I got the angles of 4 corners of the square image, lets say if the 4 corners of the image is equal to 90 degree, means the rotation along X and Y axis are 0 degrees which is a perfect square. In the above image, I need to calculate the angle of the Object based on the Glass screen.

Can someone guide me how to do calculation?

• I do image processing by I am unable to understand your question. Maybe take a photo of an example (even hand drawn) and insert it by clicking on the (middle) icone looking like a mountain range. – Jean Marie May 9 '16 at 12:02
• @JeanMarie I don't get a sample image and I can't create one, have a look at this link here the image is rotated in Z-axis (all the edges will be of same length even after the image is rotated) where as in my case the image will be rotated either in X (Horizantal center) or Y (Vertical center) axis in which all the edges will not be same. Consider seeing the 2D image in 3D view. – Vignesh May 9 '16 at 12:51
• It seems to me that there is a fundamental ambiguity: if I see a parallelogram (it will be the general form of the image of a square), I am unable to say is it results from an X-axis rotation or a Y-axis rotation... – Jean Marie May 9 '16 at 12:57
• @JeanMarie Thanks for your response, I have updated my question a bit. Here is a link of a image for my example, there u can see a square object, if you see from a glass screen it will not be a perfect square, I need to calculate the angle of the object with respect to the glass screen – Vignesh May 10 '16 at 5:22

You might as well write down the equations. You have 4 points which define 4 "ray" along which the original vertices lie:

$$\vec{r}_A(t_A)=E'+(A'-E')t_A$$ where $t_A$ is unknown. Same for all four points.

Your unknowns are the three parameters of the plane in which the square lies: the normalized normal and the distance from the coordinate origin can be combined into a single nonnormalized vector $\vec{n}$. Let the plane be:

$$(\vec{r}-E')\cdot \vec{n}=1$$ The normal $\vec{n}$ encodes the orientation you're looking for. The distance (inverse magnitude of $\vec{n}$) you probably won't be able to get accurately, because once the square is far enough so that parallax distortion isn't noticeable, the shape is independent of the distance, the problem becomes degenerate.

Ray-plane intersections become $$t_A=\frac{1}{(A'-E')\cdot \vec{n}}$$ $$A=E'+\frac{A'-E'}{(A'-E')\cdot \vec{n}}$$ and the same for $B$, $C$, $D$.

Now you have to solve the equations that ensure this to be a square. Try $$B-A=C-D$$ $$(A-B)\cdot (C-A)=0$$ $$(A-C)\cdot(B-D)=0$$ I deliberately avoided the absolute values as conditions for a square. Note that not all of these 5 equations are independent. Now the question is well posed, you just find a way to solve this system. I'd try numerically - minimization or something.

• The image I posted is just I downloaded for reference, there is nothing to do with E'. What I've is Angles of the 4 corners A', B', C' and D' from this I need to find the angle of rotation of the object with respect to the glass screen. For example if the Angles of 4 corners of the object in the glass screen is 90 degree means, the angle of rotation is 0 degree – Vignesh May 10 '16 at 8:23
• Well you do need to know from where you were looking, one way or another. The projection depends on your viewing direction. So think about what that means in your case. And note that rotations in 3D don't commute, so specifying "rotation in X and Y direction" is quite an ugly way of thinking about this. – orion May 10 '16 at 8:26
• Actually the image will be captured from a Mobile Phone using its Camera, then I got the 4 corners (points) of the object and 4 angles of corners, from this values I need to calculate rotation along X and Y axis of the object with reference to the Mobile screen – Vignesh May 10 '16 at 8:31
• In that case, your E' is a point symmetrically behind the center of the image, and its distance to the image can be determined from the field of view angle of your camera. – orion May 10 '16 at 8:41
• Thanks, I don't understand those mathematic symbols because I'm not from mathematic background, I'll try your answer and will let you know. – Vignesh May 10 '16 at 9:34