Skip to content

Instantly share code, notes, and snippets.

@envil
Last active May 1, 2019 20:49
Show Gist options
  • Save envil/5b980251c93a5271687678317efc2f68 to your computer and use it in GitHub Desktop.
Save envil/5b980251c93a5271687678317efc2f68 to your computer and use it in GitHub Desktop.
Utility function to convert between pixel to angle and vice versa in Eye Tracking. Ported from matlab code. Original article here: https://courses.washington.edu/matlab1/matlab/
import math
class Display:
def __init__(self, dist, width, resolution):
self.distance = dist
self.width = width
self.resolution = resolution
# Credit to gmb zre 11/1/07, ported to python by Viet Ta
def pix2angle(display, pix):
pix_size = display.size / display.resolution[0] # cm/pix
sz = pix * pix_size
return 2 * math.degrees(math.atan(sz / (2 * display.distance)))
# Credit to gmb zre 11/1/07, ported to python by Viet Ta
def angle2pix(display, ang):
pix_size = display.size / display.resolution[0] # cm/pix
sz = 2 * display.distance * math.tan(math.radians(ang / 2)) # cm
return round(sz / pix_size)
display = Display(60, 44.5, [1680, 1050])
pix = 100
ang = 2.529
print(pix2angle(display, pix))
print(angle2pix(display, ang))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment