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

时间:2021-08-01 12:10:18

标签: python opencv 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,因为事实证明它们在直播视频中有点不稳定。

先谢谢你!

1 个答案:

答案 0 :(得分:3)

此代码将为您提供 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]]