aruco_test_gl.cpp
Go to the documentation of this file.
1 /*****************************
2 Copyright 2011 Rafael Muñoz Salinas. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without modification, are
5 permitted provided that the following conditions are met:
6 
7  1. Redistributions of source code must retain the above copyright notice, this list of
8  conditions and the following disclaimer.
9 
10  2. Redistributions in binary form must reproduce the above copyright notice, this list
11  of conditions and the following disclaimer in the documentation and/or other materials
12  provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY Rafael Muñoz Salinas ''AS IS'' AND ANY EXPRESS OR IMPLIED
15 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Rafael Muñoz Salinas OR
17 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 The views and conclusions contained in the software and documentation are those of the
25 authors and should not be interpreted as representing official policies, either expressed
26 or implied, of Rafael Muñoz Salinas.
27 ********************************/
28 #include <iostream>
29 
30 #include <fstream>
31 #include <sstream>
32 #ifdef __APPLE__
33 #include <GLUT/glut.h>
34 #else
35 #include <GL/gl.h>
36 #include <GL/glut.h>
37 #endif
38 #include <opencv2/highgui/highgui.hpp>
39 #include <opencv2/imgproc/imgproc.hpp>
40 #include "aruco.h"
41 using namespace cv;
42 using namespace aruco;
43 
46 bool The3DInfoAvailable=false;
47 float TheMarkerSize=-1;
49 VideoCapture TheVideoCapturer;
50 vector<Marker> TheMarkers;
54 bool TheCaptureFlag=true;
55 bool readIntrinsicFile(string TheIntrinsicFile,Mat & TheIntriscCameraMatrix,Mat &TheDistorsionCameraParams,Size size);
56 
57 void vDrawScene();
58 void vIdle();
59 void vResize( GLsizei iWidth, GLsizei iHeight );
60 void vMouse(int b,int s,int x,int y);
61 
62 
63 /************************************
64  *
65  *
66  *
67  *
68  ************************************/
69 
70 bool readArguments ( int argc,char **argv )
71 {
72  if (argc!=4) {
73  cerr<<"Invalid number of arguments"<<endl;
74  cerr<<"Usage: (in.avi|live) intrinsics.yml size "<<endl;
75  return false;
76  }
77  TheInputVideo=argv[1];
78  TheIntrinsicFile=argv[2];
79  TheMarkerSize=atof(argv[3]);
80  return true;
81 }
82 
83 
84 /************************************
85  *
86  *
87  *
88  *
89  ************************************/
90 
91 int main(int argc,char **argv)
92 {
93  try
94  {//parse arguments
95  if (readArguments (argc,argv)==false) return 0;
96  //read from camera
97  if (TheInputVideo=="live") TheVideoCapturer.open(0);
99  if (!TheVideoCapturer.isOpened())
100  {
101  cerr<<"Could not open video"<<endl;
102  return -1;
103 
104  }
105 
106  //read first image
108  //read camera paramters if passed
109  TheCameraParams.readFromXMLFile(TheIntrinsicFile);
110  TheCameraParams.resize(TheInputImage.size());
111 
112  glutInit(&argc, argv);
113  glutInitWindowPosition( 0, 0);
114  glutInitWindowSize(TheInputImage.size().width,TheInputImage.size().height);
115  glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
116  glutCreateWindow( "AruCo" );
117  glutDisplayFunc( vDrawScene );
118  glutIdleFunc( vIdle );
119  glutReshapeFunc( vResize );
120  glutMouseFunc(vMouse);
121  glClearColor( 0.0, 0.0, 0.0, 1.0 );
122  glClearDepth( 1.0 );
123  TheGlWindowSize=TheInputImage.size();
125  glutMainLoop();
126 
127  } catch (std::exception &ex)
128 
129  {
130  cout<<"Exception :"<<ex.what()<<endl;
131  }
132 
133 }
134 /************************************
135  *
136  *
137  *
138  *
139  ************************************/
140 
141 void vMouse(int b,int s,int x,int y)
142 {
143  if (b==GLUT_LEFT_BUTTON && s==GLUT_DOWN) {
145  }
146 
147 }
148 
149 /************************************
150  *
151  *
152  *
153  *
154  ************************************/
155 void axis(float size)
156 {
157  glColor3f (1,0,0 );
158  glBegin(GL_LINES);
159  glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
160  glVertex3f(size,0.0f, 0.0f); // ending point of the line
161  glEnd( );
162 
163  glColor3f ( 0,1,0 );
164  glBegin(GL_LINES);
165  glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
166  glVertex3f( 0.0f,size, 0.0f); // ending point of the line
167  glEnd( );
168 
169 
170  glColor3f (0,0,1 );
171  glBegin(GL_LINES);
172  glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
173  glVertex3f(0.0f, 0.0f, size); // ending point of the line
174  glEnd( );
175 
176 
177 }
178 /************************************
179  *
180  *
181  *
182  *
183  ************************************/
185 {
186  if (TheResizedImage.rows==0) //prevent from going on until the image is initialized
187  return;
189  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
191  glMatrixMode(GL_MODELVIEW);
192  glLoadIdentity();
193  glMatrixMode(GL_PROJECTION);
194  glLoadIdentity();
195  glOrtho(0, TheGlWindowSize.width, 0, TheGlWindowSize.height, -1.0, 1.0);
196  glViewport(0, 0, TheGlWindowSize.width , TheGlWindowSize.height);
197  glDisable(GL_TEXTURE_2D);
198  glPixelZoom( 1, -1);
199  glRasterPos3f( 0, TheGlWindowSize.height - 0.5, -1.0 );
200  glDrawPixels ( TheGlWindowSize.width , TheGlWindowSize.height , GL_RGB , GL_UNSIGNED_BYTE , TheResizedImage.ptr(0) );
202  //like the real camera (without distorsion)
203  glMatrixMode(GL_PROJECTION);
204  double proj_matrix[16];
205  TheCameraParams.glGetProjectionMatrix(TheInputImage.size(),TheGlWindowSize,proj_matrix,0.05,10);
206  glLoadIdentity();
207  glLoadMatrixd(proj_matrix);
208 
209  //now, for each marker,
210  double modelview_matrix[16];
211  for (unsigned int m=0;m<TheMarkers.size();m++)
212  {
213  TheMarkers[m].glGetModelViewMatrix(modelview_matrix);
214  glMatrixMode(GL_MODELVIEW);
215  glLoadIdentity();
216  glLoadMatrixd(modelview_matrix);
217 
218 
220 
221  glColor3f(1,0.4,0.4);
222  glTranslatef(0, 0, TheMarkerSize/2);
223  glPushMatrix();
224  glutWireCube( TheMarkerSize );
225 
226  glPopMatrix();
227  }
228 
229  glutSwapBuffers();
230 
231 }
232 
233 
234 /************************************
235  *
236  *
237  *
238  *
239  ************************************/
240 void vIdle()
241 {
242  if (TheCaptureFlag) {
243  //capture image
244  TheVideoCapturer.grab();
245  TheVideoCapturer.retrieve( TheInputImage);
246  TheUndInputImage.create(TheInputImage.size(),CV_8UC3);
247  //transform color that by default is BGR to RGB because windows systems do not allow reading BGR images with opengl properly
248  cv::cvtColor(TheInputImage,TheInputImage,CV_BGR2RGB);
249  //remove distorion in image
250  cv::undistort(TheInputImage,TheUndInputImage, TheCameraParams.CameraMatrix, TheCameraParams.Distorsion);
251  //detect markers
252  PPDetector.detect(TheUndInputImage,TheMarkers, TheCameraParams.CameraMatrix,Mat(),TheMarkerSize,false);
253  //resize the image to the size of the GL window
255  }
256  glutPostRedisplay();
257 }
258 
259 
260 /************************************
261  *
262  *
263  *
264  *
265  ************************************/
266 void vResize( GLsizei iWidth, GLsizei iHeight )
267 {
268  TheGlWindowSize=Size(iWidth,iHeight);
269  //not all sizes are allowed. OpenCv images have padding at the end of each line in these that are not aligned to 4 bytes
270  if (iWidth*3%4!=0) {
271  iWidth+=iWidth*3%4;//resize to avoid padding
272  vResize(iWidth,TheGlWindowSize.height);
273  }
274  else {
275  //resize the image to the size of the GL window
276  if (TheUndInputImage.rows!=0)
278  }
279 }
280 
bool readIntrinsicFile(string TheIntrinsicFile, Mat &TheIntriscCameraMatrix, Mat &TheDistorsionCameraParams, Size size)
Definition: common.h:46
bool TheCaptureFlag
void resize(cv::Size size)
MarkerDetector PPDetector
bool readArguments(int argc, char **argv)
f
void vResize(GLsizei iWidth, GLsizei iHeight)
int main(int argc, char **argv)
void glGetProjectionMatrix(cv::Size orgImgSize, cv::Size size, double proj_matrix[16], double gnear, double gfar, bool invert=false)
std::vector< aruco::Marker > detect(const cv::Mat &input)
void vIdle()
void vDrawScene()
void readFromXMLFile(string filePath)
void vMouse(int b, int s, int x, int y)
string TheIntrinsicFile
Mat TheResizedImage
CameraParameters TheCameraParams
Size TheGlWindowSize
VideoCapture TheVideoCapturer
float TheMarkerSize
Main class for marker detection.
Parameters of the camera.
vector< Marker > TheMarkers
string TheInputVideo
Mat TheUndInputImage
bool The3DInfoAvailable
Mat TheInputImage
void axis(float size)


tuw_aruco
Author(s): Lukas Pfeifhofer
autogenerated on Mon Jun 10 2019 15:40:45