Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why both stereoRectify-workflows give different result?

I get different result in both workflows, which should be equal (in my opinion).

  1. First workflow: First i remove the distortion in the images, and then in the following functions in the workflow i use zero-distortion (e.g. Mat() as parameter).
  2. Second workflow: I don't remove the distortion, but instead of it I use the distortion coefficients in the following functions (in stereoRectify() and in initUndistortRectifyMap()).

First workflow (with inital undistortion):

undistort(image1, image1, camera_matrix1, distCoeffs1);
undistort(image2, image2, camera_matrix2, distCoeffs2);
....
F = findFundamentalMat(image_points1, image_points2, CV_FM_RANSAC);
E = findEssentialMat(image_points1, image_points2, camera_matrix1, RANSAC);
recoverPose(E, image_points1, image_points2, camera_matrix1, R, T);

stereoRectify(camera_matrix1, Mat(), camera_matrix2, Mat(), image1.size(), R, T, R1, R2, Proj1, Proj2, Q);

Mat mapx1, mapy1;
initUndistortRectifyMap(camera_matrix1, Mat(), R1, Proj1, image1.size(), CV_16SC2, mapx1, mapy1);
remap(image1, image1_rectified, mapx1, mapy1, INTER_LINEAR);

Mat mapx2, mapy2;
initUndistortRectifyMap(camera_matrix2, Mat(), R2, Proj2, image2.size(), CV_16SC2, mapx2, mapy2);
remap(image2, image2_rectified, mapx2, mapy2, INTER_LINEAR);

Resulting disparity map: image description

Second workflow (without inital undistiortion):

F = findFundamentalMat(image_points1, image_points2, CV_FM_RANSAC);
E = findEssentialMat(image_points1, image_points2, camera_matrix1, RANSAC);
recoverPose(E, image_points1, image_points2, camera_matrix1, R, T);

stereoRectify(camera_matrix1, distCoeffs1, camera_matrix2, distCoeffs2, image1.size(), R, T, R1, R2, Proj1, Proj2, Q);

Mat mapx1, mapy1;
initUndistortRectifyMap(camera_matrix1, distCoeffs1, R1, Proj1, image1.size(), CV_16SC2, mapx1, mapy1);
remap(image1, image1_rectified, mapx1, mapy1, INTER_LINEAR);

Mat mapx2, mapy2;
initUndistortRectifyMap(camera_matrix2, distCoeffs2, R2, Proj2, image2.size(), CV_16SC2, mapx2, mapy2);
remap(image2, image2_rectified, mapx2, mapy2, INTER_LINEAR);

Resulting disparity map: image description

Then i use these rectified images to calculate the disparity map, but i get different results in both workflows.
The second workflow looks like it gives better results (at least on the wall on the right side).
I would expect that both workflows do get the same results...

Why both stereoRectify-workflows give different result?

I get different result in both workflows, which should be equal (in my opinion).

  1. First workflow: First i remove the distortion in the images, and then in the following functions in the workflow i use zero-distortion (e.g. Mat() as parameter).
  2. Second workflow: I don't remove the distortion, but instead of it I use the distortion coefficients in the following functions (in stereoRectify() and in initUndistortRectifyMap()).

First workflow (with inital undistortion):

undistort(image1, image1, camera_matrix1, distCoeffs1);
undistort(image2, image2, camera_matrix2, distCoeffs2);
....
F = findFundamentalMat(image_points1, image_points2, CV_FM_RANSAC);
E = findEssentialMat(image_points1, image_points2, camera_matrix1, RANSAC);
recoverPose(E, image_points1, image_points2, camera_matrix1, R, T);

stereoRectify(camera_matrix1, Mat(), camera_matrix2, Mat(), image1.size(), R, T, R1, R2, Proj1, Proj2, Q);

Mat mapx1, mapy1;
initUndistortRectifyMap(camera_matrix1, Mat(), R1, Proj1, image1.size(), CV_16SC2, mapx1, mapy1);
remap(image1, image1_rectified, mapx1, mapy1, INTER_LINEAR);

Mat mapx2, mapy2;
initUndistortRectifyMap(camera_matrix2, Mat(), R2, Proj2, image2.size(), CV_16SC2, mapx2, mapy2);
remap(image2, image2_rectified, mapx2, mapy2, INTER_LINEAR);

Resulting disparity map: image description

Second workflow (without inital undistiortion):

F = findFundamentalMat(image_points1, image_points2, CV_FM_RANSAC);
E = findEssentialMat(image_points1, image_points2, camera_matrix1, RANSAC);
recoverPose(E, image_points1, image_points2, camera_matrix1, R, T);

stereoRectify(camera_matrix1, distCoeffs1, camera_matrix2, distCoeffs2, image1.size(), R, T, R1, R2, Proj1, Proj2, Q);

Mat mapx1, mapy1;
initUndistortRectifyMap(camera_matrix1, distCoeffs1, R1, Proj1, image1.size(), CV_16SC2, mapx1, mapy1);
remap(image1, image1_rectified, mapx1, mapy1, INTER_LINEAR);

Mat mapx2, mapy2;
initUndistortRectifyMap(camera_matrix2, distCoeffs2, R2, Proj2, image2.size(), CV_16SC2, mapx2, mapy2);
remap(image2, image2_rectified, mapx2, mapy2, INTER_LINEAR);

Resulting disparity map: image description

Then i use these rectified images to calculate the disparity map, but i get different results in both workflows.
The second workflow looks like it gives better results (at least on the wall on the right side).
I would expect that both workflows do get the same results...