-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathejemplo.cpp
205 lines (159 loc) · 5.08 KB
/
ejemplo.cpp
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
#include <iostream>
#include <aruco\aruco.h>
#include <aruco\cvdrawingutils.h>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
#include <opencv2\opencv.hpp>
#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
using namespace cv;
using namespace aruco;
using namespace std;
MarkerDetector PPDetector;
vector<Marker> TheMarkers;
Mat TheInputImage, TheUndInputImage;
CameraParameters TheCameraParams;
Size TheGlWindowSize;
float TheMarkerSize = 0.05f;
void drawCubeModel()
{
static const GLfloat LightAmbient[] = { 0.25f, 0.25f, 0.25f, 1.0f }; // Ambient Light Values
static const GLfloat LightDiffuse[] = { 0.1f, 0.1f, 0.1f, 1.0f }; // Diffuse Light Values
static const GLfloat LightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f }; // Light Position
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
glEnable(GL_COLOR_MATERIAL);
glScalef(0.02f, 0.02f, 0.02f);
glTranslatef(0, 0, 1);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
const static GLfloat vertices[] = {
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f
};
const static GLfloat normals[] = {
0.0f, -1.0f, -0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
-0.0f, 0.0f, 1.0f,
-1.0f ,-0.0f, -0.0f,
0.0f, 0.0f, -1.0f
};
const static GLubyte indices[] = {
1 - 1, 2 - 1, 3 - 1, 4 - 1,
5 - 1, 8 - 1, 7 - 1, 6 - 1,
1 - 1, 5 - 1, 6 - 1, 2 - 1,
2 - 1, 6 - 1, 7 - 1, 3 - 1,
3 - 1, 7 - 1, 8 - 1, 4 - 1,
5 - 1, 1 - 1, 4 - 1, 8 - 1
};
GLfloat normales[24];
size_t index = 0;
for (size_t i = 0; i < 24; i += 4)
{
normales[i + 0] = normals[index];
normales[i + 1] = normals[index];
normales[i + 2] = normals[index];
normales[i + 3] = normals[index];
index = index >= 18 ? 0 : index++;
}
glNormalPointer(GL_FLOAT, 0, normales);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor4f(0.2f, 0.35f, 0.3f, 0.75f);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, indices);
glLineWidth(2.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor4f(0.2f, 0.65f, 0.3f, 0.35f);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, indices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
//dibujar la ecena 3D
void drawAugmentedScene()
{
double proj_matrix[16];
double modelview_matrix[16];
TheCameraParams.glGetProjectionMatrix(TheInputImage.size(), TheGlWindowSize, proj_matrix, 0.05, 10);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadMatrixd(proj_matrix);
for (unsigned int m = 0; m < TheMarkers.size(); m++)
{
TheMarkers[m].glGetModelViewMatrix(modelview_matrix);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixd(modelview_matrix);
drawCubeModel();
}
}
//dibujar la imagen 2D proveniente de la camara o video
void drawCameraFrame()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(0, TheGlWindowSize.width, 0, TheGlWindowSize.height, -1.0, 1.0);
glViewport(0, 0, TheGlWindowSize.width, TheGlWindowSize.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glPixelZoom(1, -1);
glRasterPos2i(0, TheGlWindowSize.height);
glDrawPixels(TheGlWindowSize.width, TheGlWindowSize.height, GL_BGR_EXT, GL_UNSIGNED_BYTE, TheInputImage.data);
}
//actualizar los graficos opengl
void ARDraw(void* param) {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
drawCameraFrame();
drawAugmentedScene();
glFlush();
}
//detectar las marcas con aruco y actualizar la ventana
void detectarMarcador(String ARWindowName) {
cv::undistort(TheInputImage, TheUndInputImage, TheCameraParams.CameraMatrix, TheCameraParams.Distorsion);
PPDetector.detect(TheUndInputImage, TheMarkers, TheCameraParams.CameraMatrix, Mat(), TheMarkerSize, false);
cv::updateWindow(ARWindowName);
}
int main(int argc, char **argv)
{
try
{
String ARWindowName = "Realidad Aumentada (OpenCV+ArUco)";
//crear una ventana que puede mostrar graficos 3D con OpenGL
namedWindow(ARWindowName, WINDOW_OPENGL);
setOpenGlContext(ARWindowName);
setOpenGlDrawCallback(ARWindowName, ARDraw, NULL);
//abrir la camara (usar 0) o video (ruta)
VideoCapture video("data/video.avi");
if (!video.isOpened()) { getchar(); return -1; }
double height = video.get(CAP_PROP_FRAME_HEIGHT);
double width = video.get(CAP_PROP_FRAME_WIDTH);
//hacer el tamaño de la ventana igual el tamaño de video
resizeWindow(ARWindowName, width, height);
//cargar los parametros de calibacion de la camara
TheCameraParams.readFromXMLFile("data/intrinsics.yml");
while (true) {
video >> TheInputImage;
if (TheInputImage.empty()) { break; }
TheCameraParams.resize(TheInputImage.size());
TheGlWindowSize = TheInputImage.size();
detectarMarcador(ARWindowName);
waitKey(33);
}
}
catch (std::exception &ex)
{
cout << "Exception :" << ex.what() << endl;
}
}