OpenCV solvePnPRansac() function
I am working on a 3D model reconstruction form multiple images project.
I have the camera matrix as well as 2D-3D point correspondence. I want to compute the projection matrix.
I used cv.solvePnPRansac()
function to get the projection matrix as follow:
retval, rvec, tvec, inliers = cv.solvePnPRansac(np.ascontiguousarray(np.squeeze(np.array(selectedpts3D)) [:,:-1]).reshape(len(selectedpts3D),1,3), np.ascontiguousarray(np.array(pts)[:,:-1]).reshape(len(pts),1,2), np.array(K_global), np.array([]), flags = cv.SOLVEPNP_ITERATIVE)
rvec_matrix = cv.Rodrigues(rvec)[0]
proj_matrix = np.hstack((rvec_matrix, tvec))
Then I factorize the output projection matrix to get camera matrix, rotation matrix and translation matrix as follow:
def factorize_proj_matrix(P):
""" Factorize the camera matrix into K,R,t as P = K[R|t]. """
# factor first 3*3 part
K1,R = linalg.rq(P[:,:3])
# make diagonal of K positive
T = np.diag(np.sign(np.diag(K1)))
if np.linalg.det(T) < 0:
T[1,1] *= -1
K1 = np.dot(K1,T)
R = np.dot(T,R) # T is its own inverse
t = np.dot(np.linalg.inv(K1),P[:,3])
return K1, R, t.reshape(3,1)
but the resulted camera matrix "K" has different values than the passed camera matrix to solvePnPRansac() function.
passed camera matrix to function is:
[[1.5204e+03 0.0000e+00 3.0232e+02]
[0.0000e+00 1.5259e+03 2.4687e+02]
[0.0000e+00 0.0000e+00 1.0000e+00]]
output camera matrix is:
array([[ 2.43791400e-03, -7.07610743e-21, 3.23838460e-22],
[ 0.00000000e+00, 2.43791400e-03, -2.88994138e-22],
[ 0.00000000e+00, 0.00000000e+00, 2.43791400e-03]])
My question is does cv.solvePnPRansac()
function changes the cameraMatrix. if yes how to enforce it not to change the camera matrix?