I have two images from my stereo camera model is ZED 1 from stereoLabs. I want to rectify those images, This is the code I used to try that :
import numpy as np
import cv2
# Load the calibration parameters
left_camera_matrix = np.loadtxt('left_camera_matrix.txt')
left_distortion_coefficients = np.loadtxt('left_distortion_coefficients.txt')
right_camera_matrix = np.loadtxt('right_camera_matrix.txt')
right_distortion_coefficients = np.loadtxt('right_distortion_coefficients.txt')
rotation_matrix = np.loadtxt('rotation_matrix.txt')
translation_vector = np.loadtxt('translation_matrix.txt')
# Set up the rectification parameters
image_size = (1280,720) # replace with the size of your images
R1, R2, P1, P2, Q,roi1,roi2 = cv2.stereoRectify(left_camera_matrix, left_distortion_coefficients,
right_camera_matrix, right_distortion_coefficients,
image_size, rotation_matrix, translation_vector,1,(0,0))
# the output of stereoRectify is a tuple of 3 elements (R1, R2, P1, P2, Q)
#R1, R2, P1, P2, Q = rectify_parameters[:5]
left_map1, left_map2 = cv2.initUndistortRectifyMap(left_camera_matrix, left_distortion_coefficients, R1, P1, image_size, cv2.CV_16SC2)
right_map1, right_map2 = cv2.initUndistortRectifyMap(right_camera_matrix, right_distortion_coefficients, R2, P2, image_size, cv2.CV_16SC2)
# Load the stereo images
left_image = cv2.imread('right2.png')
right_image = cv2.imread('left2.png')
# Rectify the stereo images
left_rectified = cv2.remap(left_image, left_map1, left_map2, cv2.INTER_LANCZOS4)
right_rectified = cv2.remap(right_image, right_map1, right_map2, cv2.INTER_LANCZOS4)
# Display the rectified images (optional)
cv2.imshow('Left Rectified', left_rectified)
cv2.imshow('Right Rectified', right_rectified)
cv2.imwrite('left_rectified.png', left_rectified)
cv2.imwrite('right_rectified.png', right_rectified)
cv2.waitKey(0)
cv2.destroyAllWindows()
I thought i would get two proper images but i get two very wrong outputs , with one image being projected towards the right side and the other one is almost completely black, These are my input images and the outputs:left imageright imageLeft rectified image right rectified image,
These are my intrinsic and extrinsic parameters for the stereo camera:
right_camera_matrix: 1399.700000 0.000000 991.780000
0.000000 1399.700000 524.748000
0.000000 0.000000 1.000000
Left_camera_matrix: 1400.340000 0.000000 976.990000
0.000000 1400.340000 534.371000
0.000000 0.000000 1.000000
format for the above matrix : [[fx, 0, cx],
[0, fy, cy],
[0, 0, 1]])
right_distortion: -0.172000
0.025300
0.000000
0.000000
0.000000
left_distortion: -0.170500
0.023500
0.000000
0.000000
0.000000
format for the distortion array : = D = np.array([k1, k2, p1, p2, k3])
extrinsic parameters :
# Define the rotation and translation matrices
Tx = 120.000
Ty = 0.0000
Tz = 0.0000
Rx = 0.0028
Ry = 0.0145
Rz = 0.0005
# Compute the rotation matrix from Euler angles
theta = np.sqrt(Rx**2 + Ry**2 + Rz**2)
rvec = np.array([Rx, Ry, Rz]) / theta
R, _ = cv2.Rodrigues(rvec)
# Define the translation matrix
T = np.array([[Tx],
[Ty],
[Tz]])
# Save the rotation matrix to file
np.savetxt('rotation_matrix.txt', R, fmt='%.6f')
# Save the translation matrix to file
np.savetxt('translation_matrix.txt', T, fmt='%.6f')
I tried changing the parameters and the output changes however, i dont know why i am not getting proper images even though i got the parameters from Stereo Labs builtin image viewer but still output is wrong, so any idea what i am doing wrong