Loading texture for OpenGL with OpenCV
Asked Answered
M

4

6

I have seen many code samples for loading textures for OpenGL, many of them a bit complicated to understand or requiring new functions with a lot of code.

I was thinking that as OpenCV allows us to load any image format it can be a simple an efficient way to load textures to OpenGL, but I am missing something. I have this piece of code in c++:

cv::Mat texture_cv;
GLuint texture[1];
int Status=FALSE;

if( texture_cv = imread("stones.jpg"))  {

        Status=TRUE;                            // Set The Status To TRUE
        glGenTextures(1, &texture[0]);                  // Create The Texture


        glBindTexture(GL_TEXTURE_2D, texture[0]);               
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S , GL_REPEAT );
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

        glTexImage2D(GL_TEXTURE_2D, 0, 3, texture_cv.cols, texture_cv.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_cv.data);
}

And it is not compiling because of this error:

error C2451: conditional expression of type 'cv::Mat' is illegal

Any suggestions? How should I do the conversion from cv::Mat to openGL texture?

Machzor answered 3/2, 2012 at 8:57 Comment(4)
I think that using OpenCV for loading textures is overkill. There's the library DevIL which has been designed explicitly for this.Romberg
I am using OpenCV in my project and I am constantly changing from opengl to opencv data, that's why I got the idea of reducing libraries. Thanks anyway for the DevIL, I will have look at it.Machzor
FreeImage is also another (more recent) alternative to DevIL =)Inspissate
Should that not be GL_TEXTURE_RECTANGLE if texture.cols != texture.rows ?Droughty
N
8

Your error appears there, right?

if( texture_cv = imread("stones.jpg"))  {

because in if(expr) expr must be bool or can be casted to bool. But there is no way to convert cv::Mat into boolean implicitly. But you can check the result of imread like that:

texture_cv = imread("stones.jpg");
if (texture_cv.empty()) {
  // handle was an error
} else {
  // do right job
} 

See: cv::Mat::empty(), cv::imread

Hope that helped you.

Nauseous answered 3/2, 2012 at 9:9 Comment(0)
P
3

The assignment operator

texture_cv = imread("stones.jpg")

returns a cv::Mat that can't be used in a conditional expression. You should write something like

if((texture_cv = imread("stones.jpg")) != /* insert condition here */ )  {
      //...
}

or

texture = imread("stone.jpg");
if(!texture.empty())  {
          //...
}
Pharmacist answered 3/2, 2012 at 9:10 Comment(0)
P
2

from this doc, I suggest you to change your test:

texture_cv = imread("stones.jpg");
if (texture_cv.data != NULL)  {
  ...
Phonoscope answered 3/2, 2012 at 9:12 Comment(0)
A
0

Another short question... I think you may need to use

glTexImage2D(GL_TEXTURE_2D, 0, 3, texture_cv.cols, texture_cv.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_cv.ptr());

instead of

glTexImage2D(GL_TEXTURE_2D, 0, 3, texture_cv.cols, texture_cv.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_cv.data);
Anaphora answered 7/10, 2016 at 3:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.