forked from fnoop/aruco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharuco_test_gl.cpp
279 lines (252 loc) · 8.57 KB
/
aruco_test_gl.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
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
/**
Copyright 2017 Rafael Muñoz Salinas. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY Rafael Muñoz Salinas ''AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Rafael Muñoz Salinas OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Rafael Muñoz Salinas.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glut.h>
#endif
#include "aruco.h"
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace aruco;
using namespace std;
string TheInputVideo;
string TheIntrinsicFile;
bool The3DInfoAvailable = false;
float TheMarkerSize = -1;
MarkerDetector PPDetector;
VideoCapture TheVideoCapturer;
vector<Marker> TheMarkers;
Mat TheInputImage, TheUndInputImage, TheResizedImage;
CameraParameters TheCameraParams;
Size TheGlWindowSize;
bool TheCaptureFlag = true;
bool readIntrinsicFile(string TheIntrinsicFile, Mat& TheIntriscCameraMatrix, Mat& TheDistorsionCameraParams, Size size);
void vDrawScene();
void vIdle();
void vResize(GLsizei iWidth, GLsizei iHeight);
void vMouse(int b, int s, int x, int y);
/************************************
*
*
*
*
************************************/
bool readArguments(int argc, char** argv)
{
if (argc != 4)
{
cerr << "Invalid number of arguments" << endl;
cerr << "Usage: (in.avi|live) intrinsics.yml size " << endl;
return false;
}
TheInputVideo = argv[1];
TheIntrinsicFile = argv[2];
TheMarkerSize = atof(argv[3]);
return true;
}
/************************************
*
*
*
*
************************************/
int main(int argc, char** argv)
{
try
{ // parse arguments
if (readArguments(argc, argv) == false)
return 0;
// read from camera
if (TheInputVideo == "live")
TheVideoCapturer.open(0);
else
TheVideoCapturer.open(TheInputVideo);
if (!TheVideoCapturer.isOpened())
{
cerr << "Could not open video" << endl;
return -1;
}
// read first image
TheVideoCapturer >> TheInputImage;
// read camera paramters if passed
TheCameraParams.readFromXMLFile(TheIntrinsicFile);
TheCameraParams.resize(TheInputImage.size());
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
glutInitWindowSize(TheInputImage.size().width, TheInputImage.size().height);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("AruCo");
glutDisplayFunc(vDrawScene);
glutIdleFunc(vIdle);
glutReshapeFunc(vResize);
glutMouseFunc(vMouse);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClearDepth(1.0);
TheGlWindowSize = TheInputImage.size();
vResize(TheGlWindowSize.width, TheGlWindowSize.height);
glutMainLoop();
}
catch (std::exception& ex)
{
cout << "Exception :" << ex.what() << endl;
}
}
/************************************
*
*
*
*
************************************/
void vMouse(int b, int s, int x, int y)
{
if (b == GLUT_LEFT_BUTTON && s == GLUT_DOWN)
{
TheCaptureFlag = !TheCaptureFlag;
}
}
/************************************
*
*
*
*
************************************/
void axis(float size)
{
glColor3f(1, 0, 0);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
glVertex3f(size, 0.0f, 0.0f); // ending point of the line
glEnd();
glColor3f(0, 1, 0);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
glVertex3f(0.0f, size, 0.0f); // ending point of the line
glEnd();
glColor3f(0, 0, 1);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
glVertex3f(0.0f, 0.0f, size); // ending point of the line
glEnd();
}
/************************************
*
*
*
*
************************************/
void vDrawScene()
{
if (TheResizedImage.rows == 0) // prevent from going on until the image is initialized
return;
/// clear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/// draw image in the buffer
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, TheGlWindowSize.width, 0, TheGlWindowSize.height, -1.0, 1.0);
glViewport(0, 0, TheGlWindowSize.width, TheGlWindowSize.height);
glDisable(GL_TEXTURE_2D);
glPixelZoom(1, -1);
glRasterPos3f(0, TheGlWindowSize.height - 0.5, -1.0);
glDrawPixels(TheGlWindowSize.width, TheGlWindowSize.height, GL_RGB, GL_UNSIGNED_BYTE, TheResizedImage.ptr(0));
/// Set the appropriate projection matrix so that rendering is done in a enrvironment
// like the real camera (without distorsion)
glMatrixMode(GL_PROJECTION);
double proj_matrix[16];
TheCameraParams.glGetProjectionMatrix(TheInputImage.size(), TheGlWindowSize, proj_matrix, 0.05, 10);
glLoadIdentity();
glLoadMatrixd(proj_matrix);
// now, for each marker,
double modelview_matrix[16];
for (unsigned int m = 0; m < TheMarkers.size(); m++)
{
TheMarkers[m].glGetModelViewMatrix(modelview_matrix);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixd(modelview_matrix);
axis(TheMarkerSize);
glColor3f(1, 0.4, 0.4);
glTranslatef(0, 0, TheMarkerSize / 2);
glPushMatrix();
glutWireCube(TheMarkerSize);
glPopMatrix();
}
glutSwapBuffers();
}
/************************************
*
*
*
*
************************************/
void vIdle()
{
if (TheCaptureFlag)
{
// capture image
TheVideoCapturer.grab();
TheVideoCapturer.retrieve(TheInputImage);
TheUndInputImage.create(TheInputImage.size(), CV_8UC3);
// transform color that by default is BGR to RGB because windows systems do not allow reading BGR images with
// opengl properly
cv::cvtColor(TheInputImage, TheInputImage, CV_BGR2RGB);
// remove distorion in image
cv::undistort(TheInputImage, TheUndInputImage, TheCameraParams.CameraMatrix, TheCameraParams.Distorsion);
// detect markers
PPDetector.detect(TheUndInputImage, TheMarkers, TheCameraParams.CameraMatrix, Mat(), TheMarkerSize, false);
// resize the image to the size of the GL window
cv::resize(TheUndInputImage, TheResizedImage, TheGlWindowSize);
}
glutPostRedisplay();
}
/************************************
*
*
*
*
************************************/
void vResize(GLsizei iWidth, GLsizei iHeight)
{
TheGlWindowSize = Size(iWidth, iHeight);
// not all sizes are allowed. OpenCv images have padding at the end of each line in these that are not aligned to 4
// bytes
if (iWidth * 3 % 4 != 0)
{
iWidth += iWidth * 3 % 4; // resize to avoid padding
vResize(iWidth, TheGlWindowSize.height);
}
else
{
// resize the image to the size of the GL window
if (TheUndInputImage.rows != 0)
cv::resize(TheUndInputImage, TheResizedImage, TheGlWindowSize);
}
}