I have been trying to rectify 2 images with stereoRectify. The cameras are calibrated both intrinsic and extrinsic. However these calibrations are external, they do not come from the function stereoCalibrate. Still with all this known I do not get good rectification result. Am I doing anything wrong? (see code) or is it simply not possible? I have been stuck on this for a while now so help would be greatly appreciated
fx = 1920 / (2 )
fy = 1080 / 2
# Define principal point
px = 1920 / 2
py = 1080 / 2
# Create calibration matrix
K = np.array([[fx, 0, px],
[0, fx, py],
[0, 0, 1]])
filename1 = os.path.join(carla_save_path,'SSLidar_and_camera/sim_'+str(sim_nmber)+'/transformation_matrices/frame_' + str(left_indx) + '/transformation_m_actor_3')
t_m_1 = np.loadtxt(filename1, delimiter=",") #transformation matrix 1: R|T
filename2 = os.path.join(carla_save_path,'SSLidar_and_camera/sim_'+str(sim_nmber)+'/transformation_matrices/frame_' + str(right_indx) + '/transformation_m_actor_3')
t_m_2 = np.loadtxt(filename2, delimiter=",") #transformation matrix 2: R|T
T_1 =t_m_1[0:3,3]
T_2 =t_m_2[0:3,3]
R1 = t_m_1[0:3,0:3] #rotaion maxtrix 1
R2 = t_m_2[0:3,0:3] #rotaion maxtrix 2
R = R1.T @ R2 # rotation from coordinate system 2 to 1
T = R1.T@(T_2-T_1) # translation from coordinate system 2 to 1
img_path_l = os.path.join(carla_save_path,'SSLidar_and_camera/sim_'+str(sim_nmber)+'/camera_frames/'+ str(num_c)+'_' + str(left_indx) +'.png' )
img_path_r = os.path.join(carla_save_path,'SSLidar_and_camera/sim_'+str(sim_nmber)+'/camera_frames/'+ str(num_c)+'_' + str(right_indx) +'.png')
imgL = cv.imread(img_path_l, 0)
imgR = cv.imread(img_path_r, 0)
K1 = K
K2 = K1
dist1 = np.array([0,0,0,0,0])
dist2 = np.array([0,0,0,0,0])
img_size = (1920,1080)
R1,R2,P1,P2,Q,roi_left, roi_right =cv.stereoRectify(K1,dist1,K2,dist2,img_size,R,T)
map_1_x,map_1_y = cv.initUndistortRectifyMap(K1,dist1,R1,P1,img_size,cv.CV_32FC1)
map_2_x,map_2_y = cv.initUndistortRectifyMap(K2,dist1,R2,P2,img_size,cv.CV_32FC1)
imgR = cv2.remap(imgR, map_2_x, map_2_y, cv2.INTER_LANCZOS4, cv2.BORDER_CONSTANT, 0)
imgL = cv2.remap(imgL, map_1_x, map_1_y, cv2.INTER_LANCZOS4, cv2.BORDER_CONSTANT, 0)