Skip to content

Instantly share code, notes, and snippets.

@bigfacebear
Last active October 16, 2022 21:36
Show Gist options
  • Save bigfacebear/b05762180b1ca44047bbb8024c0c7e95 to your computer and use it in GitHub Desktop.
Save bigfacebear/b05762180b1ca44047bbb8024c0c7e95 to your computer and use it in GitHub Desktop.
How to read an image by opencv, then copy it to a texture, and read it out at last.
cv::Mat img = cv::imread("img.png");
GLuint tex_in;
glGenTextures(1, &tex_in);
glBindTexture(GL_TEXTURE_2D, tex_in);
/*By practice, the internalParam:
* GL_RGB, GL_RGBA, GL_RGB8, GL_RGBA8 work
* GL_RGB8UI, GL_RGBA8UI don't work
*/
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, img.cols, img.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, img.data); // copy data to texture object
cv::Mat test(img.rows, img.cols, CV_8UC3);
glGetTexImage(GL_TEXTURE_2D, 0, GL_BGR, GL_UNSIGNED_BYTE, (void*)test.data); // read the image out of texture object
cv::imshow("test", test);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment