用OpenCV显示OpenGL图形

前言

  本文就是一个小实验,试验 OpenCV 到底能不能支持 OpenGL 图形显示。

正文

  如果在 OpenCV 用 CMake 编译时勾选 WITH_OPENGL 且编译一切顺利的话,编译和配置的具体步骤和情况可以看 Shaun 写的一篇文档:Win10+VS2013+CMake-gui编译和配置OpenCV-3.2.0 ,那么就可以用 OpenCV 窗口显示 OpenGL 图形。

  在 VS 下使用 Windows 原有的 OpenGL 函数需要包含以下头文件和库文件:

1
2
3
4
5
6
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>

#pragma comment(lib, "OpenGL32.lib")
#pragma comment(lib, "glu32.lib")

  在 OpenCV 中显示 OpenGL 图形需要 cv::namedWindow(openGLWindowName, cv::WINDOW_OPENGL),在 namedWindow 函数中添加 cv::WINDOW_OPENGL 参数说明该窗口支持 OpenGL 图形。

附示例程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <opencv.hpp>

#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>

#pragma comment(lib, "OpenGL32.lib")
#pragma comment(lib, "glu32.lib")

static const float vertex_list[][3] =
{
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
};

// 将要使用的顶点的序号保存到一个数组里面

static const GLint index_list[][2] =
{
{ 0, 1 },
{ 2, 3 },
{ 4, 5 },
{ 6, 7 },
{ 0, 2 },
{ 1, 3 },
{ 4, 6 },
{ 5, 7 },
{ 0, 4 },
{ 1, 5 },
{ 7, 3 },
{ 2, 6 }
};

static float rotate = 0;
static int times = 0;

GLint windowWidth = 800;
GLint windowHeight = 800;

GLfloat xRotAngle = -75.0f;
GLfloat yRotAngle = 0.0f;
GLfloat zRotAngle = -135.0f;

float MIN_X = -200;
float MAX_X = 200;

float MIN_Y = -200;
float MAX_Y = 200;

float MIN_Z = -200;
float MAX_Z = 200;

GLfloat coordinatesize = 200.0f;
GLfloat ratio = 1;

void drawLine(float x1, float y1, float z1, float x2, float y2, float z2)
{
glBegin(GL_LINES);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glEnd();
glFlush();
}

// 绘制立方体
void DrawCube(void)
{
int i, j;
glBegin(GL_LINES);
for (i = 0; i < 12; ++i) // 12 条线段
{
for (j = 0; j < 2; ++j) // 每条线段 2个顶点
{
glVertex3fv(vertex_list[index_list[i][j]]);
}
}
glEnd();
glFlush();
}

void reshapeOperate()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (ratio < 1)
glOrtho(-coordinatesize, coordinatesize, -coordinatesize / ratio, coordinatesize / ratio, -coordinatesize, coordinatesize);
else
glOrtho(-coordinatesize*ratio, coordinatesize*ratio, -coordinatesize, coordinatesize, -coordinatesize, coordinatesize);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void reshape(int w, int h)
{
if ((w == 0) || (h == 0))
return;

glViewport(0, 0, w, h);

ratio = (GLfloat)w / (GLfloat)h;

reshapeOperate();
}

GLfloat AngleX = 45.0f;
GLfloat AngleY = 315.0f;

void reshape1(int w, int h)
{
GLfloat aspect = (GLfloat)w / (GLfloat)h;
GLfloat nRange = 100.0f;

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION); //将当前矩阵指定为投影模式
glLoadIdentity();

//设置三维投影区

if (w <= h)
{
glOrtho(-nRange, nRange, -nRange * aspect, nRange * aspect, -nRange, nRange);
}
else
{
glOrtho(-nRange, nRange, -nRange / aspect, nRange / aspect, -nRange, nRange);
}
}

void onDraw(void*)
{
// Draw something using OpenGL here
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除所有的像素

//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
//glPushMatrix();

////glTranslatef(-0.2, 0, 0); // 平移
////glScalef(2, 1, 1); // 缩放

//glRotatef(xRotAngle, 1.0f, 0.0f, 0.0f);
//glRotatef(yRotAngle, 0.0f, 1.0f, 0.0f);
//glRotatef(zRotAngle, 0.0f, 0.0f, 1.0f);

//glColor3f(1, 0, 0);
//drawLine(0, 0, 0, MAX_X, 0, 0); //x轴

//glColor3f(0, 1, 0);
//drawLine(0, 0, 0, 0, MAX_Y, 0); //y轴

//glColor3f(0, 0, 1);
//drawLine(0, 0, 0, 0, 0, MAX_Z); //z轴

//times++;
//if (times > 1)
//{
// times = 0;
//}

//if (times % 1 == 0)
//{
// rotate += 0.3;
//}

//glRotatef(rotate, 0, 1, 0);
//glRotatef(rotate, 1, 0, 0);

//glColor3f(0, 1, 1);

//DrawCube();

//glPopMatrix();

reshape1(windowWidth, windowHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
AngleX++;
AngleY++;
glPushMatrix();
{
glRotatef(AngleX, 1.0f, 0.0f, 0.0f);
glRotatef(AngleY, 0.0f, 1.0f, 0.0f);

glBegin(GL_POLYGON); //前表面
glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)255);//颜色设置为白色
glVertex3f(50.0f, 50.0f, 50.0f);

glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);//颜色设置为黄色
glVertex3f(50.0f, -50.0f, 50.0f);

glColor3ub((GLubyte)255, (GLubyte)0, (GLubyte)0);//颜色设置为红色
glVertex3f(-50.0f, -50.0f, 50.0f);

glColor3ub((GLubyte)255, (GLubyte)0, (GLubyte)255);//颜色设置为白色
glVertex3f(-50.0f, 50.0f, 50.0f);
glEnd();

glBegin(GL_POLYGON); //后表面
glColor3f(0.0f, 1.0f, 1.0f);//颜色设置为青色
glVertex3f(50.0f, 50.0f, -50.0f);

glColor3f(0.0f, 1.0f, 0.0f);//颜色设置为绿色
glVertex3f(50.0f, -50.0f, -50.0f);

glColor3f(0.0f, 0.0f, 0.0f);//颜色设置为黑色
glVertex3f(-50.0f, -50.0f, -50.0f);

glColor3f(0.0f, 0.0f, 1.0f);//颜色设置为蓝色
glVertex3f(-50.0f, 50.0f, -50.0f);
glEnd();

glBegin(GL_POLYGON); //上表面
glColor3d(0.0, 1.0, 1.0);//颜色设置为青色
glVertex3f(50.0f, 50.0f, -50.0f);

glColor3d(1.0, 1.0, 1.0);//颜色设置为白色
glVertex3f(50.0f, 50.0f, 50.0f);

glColor3d(1.0, 0.0, 1.0);//颜色设置为品红色
glVertex3f(-50.0f, 50.0f, 50.0f);

glColor3d(0.0, 0.0, 1.0);//颜色设置为蓝色
glVertex3f(-50.0f, 50.0f, -50.0f);
glEnd();

glBegin(GL_POLYGON); //下表面
glColor3ub(0u, 255u, 0u);//颜色设置为绿色
glVertex3f(50.0f, -50.0f, -50.0f);

glColor3ub(255u, 255u, 0u);//颜色设置为黄色
glVertex3f(50.0f, -50.0f, 50.0f);

glColor3ub(255u, 0u, 0u);//颜色设置为红色
glVertex3f(-50.0f, -50.0f, 50.0f);

glColor3ub(0u, 0u, 0u);//颜色设置为黑色
glVertex3f(-50.0f, -50.0f, -50.0f);
glEnd();

glBegin(GL_POLYGON); //左表面
glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)255);//颜色设置为白色
glVertex3f(50.0f, 50.0f, 50.0f);

glColor3ub((GLubyte)0, (GLubyte)255, (GLubyte)255);//颜色设置为青色
glVertex3f(50.0f, 50.0f, -50.0f);

glColor3ub((GLubyte)0, (GLubyte)255, (GLubyte)0);//颜色设置为绿色
glVertex3f(50.0f, -50.0f, -50.0f);

glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);//颜色设置为黄色
glVertex3f(50.0f, -50.0f, 50.0f);
glEnd();

glBegin(GL_POLYGON); //右表面
glColor3f(1.0f, 0.0f, 1.0f);//颜色设置为品红色
glVertex3f(-50.0f, 50.0f, 50.0f);

glColor3f(0.0f, 0.0f, 1.0f);//颜色设置为蓝色
glVertex3f(-50.0f, 50.0f, -50.0f);

glColor3f(0.0f, 0.0f, 0.0f);//颜色设置为黑色
glVertex3f(-50.0f, -50.0f, -50.0f);

glColor3f(1.0f, 0.0f, 0.0f);//颜色设置为红色
glVertex3f(-50.0f, -50.0f, 50.0f);
glEnd();
}
glPopMatrix();
}

void opencvWithOpenGLTest()
{
std::string openGLWindowName = "OpenGL Test";
cv::namedWindow(openGLWindowName, cv::WINDOW_OPENGL);
cv::resizeWindow(openGLWindowName, windowWidth, windowHeight);
cv::setOpenGlContext(openGLWindowName);
cv::setOpenGlDrawCallback(openGLWindowName, onDraw, NULL);

while (cv::waitKey(30) != 27)
{
cv::updateWindow(openGLWindowName); // when needed
}
}

int main(int argc, char *argv[])
{
opencvWithOpenGLTest();

return 0;
}

运行成功后可看到一个旋转的彩色立方体。

结论

  从实验结果来看,OpenCV 确实能支持 OpenGL 图形的显示,但其不足之处也很明显:没有提供鼠标和键盘的交互操作(可能是 Shaun 还没发现,毕竟只是尝试一下看它能不能显示),仅仅只是提供一个显示窗口。如果真想用 OpenGL 做一些好玩的东西,还是用 glut 和 glew 吧,不过 glut 已经停止更新许久,glew 在调试时可能会出现一些莫名其妙的错误,所以网上有人用 freeglut 代替 glut,glee 代替 glew,具体的东西 Shaun 也没试过,Shaun 目前还没做过 OpenGL 相关的事,这次用 OpenCV 显示 OpenGL 图形纯粹是为了好玩 (*^__^ *) 嘻嘻……。

后记

  本篇文档也是上次编译配置完 OpenCV-3.2 后做的一次小实验,但当时并没有记录,所以还有一些参考资料也已经不知道了 :-(。

参考资料

[1] OpenCV学习笔记(六十一)——建立支持OpenGL的OpenCV工程“Master OpenCV”chp.3http://blog.csdn.net/yang_xian521/article/category/910716

[2] 几个opengl立方体绘制案例http://blog.csdn.net/bcbobo21cn/article/category/3104565