OpenCV-OpenGL互操作性

OpenCV-OpenGL interoperability

本文关键字:互操作性 OpenCV-OpenGL      更新时间:2023-10-16

我在Linux下编译OpenCV 2.4.4与OpenGL支持,但我不知道如何使用opengl_interop.hpp函数(其中一些甚至是未记录的!至少在我的文档版本上是这样)。查看window.cpp在OpenGL启用部分,我发现了一些关于使用函数setOpenGLContext, setOpenGLDrawCallback和updateView的提示,但我不能得到工作,甚至这段非常简单的代码:

#include <opencv2/opencv.hpp>
#include <GL/gl.h>
#include <GL/glut.h>
#include <opencv2/core/opengl_interop.hpp>
using namespace cv;
void on_opengl(void* userdata);
int main(void)
{
    VideoCapture webcam(CV_CAP_ANY);
    Mat frame;
    namedWindow("window", CV_WINDOW_OPENGL);
    setOpenGlContext("window");
    while(waitKey(30) < 0)
    {
        webcam >> frame;
        setOpenGlDrawCallback("window", on_opengl);
        imshow("window", frame);
        updateWindow("window");
    }
    return 0;
}
void on_opengl(void* userdata)
{
    glLoadIdentity();
    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();
    }
}

在网络摄像头流上使用opengl的正确方法是什么?

OpenGL是为渲染图形设计的,OpenCV是为计算机视觉设计的。因此,我建议您在基于gl的应用程序中使用CV,而不是使用CV API进行渲染,回调等。

如果你想要的只是一个简单的演示,那么你可以使用freeGLUT编写一个非常简单的程序,其中包含一些回调,freeGLUT将处理窗口回调和GL上下文创建。(GLFW或Qt也可以)在程序中,使用cv::ogl::Texture2D类来处理纹理对象。使用Texture2D::copyFrom(...)Texture2D::copyTo(...)处理设备/主机内存传输。在渲染回调中,使用标准的GL例程来绘制一个全屏矩形,虽然这个方法不是很有效,但它是有效的。

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/opengl_interop.hpp>
using namespace cv;
//Global vars
Texture2D g_img;
void timer_cb( int )
{
    //...Update the content of g_img
}
void resize_cb( int w, int h ) { /*...*/ }
void render_cb() {
    /* ...render g_img here */
    g_img.bind();
#ifdef USE_FIXED_PIPELINE
//use fixed pipeline for old-school GL rendering
    glMatrixMode( GL_MODELVIEW );
    //Do some transformation
    glBegin(GL_QUADS);
        glTexCoord(...);
        glVertex**(...);
        ...
    glEnd();
#else
//use shaders and VBOs for 3.1+ GL
    glBindProgram( ... );
    glBindBuffer( ... );
    glVertexAttribPointer( ... );
    glDrawArrays( ... );
#endif
}
int main( int argc, char** argv )
{
    //...init GLUT, GLEW and other stuff
    glutMainLoop();
    return 0;
}

注意:

    建议使用
  1. freeGLUT而不是GLUT,它们是两个东西。供过于求已经过时了。但是freeGLUT继续支持最新的OpenGL版本,同时扩展GLUT。
  2. 你可能需要一个GL加载库,比如GLEW来获取GL函数指针更新的OpenGL(3.1+)不再支持固定的管道渲染,因此需要vbo和着色器。如果您的目标是GL的较低版本,则需要指定上下文版本。这可以通过glutInitContextVersion( int major, int minor )来完成。网上有很多教程。