Skip to content

Instantly share code, notes, and snippets.

@YoshiRi
Created August 30, 2017 03:04
Show Gist options
  • Save YoshiRi/ad770c56be081a17b8ef7308442c4964 to your computer and use it in GitHub Desktop.
Save YoshiRi/ad770c56be081a17b8ef7308442c4964 to your computer and use it in GitHub Desktop.
two single camera calibration file to get rectification and new projection matrix
import numpy as np
#D1 = [-0.35002811852321858, 0.096534651810849512, 0.0, 0.0, 0.000000]
#D2 = [-0.36311895125549826, 0.10890311353734204, 0.0, 0.0, 0.000000]
K1 = np.array([ 439.65213495954725,0,306.67821191917398,0,436.73222033708959,245.79237261646784,0,0,1]).reshape(3,3)
K2 = np.array([ 437.00346198880374,0,324.72266880648021,0,434.00126234994462,236.08291224903141,0,0,1]).reshape(3,3)
R1 = np.array([1.0,0,0, 0,1.0,0, 0,0,1.0 ]).reshape(3,3)
R2 = np.array([0.9999669439878518,-0.001850056106487306,-0.0079175895321455282, 0.0018719784959910215,0.99999443266408283,0.002762306527137064, 0.007912435030206437,-0.0027770367736432208,0.99996484010121622 ]).reshape(3,3)
t1 = np.array([0.0,0.0,0.0])
t2 = np.array([0.063807236626602593,3.3642187371071906e-05,0.00083301238870786727])
# get center = -R*t
c1 = -np.dot(R1.T,t1)
c2 = -np.dot(R2.T,t2)
# get vector
v1 = (c1-c2)
v2 = np.cross(R1[2,:],v1)
v3 = np.cross(v1,v2)
# get new rotation
R = np.zeros((3,3))
R[:,0] = v1/np.linalg.norm(v1)
R[:,1] = v2/np.linalg.norm(v2)
R[:,2] = v3/np.linalg.norm(v3)
# new projection matrix
K = (K1+K2)/2
P1 = np.zeros((3,4))
P2 = np.zeros((3,4))
P1[:3,:3] = R
P1[:,3] = -np.dot(R,c1)
P1 = np.dot(K,P1)
P2[:3,:3] = R
P2[:,3] = -np.dot(R,c2)
P2 = np.dot(K,P2)
print(P1.reshape(12).tolist())
print(P2.reshape(12).tolist())
# rectification matrix
# https://stackoverflow.com/questions/11838352/multiply-several-matrices-in-numpy
# transformation in normalized image plane
T1 = reduce(np.dot, [P1[:3,:3],R1.T,np.linalg.inv(K1)])
T2 = reduce(np.dot, [P2[:3,:3],R2.T,np.linalg.inv(K2)])
print(T1.reshape(9).tolist())
print(T2.reshape(9).tolist())
# transformation in picel coordinate ( may be this is the rectification matrix )
TT1 = np.dot(R,R1.T)
TT2 = np.dot(R,R2.T)
print(TT1.reshape(9).tolist())
print(TT2.reshape(9).tolist())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment