摘自:http://www.opencv.org.cn/forum/viewtopic.php?t=9045
一般来说,有2种可能性混合使用OpenGL和OpenCV。比如
1.OpenCV显示最后结果 (2D显示):利用OpenGL作为3D计算引擎,例如计算3D曲面曲线等3D坐标数据,然后再利用OpenCV的函数投影这些坐标并画到2D图像上(OpenCV窗口)。
2.OpenGL显示最后结果(3D显示): 利用OpenCV作为图像处理引擎, 比如图像,视频处理等,然后再利用OpenGL的图像相关函数,比如纹理映射,画到3D空间(OpenGL窗口)里边。
下边的范例属于第二种,利用OpenCV对图像进行二值化,然后再用OpenGL进行一个纹理映射。
Using OpenCV processed Image as the image of texture mapping by OpenGL
(开发环境:VS2008+OpenCV1.1+OpenGL1.0, 如使用源码,请注意图像路径和图像大小)
#include "stdafx.h"
#include <iostream>
#include <GL/glut.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
/* Create texture */
#define texImageWidth 512
#define texImageHeight 512
static GLubyte texImage[texImageHeight][texImageWidth][4];
static GLuint texName;
/*
// coded by Huang,Haiqiao on 2010-01-05
// Using the processed image by OpenCV as the Texture mapping image by OpenGL;
*/
void makeTexImageCV(void)
{//creating a texture image by OpenCV functions
char* filename="D:\\OpenCV_stuff\\SampleImages\\cameraman.jpg";
IplImage* imgGrey = cvLoadImage(filename,0);
if (imgGrey==NULL){
cout << "No valid image input."<<endl;
char c=getchar();
}
int imageWidth=imgGrey->width;
int imageHeight=imgGrey->height;
IplImage* binaryImg = cvCreateImage(cvSize(imageWidth, imageHeight),IPL_DEPTH_8U, 1);
cvThreshold(imgGrey,binaryImg,40,255,CV_THRESH_BINARY);
CvScalar s;
convert OpenCV image to GLubyte image
for (int h=0;h<imageHeight;h++){
for (int w=0;w<imageWidth;w++){
s=cvGet2D(binaryImg,h,w);
//binary image only val[0] got value.
//otherwise val[2] is Red,val[1] is green,val[0] is blue;
texImage[imageHeight-h][w][0] = (GLubyte)s.val[0];
texImage[imageHeight-h][w][1] = (GLubyte)s.val[0];
texImage[imageHeight-h][w][2] = (GLubyte)s.val[0];
texImage[imageHeight-h][w][3] = (GLubyte)255;//alpha
}
}
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
makeTexImageCV();//create texture image by OpenCV functions;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texImageWidth,
texImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texImage);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.6);
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 27://escape
exit(0);
break;
default:
break;
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(550, 550);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenCV image Texture Mapping OpenGL");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <GL/glut.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
/* Create texture */
#define texImageWidth 512
#define texImageHeight 512
static GLubyte texImage[texImageHeight][texImageWidth][4];
static GLuint texName;
/*
// coded by Huang,Haiqiao on 2010-01-05
// Using the processed image by OpenCV as the Texture mapping image by OpenGL;
*/
void makeTexImageCV(void)
{//creating a texture image by OpenCV functions
char* filename="D:\\OpenCV_stuff\\SampleImages\\cameraman.jpg";
IplImage* imgGrey = cvLoadImage(filename,0);
if (imgGrey==NULL){
cout << "No valid image input."<<endl;
char c=getchar();
}
int imageWidth=imgGrey->width;
int imageHeight=imgGrey->height;
IplImage* binaryImg = cvCreateImage(cvSize(imageWidth, imageHeight),IPL_DEPTH_8U, 1);
cvThreshold(imgGrey,binaryImg,40,255,CV_THRESH_BINARY);
CvScalar s;
convert OpenCV image to GLubyte image
for (int h=0;h<imageHeight;h++){
for (int w=0;w<imageWidth;w++){
s=cvGet2D(binaryImg,h,w);
//binary image only val[0] got value.
//otherwise val[2] is Red,val[1] is green,val[0] is blue;
texImage[imageHeight-h][w][0] = (GLubyte)s.val[0];
texImage[imageHeight-h][w][1] = (GLubyte)s.val[0];
texImage[imageHeight-h][w][2] = (GLubyte)s.val[0];
texImage[imageHeight-h][w][3] = (GLubyte)255;//alpha
}
}
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
makeTexImageCV();//create texture image by OpenCV functions;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texImageWidth,
texImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texImage);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.6);
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 27://escape
exit(0);
break;
default:
break;
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(550, 550);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenCV image Texture Mapping OpenGL");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}