32 #include <GLUT/glut.h> 43 #include <opencv2/highgui/highgui.hpp> 44 #include <opencv2/imgproc/imgproc.hpp> 45 #include <opencv2/calib3d/calib3d.hpp> 49 void __glGetModelViewMatrix(
double modelview_matrix[16],
const cv::Mat &Rvec,
const cv::Mat &Tvec)
throw(cv::Exception) ;
51 using namespace aruco;
67 void vResize( GLsizei iWidth, GLsizei iHeight );
68 void vMouse(
int b,
int s,
int x,
int y);
79 int main(
int argc,
char **argv)
84 cerr<<
"Invalid number of arguments"<<endl;
85 cerr<<
"Usage: (in.avi|live) markermap_config.yml intrinsics.yml size "<<endl;
86 cerr<<
"WARNING: this test creates a synthetic mask consisting of a single rectangle. "<<endl;
87 cerr<<
"WARNING: The only purpose is to show how to create an AR application with mask in OpenGL "<<endl;
93 TheMarkerSize=atof(argv[4]);
97 MMPoseTracker.
setParams(TheCameraParams,TheMMConfig);
99 if (
string(argv[1])==
"live")
100 TheVideoCapturer.open(0);
101 else TheVideoCapturer.open(argv[1]);
103 if (!TheVideoCapturer.isOpened())
throw std::runtime_error(
"could not open video");
109 TheCameraParams.
resize( TheInputImage.size());
113 glutInit(&argc, argv);
114 glutInitWindowPosition( 0, 0);
115 glutInitWindowSize(TheInputImage.size().width,TheInputImage.size().height);
116 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
117 glutCreateWindow(
"ArUco" );
119 glutIdleFunc(
vIdle );
122 glClearColor( 0.0, 0.0, 0.0, 1.0 );
126 glEnable( GL_ALPHA_TEST );
127 glAlphaFunc( GL_GREATER, 0.5 );
129 TheGlWindowSize=TheInputImage.size();
130 vResize(TheGlWindowSize.width,TheGlWindowSize.height);
133 }
catch (std::exception &ex)
136 cout<<
"Exception :"<<ex.what()<<endl;
153 cv::Mat mask(img.size(), CV_8UC1, cv::Scalar::all(255));
154 cv::rectangle(mask,cv::Rect(img.cols/4, img.rows/4, img.cols/2, img.rows/2), cv::Scalar(0), CV_FILLED);
170 cv::Mat out(img.size(), CV_8UC4, cv::Scalar::all(0));
171 for(
int i=0; i<img.total(); i++) {
172 for(
int j=0; j<3; j++) out.ptr<cv::Vec4b>()[i][j] = img.ptr<cv::Vec3b>()[i][j];
173 if(mask.size()==img.size()) out.ptr<cv::Vec4b>()[i][3] = mask.ptr<
unsigned char>()[i];
194 if (b==GLUT_LEFT_BUTTON && s==GLUT_DOWN) {
210 glVertex3f(0.0
f, 0.0
f, 0.0
f);
211 glVertex3f(size,0.0
f, 0.0
f);
216 glVertex3f(0.0
f, 0.0
f, 0.0
f);
217 glVertex3f( 0.0
f,size, 0.0
f);
223 glVertex3f(0.0
f, 0.0
f, 0.0
f);
224 glVertex3f(0.0
f, 0.0
f, size);
237 if (TheResizedImage.rows==0)
240 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
242 glMatrixMode(GL_MODELVIEW);
244 glMatrixMode(GL_PROJECTION);
246 glOrtho(0, TheGlWindowSize.width, 0, TheGlWindowSize.height, -1.0, 1.0);
247 glViewport(0, 0, TheGlWindowSize.width , TheGlWindowSize.height);
248 glDisable(GL_TEXTURE_2D);
250 glRasterPos3f( 0, TheGlWindowSize.height - 0.5, -1.0 );
251 glDrawPixels ( TheGlWindowSize.width , TheGlWindowSize.height , GL_RGB , GL_UNSIGNED_BYTE , TheResizedImage.ptr(0) );
254 glMatrixMode(GL_PROJECTION);
255 double proj_matrix[16];
258 glLoadMatrixd(proj_matrix);
261 double modelview_matrix[16];
280 glMatrixMode(GL_MODELVIEW);
282 glLoadMatrixd(modelview_matrix);
285 cerr<<TheMarkerSize<<endl;cin.ignore();
288 glTranslatef(0,0,TheMarkerSize/2);
291 glutWireTeapot(2*TheMarkerSize);
298 glDrawPixels ( TheGlWindowSize.width , TheGlWindowSize.height , GL_RGBA , GL_UNSIGNED_BYTE , multiChannelMask.ptr(0) );
313 if (TheCaptureFlag) {
315 TheVideoCapturer.grab();
316 TheVideoCapturer.retrieve( TheInputImage);
317 TheUndInputImage.create(TheInputImage.size(),CV_8UC3);
319 cv::cvtColor(TheInputImage,TheInputImage,CV_BGR2RGB);
323 TheMarkers=MDetector.
detect(TheUndInputImage);
326 cv::resize(TheUndInputImage,TheResizedImage,TheGlWindowSize);
340 void vResize( GLsizei iWidth, GLsizei iHeight )
342 TheGlWindowSize=Size(iWidth,iHeight);
346 vResize(iWidth,TheGlWindowSize.height);
350 if (TheUndInputImage.rows!=0)
351 cv::resize(TheUndInputImage,TheResizedImage,TheGlWindowSize);
357 assert(Tvec.type()==CV_32F);
359 Mat Rot(3, 3, CV_32FC1), Jacob;
360 Rodrigues(Rvec, Rot, Jacob);
363 for (
int i = 0; i < 3; i++)
364 for (
int j = 0; j < 3; j++)
365 para[i][j] = Rot.at<
float >(i, j);
367 para[0][3] = Tvec.ptr<
float >(0)[0];
368 para[1][3] = Tvec.ptr<
float >(0)[1];
369 para[2][3] = Tvec.ptr<
float >(0)[2];
372 modelview_matrix[0 + 0 * 4] = para[0][0];
374 modelview_matrix[0 + 1 * 4] = para[0][1];
375 modelview_matrix[0 + 2 * 4] = para[0][2];
376 modelview_matrix[0 + 3 * 4] = para[0][3];
378 modelview_matrix[1 + 0 * 4] = para[1][0];
379 modelview_matrix[1 + 1 * 4] = para[1][1];
380 modelview_matrix[1 + 2 * 4] = para[1][2];
381 modelview_matrix[1 + 3 * 4] = para[1][3];
383 modelview_matrix[2 + 0 * 4] = -para[2][0];
384 modelview_matrix[2 + 1 * 4] = -para[2][1];
385 modelview_matrix[2 + 2 * 4] = -para[2][2];
386 modelview_matrix[2 + 3 * 4] = -para[2][3];
387 modelview_matrix[3 + 0 * 4] = 0.0;
388 modelview_matrix[3 + 1 * 4] = 0.0;
389 modelview_matrix[3 + 2 * 4] = 0.0;
390 modelview_matrix[3 + 3 * 4] = 1.0;
392 modelview_matrix[12] *= scale;
393 modelview_matrix[13] *= scale;
394 modelview_matrix[14] *= scale;
void vResize(GLsizei iWidth, GLsizei iHeight)
void setThresholdParams(double param1, double param2)
const cv::Mat getTvec() const
void resize(cv::Size size)
bool isExpressedInPixels() const
int main(int argc, char **argv)
const cv::Mat getRvec() const
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 readFromFile(string sfile)
cv::Mat getRTMatrix() const
bool estimatePose(const vector< Marker > &v_m)
void readFromXMLFile(string filePath)
void setParams(const CameraParameters &cam_params, const MarkerMap &msconf, float markerSize=-1)
MarkerDetector TheMarkerDetector
cv::Mat createSyntheticMask(const cv::Mat &img)
MarkerMap convertToMeters(float markerSize)
void __glGetModelViewMatrix(double modelview_matrix[16], const cv::Mat &Rvec, const cv::Mat &Tvec)
Main class for marker detection.
Parameters of the camera.
vector< Marker > TheMarkers
MarkerMapPoseTracker MMPoseTracker
VideoCapture TheVideoCapturer
cv::Mat createMultiChannelMask(const cv::Mat &img, const cv::Mat &mask)
This class defines a set of markers whose locations are attached to a common reference system...
CameraParameters TheCameraParams
void vMouse(int b, int s, int x, int y)