Skip to content

Instantly share code, notes, and snippets.

@nikstar
Last active March 14, 2018 11:12
Show Gist options
  • Save nikstar/e63ff920e4f0378df931d1b9df3f4fde to your computer and use it in GitHub Desktop.
Save nikstar/e63ff920e4f0378df931d1b9df3f4fde to your computer and use it in GitHub Desktop.
Convert between different ways of describing a camera
import numpy as np
from scipy.linalg import rq
def getKRC(P):
# https://ksimek.github.io/2012/08/14/decompose/
# P = [M|-MC]
M = P[:,:3]
# camera position
C = -np.linalg.inv(M).dot(P[:,3])
# intrinsic matix and extrinsic (rotaition) matrix
K, R = rq(M) # this decomposition is not unique so we make diagonal elements (focal lengths) positive
sign = np.diag(np.sign(np.diag(K)))
K = K.dot(sign)
R = sign.dot(R)
return K, R, C
# todo
#def changeCooordinateSystem(A, transition=T):
def projectionToLookAt(P):
# conversion to a different coordinate system
# todo: extract it from here
t = np.array([[1.0, 0, 0, 0], [0, -1.0, 0, 0], [0, 0, -1.0, 0], [0, 0, 0, 1]])
P = P.dot(t)
K, R, C = getKRC(P)
# position is the camera position in world coordinates
position = C.T
# look_at is the object camera looks at
direction = np.array([0, 0, -1.0]).T
direction = R.dot(direction)
look_at = position + direction
# up is just up
up = np.array([0.0, 1.0, 0.0])
# fov = 2 arctan (x / (2 f))
# note here K[0,2] is already x/2
fov = np.rad2deg(2 * np.arctan2(K[0,2], K[0,0]))
return position, look_at, up, fov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment