Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Decomposition of essential matrix leads to wrong rotation and translation

Hi, I am doing some SfM and having troubles getting R and T from the essential matrix.

Here is what I am doing in sourcecode:

            Mat fundamental = Calib3d.findFundamentalMat(object_left, object_right);
    Mat E = new Mat();

    Core.multiply(cameraMatrix.t(), fundamental, E); // cameraMatrix.t()*fundamental*cameraMatrix;
    Core.multiply(E, cameraMatrix, E);

    Mat R = new Mat();
    Mat.zeros(3, 3, CvType.CV_64FC1).copyTo(R);

    Mat T = new Mat();

    calculateRT(E, R, T);

private void calculateRT(Mat E, Mat R, Mat T){

    /*
     * //-- Step 6: calculate Rotation Matrix and Translation Vector
        Matx34d P;
        //decompose E 
        SVD svd(E,SVD::MODIFY_A);
        Mat svd_u = svd.u;
        Mat svd_vt = svd.vt;
        Mat svd_w = svd.w;
        Matx33d W(0,-1,0,1,0,0,0,0,1);//HZ 9.13
        Mat_<double> R = svd_u * Mat(W) * svd_vt; //
        Mat_<double> T = svd_u.col(2); //u3

        if (!CheckCoherentRotation (R)) {
        std::cout<<"resulting rotation is not coherent\n";
        return 0;
        }

     */
    Mat w = new Mat();
    Mat u = new Mat();
    Mat vt = new Mat();

    Core.SVDecomp(E, w, u, vt, Core.DECOMP_SVD); // Maybe use flags
    double[] W_Values = {0,-1,0,1,0,0,0,0,1};
    Mat W = new Mat(new Size(3,3), CvType.CV_64FC1, new Scalar(W_Values) );

    Core.multiply(u, W, R);
    Core.multiply(R, vt, R);

    T = u.col(2);
}

And here are the results of all matrizes after and during calculation.

        Number matches: 10299
        Number of good matches: 590
        Number of obj_points left: 590.0

        Fundamental: 
        [4.209958176688844e-08, -8.477216249742946e-08, 9.132798068178793e-05;
        3.165719895008366e-07, 6.437858397735847e-07, -0.0006976204595236443;
        0.0004532506630569588, -0.0009224427024602799, 1]

        Essential: 
        [0.05410018455525099, 0, 0;
        0, 0.8272987826496967, 0;
        0, 0, 1]

        U: 
        [0, 0, 1;
         0, 0.9999999999999999, 0;
         1, 0, 0]

        W: 
        [1; 0.8272987826496967; 0.05410018455525099]

        vt:
        [0, 0, 1;
         0, 1, 0;
         1, 0, 0]


        R: 
        [0, 0, 0;
         0, 0, 0;
         0, 0, 0]

        T: 
        [1; 0; 0]

And for completion here are the image I am using left: https://drive.google.com/file/d/0Bx9OKnxaua8kXzRFNFRtMlRHSzg/edit?usp=sharing right: https://drive.google.com/file/d/0Bx9OKnxaua8kd3hyMjN1Zll6ZkE/edit?usp=sharing

Can someone point out where something is goind wrong or what I am doing wrong?

click to hide/show revision 2
retagged

updated 2014-03-29 07:04:16 -0600

berak gravatar image

Decomposition of essential matrix leads to wrong rotation and translation

Hi, I am doing some SfM and having troubles getting R and T from the essential matrix.

Here is what I am doing in sourcecode:

            Mat fundamental = Calib3d.findFundamentalMat(object_left, object_right);
    Mat E = new Mat();

    Core.multiply(cameraMatrix.t(), fundamental, E); // cameraMatrix.t()*fundamental*cameraMatrix;
    Core.multiply(E, cameraMatrix, E);

    Mat R = new Mat();
    Mat.zeros(3, 3, CvType.CV_64FC1).copyTo(R);

    Mat T = new Mat();

    calculateRT(E, R, T);

private void calculateRT(Mat E, Mat R, Mat T){

    /*
     * //-- Step 6: calculate Rotation Matrix and Translation Vector
        Matx34d P;
        //decompose E 
        SVD svd(E,SVD::MODIFY_A);
        Mat svd_u = svd.u;
        Mat svd_vt = svd.vt;
        Mat svd_w = svd.w;
        Matx33d W(0,-1,0,1,0,0,0,0,1);//HZ 9.13
        Mat_<double> R = svd_u * Mat(W) * svd_vt; //
        Mat_<double> T = svd_u.col(2); //u3

        if (!CheckCoherentRotation (R)) {
        std::cout<<"resulting rotation is not coherent\n";
        return 0;
        }

     */
    Mat w = new Mat();
    Mat u = new Mat();
    Mat vt = new Mat();

    Core.SVDecomp(E, w, u, vt, Core.DECOMP_SVD); // Maybe use flags
    double[] W_Values = {0,-1,0,1,0,0,0,0,1};
    Mat W = new Mat(new Size(3,3), CvType.CV_64FC1, new Scalar(W_Values) );

    Core.multiply(u, W, R);
    Core.multiply(R, vt, R);

    T = u.col(2);
}

And here are the results of all matrizes after and during calculation.

        Number matches: 10299
        Number of good matches: 590
        Number of obj_points left: 590.0

        Fundamental: 
        [4.209958176688844e-08, -8.477216249742946e-08, 9.132798068178793e-05;
        3.165719895008366e-07, 6.437858397735847e-07, -0.0006976204595236443;
        0.0004532506630569588, -0.0009224427024602799, 1]

        Essential: 
        [0.05410018455525099, 0, 0;
        0, 0.8272987826496967, 0;
        0, 0, 1]

        U: 
        [0, 0, 1;
         0, 0.9999999999999999, 0;
         1, 0, 0]

        W: 
        [1; 0.8272987826496967; 0.05410018455525099]

        vt:
        [0, 0, 1;
         0, 1, 0;
         1, 0, 0]


        R: 
        [0, 0, 0;
         0, 0, 0;
         0, 0, 0]

        T: 
        [1; 0; 0]

And for completion here are the image I am using left: https://drive.google.com/file/d/0Bx9OKnxaua8kXzRFNFRtMlRHSzg/edit?usp=sharing right: https://drive.google.com/file/d/0Bx9OKnxaua8kd3hyMjN1Zll6ZkE/edit?usp=sharing

Can someone point out where something is goind wrong or what I am doing wrong?