def tensor_pts(self, x0, x1, x2):
'''Calculates tensor from point correspondences.'''
N = len(x0[0])
M = zeros((N*4,27))
for i in range(N):
for j in range(3):
block = zeros((4,9))
block[[0,1,0,1,2,3,2,3,0,2,1,3,0,1,2,3],
[0,1,2,2,3,4,5,5,6,6,7,7,8,8,8,8]] = x0[j,i]
block[:2,6:] *= -x1[0,i]
block[2:,6:] *= -x1[1,i]
block[:2,2] *= -x2[:2,i]
block[2:,5] *= -x2[:2,i]
block[:2,8] *= -x2[:2,i]
block[2:,8] *= -x2[:2,i]
M[i*4:i*4+4, j*9:j*9+9] = block.copy()
V = cv2.SVDecomp(M)[2]
self.T = V[-1,:27].reshape((3,3,3))
return self.T, 1.0
python类SVDecomp()的实例源码
def blob_mean_and_tangent(contour):
moments = cv2.moments(contour)
area = moments['m00']
mean_x = moments['m10'] / area
mean_y = moments['m01'] / area
moments_matrix = np.array([
[moments['mu20'], moments['mu11']],
[moments['mu11'], moments['mu02']]
]) / area
_, svd_u, _ = cv2.SVDecomp(moments_matrix)
center = np.array([mean_x, mean_y])
tangent = svd_u[:, 0].flatten().copy()
return center, tangent
def getEpipoles(self):
'''Returns the epipole/position of camera 1 in image 2 and 3.'''
u = r_[[cv2.SVDecomp(t.T)[2][-1] for t in self.T]]
ei = cv2.SVDecomp(u)[2][-1]
v = r_[[cv2.SVDecomp(t)[2][-1] for t in self.T]]
eii = cv2.SVDecomp(v)[2][-1]
return ei, eii
def H_from_E(E, RandT=False):
'''Returns a 4x4x4 matrix of possible H translations.
Or returns the two rotations and translation vectors when keyword is True.
'''
S,U,V = cv2.SVDecomp(E)
#TIP: Recover E by dot(U,dot(diag(S.flatten()),V))
W = array([[0,-1,0],[1,0,0],[0,0,1]])
R1 = dot(dot(U,W),V)
R2 = dot(dot(U,W.T),V)
if cv2.determinant(R1) < 0:
R1,R2 = -R1,-R2
t1 = U[:,2]
t2 = -t1
if RandT:
return R1, R2, t1, t2
H = zeros((4,4,4))
H[:2,:3,:3] = R1
H[2:,:3,:3] = R2
H[[0,2],:3,3] = t1
H[[1,3],:3,3] = t2
H[:,3,3] = 1
return H
def mtx2rvec(R):
w, u, vt = cv2.SVDecomp(R - np.eye(3))
p = vt[0] + u[:,0]*w[0] # same as np.dot(R, vt[0])
c = np.dot(vt[0], p)
s = np.dot(vt[1], p)
axis = np.cross(vt[0], vt[1])
return axis * np.arctan2(s, c)
def manually_calculate_pose(self, f_mat):
# get essential matrix from the fundamental
# I am assuming that only one calibration matrix is fine here, because
# only one type of camera is being used.
e_mat = self.k_mat.T * f_mat * self.k_mat
singular_values, u_mat, vt = cv2.SVDecomp(e_mat)
# reconstruction from SVD:
# np.dot(u_mat, np.dot(np.diag(singular_values.T[0]), vt))
u_mat = np.matrix(u_mat)
vt = np.matrix(vt)
# from Epipolar Geometry and the Fundamental Matrix 9.13
w_mat = np.matrix([
[0, -1, 0],
[1, 0, 0],
[0, 0, 1]
], np.float32)
R_mat = u_mat * w_mat * vt # HZ 9.19
t_mat = u_mat[:, 2] # get third column of u
# check rotation matrix for validity
if np.linalg.det(R_mat) - 1.0 > 1e-07:
print('{}\nDoes not appear to be a valid rotation matrix'.format(
R_mat
))
camera_matrix = np.column_stack((R_mat, t_mat))
return camera_matrix, R_mat, t_mat
def mtx2rvec(R):
w, u, vt = cv2.SVDecomp(R - np.eye(3))
p = vt[0] + u[:,0]*w[0] # same as np.dot(R, vt[0])
c = np.dot(vt[0], p)
s = np.dot(vt[1], p)
axis = np.cross(vt[0], vt[1])
return axis * np.arctan2(s, c)
def mtx2rvec(R):
w, u, vt = cv2.SVDecomp(R - np.eye(3))
p = vt[0] + u[:,0]*w[0] # same as np.dot(R, vt[0])
c = np.dot(vt[0], p)
s = np.dot(vt[1], p)
axis = np.cross(vt[0], vt[1])
return axis * np.arctan2(s, c)
def mtx2rvec(R):
w, u, vt = cv2.SVDecomp(R - np.eye(3))
p = vt[0] + u[:,0]*w[0] # same as np.dot(R, vt[0])
c = np.dot(vt[0], p)
s = np.dot(vt[1], p)
axis = np.cross(vt[0], vt[1])
return axis * np.arctan2(s, c)