使用OpenGL在OpenCV图像上绘制对象

时间:2015-04-30 12:56:21

标签: opencv opengl augmented-reality

我一直在尝试使用opengl在opencv的相机流上绘制一个3D立方体,但不成功。添加了setOpenGlDrawCallback,但它没有将opengl图像覆盖到opencv图像上。以下是代码。

trackingmanager.h 是类的头文件,它负责处理设备/帧/等。 frameprocessor.h 是类的头文件,用于对从相机获取的帧进行一些处理。

namedWindow正在 trackingmanager.cpp 中初始化,

  

namedWindow(wnd_name,WINDOW_OPENGL);

如果我不将opencv框架渲染到此窗口,我可以看到opengl图像被绘制到同一个窗口。 (注释掉tm-> renderFrame())。

另外,我在运行时会看到以下消息,

  

1]初始化完成   2]提供Opengl支持

#include <iostream>
#include "trackingmanager.h"
#include "frameprocessor.h"
#include <GL/glut.h>

using namespace std;
static void renderObject(void *param);
vector<vector<Point> > square;

int main(int argc, char **argv)
{
    TrackingManager *tm = new TrackingManager();
    FrameProcessor *processor = new FrameProcessor();
    setOpenGlContext(tm->wnd_name);
    setOpenGlDrawCallback(tm->wnd_name, renderObject, NULL);

    while(tm->getFrame()){
        square = processor->process(&tm->frame);
        tm->renderFrame();
        updateWindow(tm->wnd_name);

        int key = waitKey(10);
        if(key == 27){
            tm->releaseCamera();
            break;
        }
    }
    setOpenGlDrawCallback(tm->wnd_name, NULL, NULL);
    return 0;
}

static void renderObject(void* param)
{
     glLoadIdentity();
     glClearColor (0.0, 0.0, 0.0, 0.0);
     glClear (GL_COLOR_BUFFER_BIT);
     glColor3f (1.0, 1.0, 1.0);
     glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
     glTranslated(0.0, 0.0, -1.0);

     glRotatef( 55, 1, 0, 0 );
     glRotatef( 45, 0, 1, 0 );
     glRotatef( 0, 0, 0, 1 );

     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();
    }
}

1 个答案:

答案 0 :(得分:0)

如果可以使用python,可以通过pip提供一个名为scikit-surgeryvtk的库。该库使您可以轻松地将任意对象覆盖到opencv背景上。该库实现了Visual ToolKit(VTK)以完成所有3D繁重的工作。这个库比openGL高很多(更易于使用)。