如何计算相机到目标的距离? (在 Python 中)

How to calculate distance from camera to target? (in Python)

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

距离=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(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)

cos 函数以弧度而不是度数作为参数。

改变

return CH / math.cos(CA)

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