OpenCV + OpenGL: How to use SolvePnP Rotation and translation vector in OpenGL

asked 2016-09-19 00:43:04 -0600

jys923 gravatar image

updated 2016-09-19 01:16:56 -0600

I'm working on an AR program.

please heip me dealing's almost done.

I found marker and rvec,tvec by SolvePnP.

I can not draw object in OpenGL.

I am beginer OpenGL,OpenCV.

I want to render 3D cube over that marker using OpenGL.

use support opengl mode by build opencv.

    int main(int argc, char* argv[])
{
    ....
    namedWindow(winname, CV_WINDOW_OPENGL);
    setOpenGlDrawCallback(winname.c_str(), on_opengl);
    .....
        find marker

According to "http://answers.opencv.org/question/23089/opencv-opengl-proper-camera-pose-using-solvepnp/" I'm doing it like this:

Mat rotation(4, 4, CV_64F);
                        Mat viewMatrix = cv::Mat::zeros(4, 4, CV_64FC1);
                        cv::Rodrigues(rvec, rotation);

                        for (unsigned int row = 0; row<3; ++row)
                        {
                            for (unsigned int col = 0; col<3; ++col)
                            {
                                viewMatrix.at<double>(row, col) = rotation.at<double>(row, col);
                            }
                            viewMatrix.at<double>(row, 3) = tvec.at<double>(row, 0);
                        }
                        viewMatrix.at<double>(3, 3) = 1.0f;

                        cv::Mat glViewMatrix = cv::Mat::zeros(4, 4, CV_64F);
                        cv::transpose(viewMatrix, glViewMatrix);
                        glViewMatrix2 = glViewMatrix.clone();

opengl callback

    void on_opengl(void* param)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glLoadMatrixd(&glViewMatrix2.at<double>(0, 0));
    //glTranslated(glViewMatrix2.at<double>(0, 3), -glViewMatrix2.at<double>(1, 3),0);
    //glTranslated(glViewMatrix2.at<double>(3, 0), -glViewMatrix2.at<double>(3, 1), -glViewMatrix2.at<double>(3, 2));
    glTranslated(0.0, 0.0, 0.0);
    static const int coords[6][4][3] = {
        { { +1, -1, -1 },{ -1, -1, -1 },{ -1, +1, -1 },{ +1, +1, -1 } },
        { { +1, +1, -1 },{ -1, +1, -1 },{ -1, +1, +1 },{ +1, +1, +1 } },
        { { +1, -1, +1 },{ +1, -1, -1 },{ +1, +1, -1 },{ +1, +1, +1 } },
        { { -1, -1, -1 },{ -1, -1, +1 },{ -1, +1, +1 },{ -1, +1, -1 } },
        { { +1, -1, +1 },{ -1, -1, +1 },{ -1, -1, -1 },{ +1, -1, -1 } },
        { { -1, -1, +1 },{ +1, -1, +1 },{ +1, +1, +1 },{ -1, +1, +1 } }
    };

    for (int i = 0; i < 6; ++i) {
        glColor3ub(i * 20, 100 + i * 10, i * 42);
        glBegin(GL_QUADS);
        for (int j = 0; j < 4; ++j) {
            glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], 0.2 * coords[i][j][2]);
        }
        glEnd();
    }
}

i cannot find cube.

if I delete "glLoadMatrixd(&glViewMatrix2.at<double>(0, 0));" this line.

I can find cube on center.

Q2-How to use glut.where can I do"glutInit(&argc2, argv2);"

edit retag flag offensive close merge delete

Comments

  • remember, that opencv's y-axis points down, opengl's points up.
  • so far, you're still in "ortho" mode, no perspective transform yet. (but you'll probabaly need one !)
berak gravatar imageberak ( 2016-09-19 02:05:39 -0600 )edit