Last active
May 1, 2019 20:49
-
-
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/
This file contains hidden or 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
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