我已经在网上搜索了几天,寻找最快的方法来捕获 OpenCV 网络摄像头并将其显示在 OpenGL 上下文中。到目前为止,这似乎工作正常,直到我需要缩放。
void Camera::DrawIplImage1(IplImage *image, int x, int y, GLfloat xZoom, GLfloat yZoom)
{
GLenum format;
switch(image->nChannels) {
case 1:
format = GL_LUMINANCE;
break;
case 2:
format = GL_LUMINANCE_ALPHA;
break;
case 3:
format = GL_BGR;
break;
default:
return;
}
yZoom =- yZoom;
glRasterPos2i(x, y);
glPixelZoom(xZoom, yZoom); //Slow when not (1.0f, 1.0f);
glDrawPixels(image->width, image->height, format, GL_UNSIGNED_BYTE, image->imageData);
}
我听说也许采用 FBO 方法会更快。关于以最快的方式将 OpenCV 网络摄像头捕捉到 OpenGL 上下文的任何想法。我将测试我看到的所有内容并发布结果。
最佳答案
您确定您的 openGL 实现需要 ^2 个纹理吗?即使是非常糟糕的 PC 实现(是的英特尔)现在也可以管理任意大小。
那么最快的可能是使用 openGL Pixel buffer 抱歉,代码来自 Qt,因此函数名称略有不同,但顺序相同
分配opengl纹理
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, glFormat, width, height, 0, glFormatExt, glType, NULL );
glDisable(GL_TEXTURE_2D);
现在获取指向纹理的指针以使用内存
glbuffer.bind();
unsigned char *dest = (unsigned char*)glbuffer.map(QGLBuffer::ReadWrite);
// creates an openCV image but the pixel data is stored in an opengl buffer
cv::Mat opencvImage(rows,cols,CV_TYPE,dest);
.... do stuff ....
glbuffer.unmap(); // pointer is no longer valid - so neither is openCV image
然后绘制它 - 这基本上应该是即时的,因为数据已在上面的映射调用中复制到 GPU
glBindTexture(GL_TEXTURE_2D,texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, width, height, glFormatExt, glType, 0);
glbuffer.release();
通过使用不同类型的 glFormat 和 glFormatExt,您可以让显卡在硬件中自动在 opencVs BGR 和典型 RGBA 显示格式之间进行转换。
关于performance - 最快的 OpenCV 2 OpenGL 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10324337/