Problems using OpenGL with OpenCV

Hi!

I’m pretty new to OpenGL and currently just testing to see if I can get some basic things to work, but I’ve run into some issues. In my program I am trying to load an image into a matrix type using OpenCV 3.1’s imread(), convert it to a texture with a texture_id handle using glTexImage2D(), and pass the handle to another function which converts it back to a matrix type using glGetTexImage().

My problem is that the data attached to the texture_id somehow returns to NULL at the read by glGetTexImage(). I have tried to implement the context for the OpenCV functions, but i don’t know if it is correct. There may also be other faults to my code, that I have not considered. Any help would be appreciated! I am writing in C++11 using VisualStudio15, OpenCV3.1, OpenGL4.4, GLFW and GLEW.

The CPP file:

#include "Texture.h"

GLuint matToTexture(Mat image) {

	GLuint texture_id;

	if (image.empty()) {
		cout << "Image empty." << endl;
	}

	else {

		glGenTextures(1, &texture_id);
		glBindTexture(GL_TEXTURE_2D, texture_id);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

		glTexImage2D(GL_TEXTURE_2D,
			0,
			GL_RGB,
			image.cols,
			image.rows,
			0,
			GL_BGR,
			GL_UNSIGNED_BYTE,
			image.ptr());

	}
	return texture_id;
}

Mat textureToMat(GLuint texture_id) {

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

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

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

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

	Mat out = Mat(texture_height, texture_width, CV_8UC3, texture_bytes);

	//Mat out = Mat(texture_height, texture_width, CV_8UC3, texture_bytes).clone();

	return out;

}

The header file:


#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);
GLuint matToTexture(Mat image);

#endif /*!TEXTURE_H*/

The main call:

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

using namespace std;

int main(){

	glfwInit();
	glewExperimental = GL_TRUE;
	glewInit();

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
	glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

	GLFWwindow* offscreen_context = glfwCreateWindow(630, 354, "OpenGL", NULL, NULL);
	glfwMakeContextCurrent(offscreen_context);

	Mat dummy = imread("mini.jpg", CV_LOAD_IMAGE_COLOR);

	GLuint tex = matToTexture(dummy);

	glBindTexture(GL_TEXTURE_2D, tex);

	Mat some = textureToMat(tex);

	while (!glfwWindowShouldClose(offscreen_context)) {

		glfwSwapBuffers(offscreen_context);
		glfwPollEvents();

		if (glfwGetKey(offscreen_context, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
			glfwSetWindowShouldClose(offscreen_context, GL_TRUE);
		}
	}

	glfwTerminate();

	return 0;

}