【问题标题】:How to calculate distance from camera to target? (in Python)如何计算相机到目标的距离? (在 Python 中)
【发布时间】:2018-09-10 00:34:04
【问题描述】:

我正在尝试使用此公式计算相机与地面之间的距离。

距离 = D

相机高度 = CH(米)

相机角度 = CA

D = CH/cos(CA)

所以在代码中我这样做是为了计算距离

def findDistance(CH, CA):
    return CH / math.cos(CA)

#for test
cameraHight = 1.2 #In meter
cameraAngle = 65   #Degress angle
estimatedDistance = findDistance(cameraHight, cameraAngle)
print(estimatedDistance)

然后给我这个-2.1335083711460943。我不认为答案应该是否定的。到目标的距离大约是正确的,但不是负 2 米。

任何关于如何更好地做到这一点或我做错了什么的建议将不胜感激。 谢谢

【问题讨论】:

  • 你为什么不认为答案不应该是否定的?您使用 cos() 可以返回负值。如果您希望答案是肯定的,请使用 CH/cos(CA) 的绝对值。
  • 我应该总是取这个的绝对值吗?如果是,为什么?我真的很想明白这一点

标签: python-3.x distance trigonometry


【解决方案1】:

cos(65 度) = 0.42261826174, cos(65 弧度) = -0.56245385123。

根据文档:

math.cos(x)
Return the cosine of x radians.

您需要先将度数转换为弧度。

cameraAngle = 65
cameraRadians = math.radians(cameraAngle)

然后在计算中使用cameraRadians,而不是cameraAngle。

完整:

def findDistance(CH, CA):
    return CH / math.cos(CA)

#for test
cameraHight = 1.2 #In meter
cameraAngle = 65   #Degress angle
cameraRadians = math.radians(cameraAngle) #convert degrees to radians
estimatedDistance = findDistance(cameraHight, cameraRadians)
print(estimatedDistance)

【讨论】:

    【解决方案2】:

    cos 函数采用弧度而不是度数。

    改变

    return CH / math.cos(CA)
    

    return CH / math.cos(math.radians(CA))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      相关资源
      最近更新 更多