如何确定轮廓中每条边的长度?

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

我正在使用 Python-OpenCV 来检测形状,测量它们与相机镜头的距离,并最终测量形状相对于相机的旋转角度。

This is what it looks like when the shapes are parallel to the camera

This is what it looks like when the shapes are slightly tilted/rotated

我试图确定放置形状的板的旋转角度,这就是为什么我想出将矩形的平行边的长度相互比较(任何其他非常感谢想法或提示)。 Required Lengths

我的想法是,当板子旋转时,平行边的长度不再相等,我或许可以想出长度差和旋转角度之间的关系。我愿意接受任何其他建议。

我正在努力计算如上图所示的那些长度。我试图避免使用 Hough Lines,并坚持使用 Contours,因为事实证明它们在实时视频中有点不稳定。

提前致谢!

此代码将为您提供 4 个角。

p = cv2.arcLength(cnt, True) # cnt is the rect Contours
appr = cv2.approxPolyDP(cnt, 0.02*p, True) # appr contains the 4 points

appr = sorted(appr, key=lambda c: c[0][0])

#pa = top lef point
#pb = bottom left point
#pc = top right point
#pd = bottom right point

pa, pb = sorted(appr[:2], key=lambda c: c[0][1])
pc, pd = sorted(appr[2:], key=lambda c: c[0][1])

# the points are x, y in list of list [[x, y]]