반응형

OpenCV로 캡처한 영상을 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//상대경로로 헤더파일을 지정합니다.
#include ".\include\GL\freeglut.h"
#include "opencv2/opencv.hpp"  
#include <iostream>  
#include <string> 
 
//사용할 라이브러리를 지정해줍니다.
#pragma comment(lib, "freeglut.lib")
#pragma comment(lib, "glew32.lib")
 
using namespace cv;
using namespace std;
 
 
VideoCapture * capture;
Mat img_cam;
int screenW;
int screenH;
 
GLuint texture_background, texture_cube;
float cubeAngle = 0;            
 
 
//OpenCV Mat을 OpenGL Texture로 변환 
GLuint MatToTexture(Mat image)
{
    if (image.empty())  return -1;
 
    //OpenGL 텍스처 생성
    GLuint textureID;
    glGenTextures(1&textureID);
 
    //텍스처 ID를 바인딩 -  사용할 텍스처 차원을 지정해준다.
    glBindTexture(GL_TEXTURE_2D, textureID); 
 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.cols, image.rows,
        0, GL_RGB, GL_UNSIGNED_BYTE, image.ptr());
 
    return textureID;
}
 
 
void draw_background()
{
    int x = screenW / 100.0;
    int y = screenH / 100.0;
 
    glBegin(GL_QUADS);
    glTexCoord2f(0.01.0); glVertex3f(-x, -y, 0.0);
    glTexCoord2f(1.01.0); glVertex3f(x, -y, 0.0);
    glTexCoord2f(1.00.0); glVertex3f(x, y, 0.0);
    glTexCoord2f(0.00.0); glVertex3f(-x, y, 0.0);
    glEnd();
}
 
 
void drawBitmapText(char *str, float x, float y, float z)
{
    glRasterPos3f(x, y, z); //문자열이 그려질 위치 지정
 
    while (*str)
    {
        //GLUT_BITMAP_TIMES_ROMAN_24 폰트를 사용하여 문자열을 그린다.
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *str);
 
        str++;
    }
}
 
 
//큐브의 한 면, 화면 안쪽 방향인 -Z축방향으로 0.5이동하여 정사각형을 그린다.
static void cubebase(void)
{
    glBegin(GL_QUADS);
    glVertex3d(-0.5-0.5-0.5);
    glVertex3d(-0.50.5-0.5);
    glVertex3d(0.50.5-0.5);
    glVertex3d(0.5-0.5-0.5);
    glEnd();
}
 
//cubebase함수에서 그린 사각형을 회전 및 이동시켜
//큐브를 완성시킨다.
void draw_cube()
{
    glPushMatrix();
 
    glColor3f(0.0f, 1.0f, 0.0f);     // Green, -Z축 방향
    cubebase();
    
    glPushMatrix();
 
    glTranslated(1.00.00.0);
    glRotated(90.00.01.00.0);
    glColor3f(0.0f, 0.0f, 1.0f);     // Blue, +X축 방향
    cubebase();
 
    glPopMatrix();
 
    glPushMatrix();
    glTranslated(-1.00.00.0);
    glRotated(-90.00.01.00.0);
    glColor3f(1.0f, 0.5f, 0.0f);     // Orange, -X축 방향
    cubebase();
    glPopMatrix();
 
    glPushMatrix();
    glTranslated(0.01.00.0);
    glRotated(-90.01.00.00.0);
    glColor3f(1.0f, 0.0f, 0.0f);     // Red, +Y축 방향
    cubebase();
    glPopMatrix();
 
    glPushMatrix();
    glTranslated(0.0-1.00.0);
    glRotated(90.01.00.00.0);
    glColor3f(1.0f, 1.0f, 0.0f);     // Yellow, -Y축 방향
    cubebase();
    glPopMatrix();
 
    glBegin(GL_QUADS);
    glColor3f(1.0f, 0.0f, 1.0f);     // Magenta, +Z축 방향
    glVertex3d(-0.5-0.50.5);
    glVertex3d(0.5-0.50.5);
    glVertex3d(0.50.50.5);
    glVertex3d(-0.50.50.5);
    glEnd();
        
    glPopMatrix();
 
    glFlush();
}
 
void draw_line()
{
 
    glPushMatrix(); //X축 붉은색
    glColor3f(1.00.00.0);
    glBegin(GL_LINES);
    glVertex3f(1.00.00.0);
    glVertex3f(-1.00.00.0);
    glEnd();
    drawBitmapText("+X"0.80.00.0);
    drawBitmapText("-X"-0.80.00.0);
    glPopMatrix();
 
    glPushMatrix(); //Y축 녹색
    glColor3f(0.01.00.0);
    glBegin(GL_LINES);
    glVertex3f(0.01.00.0);
    glVertex3f(0.0-1.00.0);
    glEnd();
    drawBitmapText("+Y"0.00.80.0);
    drawBitmapText("-Y"0.0-0.80.0);
    glPopMatrix();
 
    glPushMatrix(); //Z축 파란색
    glColor3f(0.00.01.0);
    glBegin(GL_LINES);
    glVertex3f(0.00.01.0);
    glVertex3f(0.00.0-1.0);
    glEnd();
    drawBitmapText("+Z"0.00.00.8);
    drawBitmapText("-Z"0.00.0-0.8);
    glPopMatrix();
 
 
    glFlush();
}
 
// 카메라 초기화
void cameraInit()
{
    
    capture = new VideoCapture(0);
 
    if (!capture) {
        printf("Could not capture a camera\n\7");
        return;
    }
 
    Mat img_frame;
   
    capture->read(img_frame);
 
    screenW = img_frame.cols;
    screenH = img_frame.rows;
 
    cout << screenW << " " << screenH << endl;
}
 
 
void display()
{
    //화면을 지운다. (컬러버퍼와 깊이버퍼)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    //이후 연산은 ModelView Matirx에 영향을 준다.
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();                                            
    
    texture_background = MatToTexture(img_cam);
    if (texture_background < 0return;
 
    
    glEnable(GL_TEXTURE_2D);
    glColor3f(1.0f, 1.0f, 1.0f); //큐브나 좌표축 그릴 때 사용한 색의 영향을 안받을려면 필요
    glBindTexture(GL_TEXTURE_2D, texture_background);
    glPushMatrix();
    glTranslatef(0.00.0-9.0);
    draw_background(); //배경그림
    glPopMatrix();
 
    
    glDisable(GL_TEXTURE_2D);
    glPushMatrix();
    glTranslatef(0.00.0-4.0);
    glRotatef(cubeAngle, 1.01.01.0);
    draw_cube(); //큐브
    draw_line();  //좌표축
    glPopMatrix();
    
 
    glutSwapBuffers();
}
 
 
void reshape(GLsizei width, GLsizei height)
{
    glViewport(00, (GLsizei)width, (GLsizei)height); //윈도우 크기로 뷰포인트 설정 
 
    glMatrixMode(GL_PROJECTION); //이후 연산은 Projection Matrix에 영향을 준다.
    glLoadIdentity();
 
    //Field of view angle(단위 degrees), 윈도우의 aspect ratio, Near와 Far Plane설정
    gluPerspective(45, (GLfloat)width / (GLfloat)height, 1.0100.0);
 
    glMatrixMode(GL_MODELVIEW); //이후 연산은 ModelView Matirx에 영향을 준다. 
}
 
 
void timer(int value) {
    //웹캠으로부터 이미지 캡처
    capture->read(img_cam);
    cvtColor(img_cam, img_cam, COLOR_BGR2RGB);
 
    cubeAngle += 1.0f;
    if (cubeAngle > 360) {
        cubeAngle -= 360;
    }
 
    glutPostRedisplay();      //윈도우를 다시 그리도록 요청
    glutTimerFunc(1, timer, 0); //다음 타이머 이벤트는 1밀리세컨트 후  호출됨.
}
 
 
 
void init()
{
    glGenTextures(1&texture_background);
 
    //화면 지울때 사용할 색 지정
    glClearColor(0.00.00.00.0);
 
    //깊이 버퍼 지울 때 사용할 값 지정
    glClearDepth(1.0);
 
    //깊이 버퍼 활성화
    glEnable(GL_DEPTH_TEST);
 
}
 
void keyboard(unsigned char key, int x, int y)
{
    //ESC 키가 눌러졌다면 프로그램 종료
    if (key == 27)
    {
        capture->release();
        exit(0);
    }
}
 
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);  //GLUT 초기화
 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); //더블 버퍼와 깊이 버퍼를 사용하도록 설정, GLUT_RGB=0x00임
 
    cameraInit();
    glutInitWindowSize(screenW, screenH);
    glutInitWindowPosition(100100); //윈도우의 위치 (x,y)
    glutCreateWindow("OpenGL Example"); //윈도우 생성
 
 
    init();
 
    //디스플레이 콜백 함수 등록, display함수는 윈도우 처음 생성할 때와 화면 다시 그릴 필요 있을때 호출된다. 
    glutDisplayFunc(display);
 
    //reshape 콜백 함수 등록, reshape함수는 윈도우 처음 생성할 때와 윈도우 크기 변경시 호출된다.
    glutReshapeFunc(reshape);
    //타이머 콜백 함수 등록, 처음에는 바로 호출됨.
    glutTimerFunc(0, timer, 0);
    //키보드 콜백 함수 등록, 키보드가 눌러지면 호출된다. 
    glutKeyboardFunc(keyboard);
 
    //GLUT event processing loop에 진입한다.
    //이 함수는 리턴되지 않기 때문에 다음줄에 있는 코드가 실행되지 않는다. 
    glutMainLoop();
        
 
    return 0;
}
cs



참고..

//http://stackoverflow.com/a/16815662

//http://ddorobot.blogspot.kr/2015/04/opencv-camera-image-in-opengl-background.html

//http://openglsamples.sourceforge.net/cube2_py.html

//https://rdmilligan.wordpress.com/2015/09/10/augmented-reality-using-opencv-and-opengl/

//http://www0.cs.ucl.ac.uk/staff/a.steed/book_tmp/CGVE/code/Lib3D1.0/maintexture.c

반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts