Last active
February 18, 2016 21:23
-
-
Save mandeluna/23aa687450750acff9a6 to your computer and use it in GitHub Desktop.
Calculate the distance from a camera to a subject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: UTF-8 -*- | |
# | |
# tilt-distance.py - calculate the horizontal distance to a target given camera pixel offset value | |
# | |
# To find d, the distance of the target from the camera, along the ground | |
# See https://drive.google.com/open?id=0B6e2rOpUwmteMWtOREdHSGdqOTg | |
# | |
# The triangle ΔABC is complementary to ΔDEC | |
# | |
# FOVp = the height of the camera field of view in pixels | |
# hT = the height of the the target (in feet or meters) | |
# ha = AC, the height of the target above the center line (feet or meters) | |
# hp = the image position of the target (in pixels) | |
# da = the distance of the target from the camera, along the center line | |
# | |
# We need to solve the 3 simultaneous equations | |
# | |
# 1) da = ha × FOVp ÷ (2 hp × tan(θ)) | |
# | |
# 2) cos(α) = DE ÷ DC = d ÷ (da - BC) | |
# → d = cos(α) × (da - BC) = cos(α) × (da - ha × tan(α)) | |
# = da cos(α) - ha sin(α) | |
# | |
# 3) tan(α) = EC ÷ DE = EC ÷ d | |
# also, h = EC + AC = d × tan(α) + ha | |
# → ha = h - d × tan(α) | |
import math | |
FOVp = 600 | |
view = 25.5 * math.pi / 180.0 # angle of view converted to radians (θ in comments above) | |
hc = 1.0 # camera height in feet | |
ht = (85) / 12.0 - hc # 7'1" minus camera height | |
tilt = 9.7 * math.pi / 180.0 # tilt angle converted to radians (α in comments above) | |
def distance(hp): | |
fov_adjustment = (FOVp * math.cos(tilt) / 2 / hp / math.tan(view)) - math.sin(tilt) | |
return ht * fov_adjustment / (1 + fov_adjustment * math.tan(tilt)) | |
for hp in range(10, 500, 10): | |
print "hp=%d, distance=%3.1f feet" % (hp, distance(hp)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment