【问题标题】:How can I determine the lengths of each side in a contour?如何确定轮廓中每一边的长度?
【发布时间】:2021-10-07 03:20:10
【问题描述】:

我正在使用 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

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

如上图所示,我正在努力计算这些长度。我试图避免使用霍夫线,并坚持使用等高线,因为事实证明它们在现场视频中有点不稳定。

提前谢谢你!

【问题讨论】:

标签: python opencv contour


【解决方案1】:

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

【讨论】:

  • 谢谢,但是我怎么能看到这些点呢?如,(x1, y1) (x2, y2) (x3, y3) (x4, y4)。如果我能得到每个点的坐标,那么我可以减去它们得到长度。
  • 谢谢!这有很大帮助。有没有办法分开x和y?假设点“a”和“c”是矩形的左上角和右上角,我希望能够减去 x 值以获得“ac”的长度。我是 python 新手,所以请不要评判我的业余问题:p。再次感谢你! @阿里
  • 对于每个点 x = pa[0][0]y = pa[0][1]
  • 如何对矩形做同样的事情?我必须在上面的代码中进行哪些更改才能使其正常工作?提前致谢。 @阿里
猜你喜欢
  • 2021-06-15
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多