Loading texture for OpenGL with OpenCV

本秂侑毒 提交于 2019-11-30 00:59:27

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.

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())  {
          //...
}

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

texture_cv = imread("stones.jpg");
if (texture_cv.data != NULL)  {
  ...
EdwinDebuger

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!