How to use an OpenCV rotation and translation vector with OpenGL ES in Android?

后端 未结 2 1791
春和景丽
春和景丽 2020-12-08 01:19

I\'m am working on a basic augmented reality application on Android. What I did so far is detect a square with opencv and then using cvFindExtrinsicCameraParams2() I calcula

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 01:59

    Pandoro's method really works! In case someone wondering "how to convert the rotation vector into a rotation matrix" here's how I did it. By the way, I've used these in OpenGL 2, not ES.

    // use the rotation vector generated from OpenCV's cvFindExtrinsicCameraParams2() 
    float rv[] = {rotation->data.fl[0], rotation->data.fl[1], rotation->data.fl[2] }; 
    
    // use the translation vector generated from OpenCV's cvFindExtrinsicCameraParams2() 
    float tv[] = {translation->data.fl[0], translation->data.fl[1], translation->data.fl[2]} ; 
    
    float rm[9];
    // rotation matrix
    CvMat* rotMat = cvCreateMat (3, 3, CV_32FC1); 
    
    // rotation vectors can be converted to a 3-by-3 rotation matrix
    // by calling cvRodrigues2() - Source: O'Reilly Learning OpenCV
    cvRodrigues2(rotation, rotMat, NULL);
    
    for(int i=0; i<9; i++){
        rm[i] = rotMat->data.fl[i];
    }
    
    rv[1]=-1.0f * rv[1]; rv[2]=-1.0f * rv[2];
    //Convert the rotation vector into a matrix here.
    
    //Complete matrix ready to use for OpenGL
    float RTMat[] = {rm[0], rm[3], rm[6], 0.0f,
                 rm[1], rm[4], rm[7], 0.0f,
                 rm[2], rm[5], rm[8], 0.0f,
                 tv[0], -tv[1], -tv[2], 1.0f};
    

    Good luck!

提交回复
热议问题