将OpenGL纹理转换为OpenCV矩阵

5
我一直在寻找一种将OpenGL纹理转换为OpenCV矩阵类型的方法。我找到了很多指南,展示了从OpenCV矩阵到OpenGL纹理的转换,但可悲的是没有反向转换的指南。我也阅读了this和它的answer,但并没有让我更聪明。我使用OpenCV3.1和OpenGL4.4编写C++代码。
编辑:更新的代码

main.cpp:

#include "CameraCapture.h"
#include "GUIMainWindow.h"
#include "glfw3.h"
#include "Texture.h"

using namespace std;

int main(int argc, char* argv[]) {

    if (!glfwInit()) {
        fprintf(stderr, "Failed to initialize GLFW \n");
        return -1;
    }

    glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
    GLFWwindow* window = glfwCreateWindow(1929, 1341, "OpenGL", NULL, NULL);

    if (window == NULL) {
        fprintf(stderr, "Failed to make GLFW window.");
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    CameraCapture *cc = new CameraCapture();
    cc->CameraCapture::AvailableCameras();
    GLuint texture = cc->CameraCapture::OpenCamera(0);

    glEnable(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, texture);

    drawGLTexture(window);

    Mat out = textureToMat(texture);
    namedWindow("Raytrix feed", WINDOW_AUTOSIZE);
    imshow("Raytrix feed", out);

    waitKey(0);

    return 0;

}

Texture.cpp:

Mat textureToMat(GLuint texture_id) {

    glBindTexture(GL_TEXTURE_2D, texture_id);
    GLenum texture_width, texture_height;

    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (GLint*)&texture_width);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (GLint*)&texture_height);

    unsigned char* texture_bytes = (unsigned char*)malloc(sizeof(unsigned char)*texture_width*texture_height * 4);

    glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_bytes);

    return Mat(texture_height, texture_width, CV_8UC4, texture_bytes);

}

void drawGLTexture(GLFWwindow *window) {

    glColor3f(1.0f, 1.0f, 1.0f);

    glBegin(GL_TRIANGLES);
    glTexCoord2f(0, 1);
    glVertex2f(-1, -1);

    glTexCoord2f(1, 1);
    glVertex2f(1, -1);

    glTexCoord2f(0, 0);
    glVertex2f(-1, 1);

    glTexCoord2f(1, 1);
    glVertex2f(1, -1);

    glTexCoord2f(1, 0);
    glVertex2f(1, 1);

    glTexCoord2f(0, 0);
    glVertex2f(-1, 1);
    glEnd();

    glfwSwapBuffers(window);
    glfwPollEvents();

    glFlush();
    glFinish();

}

头部:

#ifndef TEXTURE_H
#define TEXTURE_H

#include "glew.h"
#include "glfw3.h"

#include <string>
#include <iostream>
#include <vector>
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <opencv2\opencv.hpp>

using namespace std;
using namespace cv;

Mat textureToMat(GLuint textureID);
void drawGLTexture(GLFWwindow *window);

#endif /*!TEXTURE_H*/

1
你链接的答案基本上展示了正确的做法,你觉得哪一部分难以理解? - Emily L.
没错。现在还是清晨,我还没有喝咖啡,我会更新帖子的。 - T. Soemod
对我来说很难看出纹理ID的使用情况。假设我在OpenGL中加载了一张图像并获取了它的纹理ID,那么我该如何将其转换为OpenCV矩阵? - T. Soemod
通过回到两个步骤,甚至在加载纹理到OpenGL之前,将数据放入OpenCV矩阵中。OpenGL不处理图像文件格式,事实上许多人实际上使用OpenCV来加载图像文件(与OpenGL不同,OpenCV确实知道如何处理图像文件和格式),并将结果的OpenCV mat加载到OpenGL中。当然,如果您想使用OpenGL生成图像并使用OpenCV处理它们,那么您必须读取图像数据,这就是glReadPixels的用途。但这通常不涉及纹理。 - datenwolf
感谢您的输入,但那不是我想要的。基本上,我有一个系统,它会输出由纹理ID给出的图像。我想要的是将此图像加载到OpenCV中,以便我可以在那里执行各种操作。我无法直接将其加载到OpenCV中,它必须通过纹理ID进行处理。 - T. Soemod
2个回答

3
我已测试下面的代码,它似乎可以工作。(请注意在glGetTexImage()中使用的GL_BGR
cv::Mat get_ocv_img_from_gl_img(GLuint ogl_texture_id)
{
    glBindTexture(GL_TEXTURE_2D, ogl_texture_id);
    GLenum gl_texture_width, gl_texture_height;

    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (GLint*)&gl_texture_width);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (GLint*)&gl_texture_height);

    unsigned char* gl_texture_bytes = (unsigned char*) malloc(sizeof(unsigned char)*gl_texture_width*gl_texture_height*3);
    glGetTexImage(GL_TEXTURE_2D, 0 /* mipmap level */, GL_BGR, GL_UNSIGNED_BYTE, gl_texture_bytes);

    return cv::Mat(gl_texture_height, gl_texture_width, CV_8UC3, gl_texture_bytes);
}

我又遇到了一些问题。这是你用于转换的所有代码吗?假设上下文正确?当我尝试显示转换后的图像时,它返回空白(白色)。 - T. Soemod
@T.Soemod 以下是完整的源代码。https://gist.github.com/anonymous/50cd7f9b51f8f550024bcaec89726494 - sarasvati
谢谢,我会尽力更加密切地遵循那个。 - T. Soemod
@T.Soemod 看起来 Zoidberg 被 Github 弄坏了 :). 我使用的参考图像在这里 - sarasvati
1
我想发一篇帖子告诉你问题已经解决了。最终,我不得不将数据写入帧缓冲区,然后从那里使用glReadPixels()读取它。显然,当glGetTexImage尝试读取它不理解的格式时,它具有输出空白数据的有效行为,并且没有已知的解决方法。在我看来,这相当愚蠢,但我想这就是现实。感谢您的耐心和帮助! - T. Soemod
显示剩余41条评论

0
  1. 首先需要绑定要检索的纹理,然后获取其尺寸。
  2. 找到一个连续的内存区域,以符合OpenGL的要求,将数据放入其中。
  3. 最后使用glGetTexImage来获取像素。

伪代码:

GLint width,height,alignment;
glBindTexture(GL_TEXTURE_2D, textureID);
glGetIntegerv(GL_PACK_ALIGNMENT, &alignment);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
// Fiddle with alignment to make sure you get properly aligned buffer/width.
byte* imagedata = new byte[width*height*3];
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, imagedata);
// move data to cv::Mat or use cv::Mat to allocate to original image buffer (imagedata)
// but be mindful that the memory region in cv::Mat is contiguous and the right
// size and all.

https://www.opengl.org/sdk/docs/man2/xhtml/glGetTexImage.xml

https://www.opengl.org/sdk/docs/man2/xhtml/glGet.xml

https://www.opengl.org/sdk/docs/man2/xhtml/glGetTexLevelParameter.xml


非常感谢!这个加上@sarasvati的回答让它起作用了 :) - T. Soemod
我在@sarasvati的帮助下已经取得了很大进展,但是我在使用OpenCV显示glGetTexImage()输出时遇到了一些问题,即函数返回的数据为空白。我有理由相信这是由于从glTexImage2D()获取的纹理存在某些对齐问题,我尝试设置pack/unpack对齐以纠正此问题,但没有成功。你有什么建议吗?我的调用是glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI, t_width, t_height, 0, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, data); 还有上面的其余代码。 - T. Soemod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接