Download texture for OpenGL with OpenCV

I have seen many sample code for loading textures for OpenGL , many of which are a little difficult to understand or require new features with a lot of code.

I thought that since OpenCV allows you to load any image format, it might be a simple, efficient way to load textures into OpenGL , but I'm 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 does not compile due to this error:

error C2451: conditional expression like 'cv :: Mat' is illegal

Any suggestions? How do I convert from cv :: Mat to openGL texture?

+6
source share
4 answers

Your mistake appears there, right?

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

because in if(expr) expression must be bool or it can be sent to bool . But it is not possible to convert cv::Mat to logical implicitly. But you can check the imread result as follows:

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

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

Hope this helps you.

+8
source

Assignment operator

 texture_cv = imread("stones.jpg") 

returns a cv::Mat , which cannot 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()) { //... } 
+3
source

from this document, I suggest you change your test:

 texture_cv = imread("stones.jpg"); if (texture_cv.data != NULL) { ... 
+2
source

Another short question ... I think you might need

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

instead

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

Source: https://habr.com/ru/post/907552/


All Articles