使用 OpenCV 加载 OpenGL 纹理
我见过很多为 OpenGL 加载纹理的代码示例,其中许多都有点难以理解或需要带有大量代码的新函数。
我认为,由于 OpenCV 允许我们加载任何图像格式,因此它可以是一种将纹理加载到 OpenGL 的简单有效的方法,但我错过了一些东西。我在 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);
}
由于此错误,它无法编译:
错误 C2451:“cv::Mat”类型的条件表达式非法
什么建议吗?我应该如何进行从 cv::Mat 到 openGL 纹理的转换?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的错误出现在那里,对吗?
因为在
if(expr)
中 expr 必须是bool
或者可以转换为bool
。但是没有办法将cv::Mat
隐式转换为布尔值。但是您可以像这样检查imread
的结果:请参阅: cv::Mat::empty(), cv::imread
希望对您有帮助。
Your error appears there, right?
because in
if(expr)
expr must bebool
or can be casted tobool
. But there is no way to convertcv::Mat
into boolean implicitly. But you can check the result ofimread
like that:See: cv::Mat::empty(), cv::imread
Hope that helped you.
赋值运算符
返回不能在条件表达式中使用的
cv::Mat
。你应该写一些类似或
The assignment operator
returns a
cv::Mat
that can't be used in a conditional expression. You should write something likeor
从 this 文档,我建议您更改测试:
from this doc, I suggest you to change your test:
另一个简短的问题...
我认为你可能需要使用
而不是
Another short question...
I think you may need to use
instead of