使用OpenCV读取图片代码如下
img = imread(m_fileName); if (img.empty()) { fprintf(stderr, "Can not load image %s\n", m_fileName); return -1; } //设置长宽 int width = img.cols; int height = img.rows; int channel = img.channels(); printf(" depth %d\n", channel); //获取图像指针 int pixellength = width * height * channel; pixels = new GLubyte[pixellength]; memcpy(pixels, img.data, pixellength * sizeof(char)); //imshow("OpenCV", img); glGenTextures(1, &m_textureObj); glBindTexture(m_textureTarget, m_textureObj); //必须一个RGB 一个BGR(opencv的mat类的颜色通道是BGR) 否则会出现颜色偏差 glTexImage2D(m_textureTarget, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, pixels); //纹理放大缩小使用线性插值 glTexParameterf(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glBindTexture(m_textureTarget, 0); free(pixels);
运行程序时,出现了两个问题:
- 纹理贴图是黑白的——解决方案:读取的图片的高和宽的大小改为2的倍数即可
- 贴图颜色出现偏差——解决方案:(原因见上)
glTexImage2D(m_textureTarget, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, pixels);
原文地址:https://www.cnblogs.com/farewell-farewell/p/9581674.html
时间: 2024-10-10 15:40:19