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;
}
注意:
- 建议使用
- freeGLUT而不是GLUT,它们是两个东西。供过于求已经过时了。但是freeGLUT继续支持最新的OpenGL版本,同时扩展GLUT。 你可能需要一个GL加载库,比如GLEW来获取GL函数指针更新的OpenGL(3.1+)不再支持固定的管道渲染,因此需要vbo和着色器。如果您的目标是GL的较低版本,则需要指定上下文版本。这可以通过
glutInitContextVersion( int major, int minor )
来完成。网上有很多教程。相关文章:
- COM 互操作性需要强命名程序集吗?
- CComPtr 和 std::shared_ptr 互操作性
- C和C++原子之间的互操作性
- C/C++ 与 C# 的互操作性命名约定
- C和C++中类型的互操作性
- C++与Fortran的互操作性
- 关于C#与非托管C++的互操作性的持续传奇
- java和c++的互操作性
- 如何从C++和通过COM互操作性访问.NET类的属性
- OpenCL/OpenGL 互操作性纹理段错误
- Swift和Objective-C++互操作性
- Windows XP和Windows 7之间的DCOM互操作性
- 寻找C++到 WPF/Cocoa 互操作性的跨平台、端到端示例
- QML 和 C++ 映像互操作性
- GNU Fortran与C的互操作性
- linux时区互操作性
- VS 2012与VS 2010的互操作性
- 互操作性的工作原理
- C++到D的互操作性
- OpenCV-OpenGL互操作性