How can I determine the lengths of each side in a contour?

I am using Python-OpenCV to detect shapes, measure their distance from the camera lens, and eventually measure the angle of the rotation of the shapes with respect to the camera.

This is what it looks like when the shapes are parallel to the camera:
(Imgur: The magic of the Internet)

This is what it looks like when the shapes are slightly tilted/rotated:
(Imgur: The magic of the Internet)

I am trying to determine the angle of rotation of the board on which the shapes are placed, which is why I came up with the idea of comparing the lengths of the parallel sides of the rectangle to each other (any other Idea or tip is greatly appreciated).

Required Lengths:

The idea is that when the board is rotated, the parallel sides are no longer of equal length and I might be able to come up with a relation between the difference in length and the angle of rotation. I am open to any other suggestions.

I am struggling with calculating those lengths as shown in the picture above. I am trying to avoid using Hough Lines, and stick to Contours, since they proved to be a bit unstable in a live video.

Thank you in advance!

use cv.approxPolyDP()

and after that you might want to browse the calib3d module and its tutorials. when you have the true dimensions (2D point coordinates on the surface) or your rectangles and triangles, and if you can match the corners of the shapes from the picture to that model, you can calculate a pose matrix.

related: