A camera file with a range of functions to handle the camera and it's coordinates.
def normalize_screen_coordinates(X, w, h):
"""
Normalize so that [0, w] is mapped to [-1, 1], while preserving the
aspect ratio.
"""
assert X.shape[-1] == 2
return 2*(X/w) - [1, h/w]
def image_coordinates(X, w, h):
"""Return the image coordinates"""
assert X.shape[-1] == 2
# Reverse camera frame normalization
return (X + [1, h/w])*w/2
def world_to_camera_miqus(P, R, T):
"""
Convert points from world to camera coordinates
Args
P: Nx3 3d points in world coordinates
R: 3x3 Camera rotation matrix
T: 3x1 Camera translation parameters
Returns
X_cam: Nx3 3d points in camera coordinates
"""
assert len(P.shape) == 2
assert P.shape[1] == 3
X_cam = R.dot( P.T - T ) # rotate and translate
return X_cam.T
def camera_to_world_miqus(P, R, T):
"""Inverse of world_to_camera_frame
Args
P: Nx3 points in camera coordinates
R: 3x3 Camera rotation matrix
T: 3x1 Camera translation parameters
Returns
X_cam: Nx3 points in world coordinates
"""
assert len(P.shape) == 2
assert P.shape[1] == 3
X_cam = R.T.dot( P.T ) + T # rotate and translate
return X_cam.T