Camera pose estimation (OpenCV PnP)

后端 未结 3 1316
小鲜肉
小鲜肉 2020-12-05 01:30

I am trying to get a global pose estimate from an image of four fiducials with known global positions using my webcam.

I have checked many stackexchange questions an

3条回答
  •  再見小時候
    2020-12-05 02:05

    If you mean with global pose a 4x4 camera pose matrix, which can be used in OpenGL, I do it this way

    CvMat* ToOpenGLCos( const CvMat* tVec, const CvMat* rVec )
    {
        //** flip COS 180 degree around x-axis **//
    
        // Rodrigues to rotation matrix
        CvMat* extRotAsMatrix = cvCreateMat(3,3,CV_32FC1);
        cvRodrigues2(rVec,extRotAsMatrix);
    
        // Simply merge rotation matrix and translation vector to 4x4 matrix 
        CvMat* world2CameraTransformation = CreateTransformationMatrixH(tVec,
        extRotAsMatrix );
    
        // Create correction rotation matrix (180 deg x-axis)
        CvMat* correctionMatrix = cvCreateMat(4,4,CV_32FC1);
        /* 1.00000   0.00000   0.00000   0.00000
           0.00000  -1.00000  -0.00000   0.00000
           0.00000   0.00000  -1.00000   0.00000
           0.00000   0.00000   0.00000   1.00000 */
        cvmSet(correctionMatrix,0,0,1.0); cvmSet(correctionMatrix,0,1,0.0);
        ... 
    
        // Flip it
        CvMat* world2CameraTransformationOpenGL = cvCreateMat(4,4,CV_32FC1);
        cvMatMul(correctionMatrix,world2CameraTransformation,   world2CameraTransformationOpenGL);
    
        CvMat* camera2WorldTransformationOpenGL = cvCreateMat(4,4,CV_32FC1);
        cvInv(world2CameraTransformationOpenGL,camera2WorldTransformationOpenGL,
        CV_LU );
    
        cvReleaseMat( &world2CameraTransformationOpenGL );
        ...
    
        return camera2WorldTransformationOpenGL;
    }
    

    I think flipping the coordinate system is necessary, because OpenCV and OpenGL/VTK/etc. use different coordinate systems, as illustrated in this picture OpenGL and OpenCV Coordinate Systems

    Well, it works this way but somebody might have a better explanation.

提交回复
热议问题