unable to replicate fisheye.stereoRectify results in Python

asked 2019-04-08 15:08:39 -0600

updated 2019-04-09 01:36:11 -0600

berak gravatar image

I don't manage to rectify a fisheye stereo pair in Python, so I have recreated a simple script to replicate what is done in test_fisheye.cpp TEST_F(fisheyeTest, rectify).

I do (I think) the exact same operations and I have taken the _exact same input_ which is given to the stereoRectify method and then checked the output. It does not match. Can anybody help me to discover why?

In particular, in the test the results are

P1 = [552.8721387695351, 0, 316.1239882810687, 0;
 0, 552.8721387695351, 247.7687370495324, 0;
 0, 0, 1, 0]

P2 = [552.8721387695351, 0, 316.1239882810687, -54.88267970005029;
 0, 552.8721387695351, 247.7687370495324, 0;
 0, 0, 1, 0]

while with my script I get

P1 = [248.02709551, 0 608.5711385, 0;
 0, 248.02709551, 385.1740417, 0; 
 0, 0, 1, 0]
P2 = [248.02709551, 0, 608.5711385, -24.62123676;
 0, 248.02709551, 385.1740417, 0; 
 0, 0, 1, 0]

As a result, the rectification is completely wrong.

Here is what you get with test_fisheye.cpp (rectification_AB_000.png)

test fisheye

and here is what I get with my script.

my results

Here is my full python code (very simple)

import cv2
import numpy as np

print cv2.__file__
print cv2.__version__

basePath = '/home/davide/Code/opencv-3.3.1/opencv_extra/testdata/cv/cameracalibration/fisheye/calib-3_stereo_from_JY'

# --- load images

img1 = cv2.imread(basePath+'/left/stereo_pair_000.jpg',0)
img2 = cv2.imread(basePath+'/right/stereo_pair_000.jpg',0)

# --- input

R = np.array([
    [0.9975670008442493, 0.06969827764018387, 0.001492956999132114],
    [-0.06971182516232298, 0.9974824984553177, 0.01299718076641846],
    [-0.0005833173639831654, -0.01306963539388498, 0.9999144185236674]])

t = np.array([-0.0992174, 0.00317418, 0.00018551])

K = np.array([
    [558.478087865323, 0, 620.458515360843],
    [0, 560.506767351568, 381.939424848348],
    [ 0, 0, 1]])

D = np.array([-0.0014613319981768, -0.00329861110580401, 0.00605760088590183, -0.00374209380722371])


# --- do the thing

R1, R2, P1, P2, Q = cv2.fisheye.stereoRectify(K, D, K, D, (1280,800), R, t, flags=cv2.CALIB_ZERO_DISPARITY, balance=1.0, fov_scale=1.1) #

map11, map12 = cv2.initUndistortRectifyMap(K, D, R1, P1, (1280,800), cv2.CV_32FC1);
map21, map22 = cv2.initUndistortRectifyMap(K, D, R2, P2, (1280,800), cv2.CV_32FC1);

undistorted_rectified1 = cv2.remap(img1, map11, map12, cv2.INTER_LINEAR);
undistorted_rectified2 = cv2.remap(img2, map21, map22, cv2.INTER_LINEAR);

# --- output

print 'R1 ', R1
print 'R2 ', R2
print 'P1 ', P1
print 'P2 ', P2

joined = np.concatenate((undistorted_rectified1, undistorted_rectified2), 1);

cv2.namedWindow('rectification', cv2.WINDOW_NORMAL);
cv2.imshow('rectification', joined)

cv2.waitKey()

cv2.imwrite('/tmp/result.png', joined)
edit retag flag offensive close merge delete