Josep Bosch Josep Bosch - 1 month ago 18
Python Question

Opencv: Computing fundamental matrix from R and T

I want to compute the epipolar lines of a stereo camera.
I know both camera intrinsics matrix as well as R and T.
I tried to compute the essential matrix as told in Learning Opencv book and wikipedia.

Essential Matrix

where [t]x is the matrix representation of the cross product with t.

Fundamental matrix so

enter image description here

I tried to implement this with python and then use the opencv function cv2.computeCorrespondEpilines to compute the epilines.

Bad epilines

The problem is that the lines I get don't converge in a point as they should...
I guess I must have a problem computing F.

This is the relevant pice of code:

T #Contains translation vector
R #Rotation matrix
S=np.mat([[0,-T[2],T[1]],[T[2],0,-T[1]],[-T[1],T[0],0]])
E=np.mat(R)*S

M1=np.mat(self.getCameraMatrix(cam1))
M1_inv=np.linalg.inv(M1)
M2=np.mat(self.getCameraMatrix(cam2))
M2_inv=np.linalg.inv(M2)

F=(M2_inv.T)*E*M1_inv


The matrices are:

M1=[[ 776.21275864 0. 773.70733324]
[ 0. 776.21275864 627.82872456]
[ 0. 0. 1. ]]

M2=[[ 764.35675708 0. 831.26052677]
[ 0. 764.35675708 611.85363745]
[ 0. 0. 1. ]]

R=[[ 0.9999902 0.00322032 0.00303674]
[-0.00387935 0.30727176 0.9516139 ]
[ 0.0021314 -0.95161636 0.30728124]]

T=[ 0.0001648 0.04149158 -0.02854541]


The ouput F I get it's something like:

F=[[ 4.75910592e-07 6.28777619e-08 -2.78886982e-04]
[ -4.66942275e-08 -7.62837993e-08 -7.34825205e-04]
[ -8.86965149e-04 -6.86717269e-04 1.40633035e+00]]


EDITED:
The cross multiplication matrix was wrong, it has to be:
S=np.mat([[0,-T2,T1],[T2,0,-T[0]],[-T1,T[0],0]])

The epilines converge now at the epipole.Eipole ok

Answer

Hum, your F matrix seems wrong - to begin with, the rank is closer to 3 than 2. From your data I get:

octave:9> tx = [ 0 -T(3) T(2)
> T(3) 0 -T(1)
> -T(2) T(1) 0]
tx =

   0.000000   0.028545   0.041492
  -0.028545   0.000000  -0.000165
  -0.041492   0.000165   0.000000

octave:11> E= R* tx
E =

  -2.1792e-04   2.8546e-02   4.1491e-02
  -4.8255e-02   4.6088e-05  -2.1160e-04
   1.4415e-02   1.1148e-04   2.4526e-04

octave:12> F=inv(M1')*E*inv(M2)
F =

  -3.6731e-10   4.8113e-08   2.4320e-05
  -8.1333e-08   7.7681e-11   6.7289e-05
   7.0206e-05  -3.7128e-05  -7.6583e-02

octave:14> rank(F)
ans =  2

Which seems to make more sense. Can you try that F matrix in your plotting code?