gpt4 book ai didi

performance - 最快的 OpenCV 2 OpenGL 上下文

转载 作者:太空宇宙 更新时间:2023-11-03 21:28:19 24 4
gpt4 key购买 nike

我已经在网上搜索了几天,寻找最快的方法来获取 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 像素缓冲区抱歉,代码来自 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com