Detect the angle of a surface in real-time

My project is about image detection. I am using 1 camera and have 2 shapes (rectangle and triangle) placed on a flat surface. I have successfully been able to:

  • Detect the contours
  • Detect the area of the contours
  • Make a relation between both areas and the distances, to be able to calculate both distances from the camera
  • Calculate the lengths, corner coordinates and the angle of each corner in both shapes

Now I need to calculate the angle of rotation of the whole surface when rotated around the x-axis (up & down), y-axis (left & right) and both axes (oblique). This can be seen in the gif I provided. The default angle (0 degrees) is when the surface is parallel to the camera, and any rotation would mean that an angle around 1 or both axes is present.


P.S When the surface is rotating around both axes (oblique), both angles have to be detected separately. For example, I would get 14° x-axis & 27° y-axis.

Any help would be appreciated.


Mate, you are looking for the decomposeHomographyMat function.

First, compute the camera matrix using a checkerboard pattern (see here).
Then, get the homography matrix using feature matching between the recerence image (at 0°) and the camera image; see this tutorial.
Finally use decomposeHomographyMat to get the translation and the rotations of the camera image.

Simple like that.

See also Demo 4 in this page.

P.S. If you are not bound by a predefined image type (square and triangle), you can use an ArUCo marker to get directly the angle of the image. It might even be more robust (as it has more corners.