简体   繁体   English

将OpenGL纹理转换为OpenCV矩阵

[英]Convering OpenGL texture to OpenCV matrix

I've been looking for a way to convert an OpenGL texture into a OpenCV matrix type. 我一直在寻找一种将OpenGL纹理转换为OpenCV矩阵类型的方法。 I have found many guides which shows the conversion from OpenCV matrix to OpenGL texture, but sadly not the other way around. 我发现了许多指南,显示了从OpenCV矩阵到OpenGL纹理的转换,但遗憾的是没有相反的方式。 I have also read through this and its answer but it did not make me much wiser. 我也读过这篇文章及其答案,但这并没有让我更加明智。 I am writing in C++ and using OpenCV3.1 and OpenGL4.4. 我用C ++编写并使用OpenCV3.1和OpenGL4.4。

EDIT: UPDATED CODE 编辑:更新的代码

main.cpp: 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: 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();

}

Header: 标题:

#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*/

I've tested the code below and it seems to work. 我已经测试了下面的代码,它似乎工作。 (Note the usage of GL_BGR in glGetTexImage() ). (注的用法GL_BGRglGetTexImage()

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);
}
  1. First you need to bind the texture you want to retrieve and then get its dimensions. 首先,您需要绑定要检索的纹理,然后获取其尺寸。
  2. Figure out a contiguous memory region to put the data into that matches OpenGL's requirements. 找出一个连续的内存区域来放入符合OpenGL要求的数据。
  3. Finally use glGetTexImage to obtain the pixels. 最后使用glGetTexImage获取像素。

Pseudocode: 伪代码:

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/glGetTexImage.xml

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM