1 26 (2) 3DCG TA 2014 10 17 1 OpenGL USB (3DCG) OpenCV CG Augmented Reality ARToolKit 3DCG 1.1 http://www.cyber.t.u-tokyo.ac.jp/~tani/class/mech_enshu/ 1.2 TA 1.3 ( ) USB (2014/11/10( ),11( ),13( )) (2012/11/17( ), 18( )) Bluetooth USB (2) (2012/11/18( ))
2 2 2 OpenGL Linux Linux Video for Linux(Video4Linux, v4l ) API Video4Linux USB IEEE1394 API Linux Video for Linux 2(Video4Linux2, v4l2 ) OpenCV API USB (USB Video Class, UVC) USB 2.0 Linux Linux Video4Linux lsmod uvcvideo 51204 0 compat_ioctl32 5569 1 uvcvideo videodev 30785 1 uvcvideo v4l1_compat 17349 2 uvcvideo,videodev v4l2_common 20801 2 uvcvideo,videodev 2.1 OpenCV Video4Linux API OpenCV(Open Source Computer Vision Library) OpenCV OpenGL Video4Linux API OS OpenCV OS OS OpenCV OpenGL 3DCG 3DCG OpenCV OpenCV http://opencv.jp/ http://opencv.jp/sample/
2.1 3 OpenCV OpenCV IplImage OpenCV 640 480 3 RGB 8bit RGB OpenCV B G R B G R 3 1 nsize nchannels depth origin width height imagesize imagedata widthstep IplImage 1,2,3,4 1 = widthstep * height 1 IplImage cvloadimage IplImage* cvloadimage( const char* filename, int flags=cv_load_image_color ); filename flags / cvcreateimage RGB 8 24 depth 8 IPL DEPTH 8U channels 3
4 2 IplImage* cvcreateimage( CvSize size, int depth, int channels ); size depth IPL_DEPTH_8U - 8 IPL_DEPTH_8S - 8 IPL_DEPTH_16U - 16 IPL_DEPTH_16S - 16 IPL_DEPTH_32S - 32 IPL_DEPTH_32F - IPL_DEPTH_64F - channels 1, 2, 3, 4 cvreleaseimage void cvreleaseimage( IplImage** image ); image OpenGL R G B OpenCV OpenGL OpenCV OpenGL (GL BGR EXT GL BGRA ) OpenCV cvcvtcolor() CV BGR2RGB BGR RGB void cvcvtcolor( const CvArr* src, CvArr* dst, int code ); src 8 8u 16 16u 32f dst code CV_<src_color_space>2<dst_color_space>
2.1 5 OpenCV CvCapture cvcreatecameracapture() OpenCV 0 cvcreatecameracapture() NULL OpenCV cvcapturefromcam() cvcreatecameracapture() CvCapture* cvcreatecameracapture( int index ); index -1 cvcreatecameracapture() cvcreatefilecapture() CvCapture* cvcreatefilecapture( const char* filename ); filename cvsetcaptureproperty() int cvsetcaptureproperty( CvCapture* capture, int property_id, double value ); capture property_id ID
6 2 cvgetcaptureproperty() cvqueryframe() double cvgetcaptureproperty( CvCapture* capture, int property_id ); capture property_id ID ID CV CAP PROP POS MSEC CV CAP PROP POS FRAMES CV CAP PROP POS AVI RATIO CV CAP PROP FRAME WIDTH CV CAP PROP FRAME HEIGHT CV CAP PROP FPS CV CAP PROP FOURCC CV CAP PROP FRAME COUNT / 4 2 ID 1 CvCapture 1 cvqueryframe() AVI 1 IplImage IplImage NULL IplImage* cvqueryframe( CvCapture* capture ); capture
2.2 7 cvcreatecameracapture() cvcreatefilecapture() cvreleasecapture() void cvreleasecapture( CvCapture** capture ); capture 2.2 Window 1) 1 OpenGL OpenCV /* OpenGL */ #include <GL/glut.h> /* OpenCV */ #include <opencv/cv.h> #include <opencv/highgui.h>
8 2 IplImage *frame; /* Capture Imege from OpenCV */ CvCapture* capture = 0; /* Video Capture Structure */ int dev_index = 0; /* Capture Device ID */ void my_init(int argc, char **argv) { // Initialization Part - OpenCV // Connect to Capture Device capture = cvcreatecameracapture(dev_index); if (capture) { frame = cvqueryframe(capture); cap_width = cvgetcaptureproperty(capture, CV_CAP_PROP_FRAME_WIDTH); cap_height = cvgetcaptureproperty(capture, CV_CAP_PROP_FRAME_HEIGHT); else { fprintf(stderr, "Found No Camera\n"); exit(-1); // Flip Captured Image if (frame->origin==0) { cvflip(frame, frame, 0); // Convert Color Alignment BGR -> RGB cvcvtcolor(frame, frame, CV_BGR2RGB); win_width = cap_width; win_height = cap_height; int my_exit(int e) { /* Release Process - OpenCV */ if (capture) cvreleasecapture(&capture); exit(e);
2.2 9 idle cvqueryframe() OpenGL cvflip() BGR RGB cvcvtcolor() void idle(void) { if (capture) { frame = cvqueryframe(capture); if (frame->origin==0) { cvflip(frame, frame, 0 ); cvcvtcolor(frame, frame, CV_BGR2RGB); glpixelstorei( GL_UNPACK_ALIGNMENT, 1 ); glutpostredisplay(); IplImage cvcloneimage() IplImage* cvcloneimage( const IplImage* image ); image gldrawpixels() void gldrawpixels(glsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels); void glrasterpos{234{sifd(type x, TYPE y, TYPE z, TYPE w);
10 2 void display(void) { glclear(gl_color_buffer_bit); glpixelstorei(gl_unpack_alignment, 1); glpixelzoom((gldouble)(win_width-2)/(gldouble)cap_width, (GLdouble)(win_height-2)/(GLdouble)cap_height); glrasterpos2i(-win_width/2+1, -win_height/2+1); gldrawpixels(cap_width, cap_height, GL_RGB, GL_UNSIGNED_BYTE, frame->imagedata); glflush(); glutswapbuffers(); 2.3 2 : sample-texture.c 2 3 glteximage2d() 2 gltexsubimage2d()
2.3 11 void gltexsubimage2d( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels ); idle gltexsubimage2d() void idle(void) { if (capture) { frame = cvqueryframe(capture); if (frame->origin==0) { cvflip(frame, frame, 0); cvcvtcolor(frame, frame, CV_BGR2RGB); glpixelstorei( GL_UNPACK_ALIGNMENT, 1 ); glbindtexture( GL_TEXTURE_2D, tex_index ); gltexsubimage2d( GL_TEXTURE_2D, 0, 0, 0, frame->width, frame->height, GL_RGB, GL_UNSIGNED_BYTE, frame->imagedata ); glutpostredisplay(); glvertex() gltexcoord() 0.0...1.0 OpenGL 2 2 640 480 1024 512 3 640 480 1024 512 gltexcoord() 0.625(=640/1024) 0.9375(=480/512)
12 2 void gltexcoord{1,2,3,4{s,i,d,f(type coords); void gltexcoord{1,2,3,4{s,i,d,fv(type *coords); 3 void display(void) { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glcolor3f(1.0, 1.0, 1.0); glenable(gl_texture_2d); glbindtexture( GL_TEXTURE_2D, tex_index ); glpushmatrix(); glbegin(gl_triangle_fan); gltexcoord2f(0.0, 0.0); glvertex3f(-cap_width/2, -cap_height/2, 0.0); gltexcoord2f((float)cap_width/tex_width, 0.0); glvertex3f(cap_width/2, -cap_height/2, 0.0); gltexcoord2f((float)cap_width/tex_width, (float)cap_height/tex_height); glvertex3f(cap_width/2, cap_height/2, 0.0); gltexcoord2f(0.0, (float)cap_height/tex_height); glvertex3f(-cap_width/2, cap_height/2, 0.0); glend(); glpopmatrix();
2.3 13 3 ( 2) 3 ( 4) 1 1. opencv-sample USB v4l2-sample 2. opencv-texture 2 3. opencv-divtex TODO 2.2 4. 4 5. opencv-dynamic 4 3
14 3 Augmented Reality 3 Augmented Reality 3.1 Augmented Reality VR VR (AR: AugmentedReality) (MR: Mixed Reality) (HMD: Head Mounted Display) VR CG 2 (Optical See-Through) (Video See-Through) VR VR VR VR VR 5 (Optical See-Through) HMD 6 (Video See-Through)
15 4 ARToolKit ARToolKit OpenCV OpenGL AR:Augmented Reality) ARToolKit HITL Human Interface Technology Lab ARToolKit Home Page (http://www.hitl.washington.edu/artoolkit/) AR ARToolKit ARToolKit SGI IRIX, PC Mac OS X PC Windows(95/98/NT/2000/XP) ARToolKit ( ) ARToolkit OS 3DCG ( ) OpenGL GLUT 7 ARToolKit 4.1 ARToolKit ARToolKit 1 6 2 5
16 4 ARToolKit 1. 2. 3. 4. 5. VR 6. 3 ARToolKit 4.2 ARToolKit ARToolKit (simpletest(simple.c)) 8 simpletest ARToolKit Step Functions 1. Initialize the application init 2. Grab a video input frame arvideogetimage (called in mainloop) 3. Detect the markers ardetectmarker (called in mainloop) 4. Calculate camera transformation argettransmat (called in mainloop) 5. Draw the virtual objects draw (called in mainloop) 6. Close the video capture down cleanup 4 ARToolKit main, init, mainloop, draw, cleanup 5
4.2 ARToolKit 17 main main() main argmainloop() main(int argc, char *argv[]) { init(); arvideocapstart(); argmainloop( NULL, keyevent, mainloop ); 1 arvideocapstart argmainloop rendering loop main program loop init() main ARToolKit ARToolKit ( XML ) Data/camera para.dat) /* open the video path */ if( arvideoopen( vconf ) < 0 ) exit(0); /* find the size of the window */ if( arvideoinqsize(&xsize, &ysize) < 0 ) exit(0); printf("image size (x,y) = (%d,%d)\n", xsize, ysize);
18 4 ARToolKit /* set the initial camera parameters */ if( arparamload(cparaname, 1, &wparam) < 0 ) { printf("camera parameter load error!!\n"); exit(0); arparamchangesize( &wparam, xsize, ysize, &cparam ); arinitcparam( &cparam ); arparamdisp( &cparam ); ( Data/patt.hiro) if( (patt_id=arloadpatt(patt_name)) < 0 ) { printf("pattern load error!!\n"); exit(0); patt id ID /* open the graphics window */ arginit( &cparam, 1.0, 0, 0, 0, 0 ); 2 mailloop() 3 2-5 arvideogetimage /* grab a video frame */ if( (dataptr = (ARUint8 *)arvideogetimage()) == NULL ) { arutilsleep(2); return;
4.2 ARToolKit 19 argdrawmode2d(); argdispimage( dataptr, 0,0 ); ardetectmarker marker num marker info ID marker info[j].id ID marker info[j].cf (0.0 1.0) if( ardetectmarker(dataptr, thresh, &marker_info, &marker_num) < 0 ) { cleanup(); exit(0); ); arvideocapnext(); ID (k==-1) SwapBuffers /* check for object visibility */ k = -1; for( j = 0; j < marker_num; j++ ) { if( patt_id == marker_info[j].id ) { if( k == -1 ) k = j; else if( marker_info[k].cf < marker_info[j].cf ) k = j; if( k == -1 ) { argswapbuffers(); return; argettransmat() i 3x4 patt trans
20 4 ARToolKit /* get the transformation between the marker and the real camera */ argettransmat(&marker_info[k], patt_center, patt_width, patt_trans); draw() 3D draw(); argswapbuffers(); draw() draw,, ARToolKit 3D argdrawmode3d() OpenGL argdraw3dcamera() 3D argdrawmode3d(); argdraw3dcamera( 0, 0 ); glcleardepth( 1.0 ); glclear(gl_depth_buffer_bit); glenable(gl_depth_test); gldepthfunc(gl_lequal); argconvglpara ARToolKit 3x4 OpenGL (4x4 ) OpenGL /* load the camera transformation matrix */ argconvglpara(patt_trans, gl_para); glmatrixmode(gl_modelview); glloadmatrixd( gl_para ); OpenGL 25.0mm glenable(gl_lighting); glenable(gl_light0); gllightfv(gl_light0, GL_POSITION, light_position); gllightfv(gl_light0, GL_AMBIENT, ambi); gllightfv(gl_light0, GL_DIFFUSE, lightzerocolor);
4.2 ARToolKit 21 glmaterialfv(gl_front, GL_SPECULAR, mat_flash); glmaterialfv(gl_front, GL_SHININESS, mat_flash_shiny); glmaterialfv(gl_front, GL_AMBIENT, mat_ambient); glmatrixmode(gl_modelview); gltranslatef( 0.0, 0.0, 25.0 ); glutsolidcube(50.0); OpenGL gldisable( GL_LIGHTING ); gldisable( GL_DEPTH_TEST ); cleanup() cleenup video path arvideocapstop(); arvideoclose(); argcleanup();
22 4 ARToolKit 4.3 ARToolKit OpenGL ARToolKit 3 OpenGL 9 ARToolKit argettransmat() patt trans(3x4 ) r 11 r 33 t x, t y, t z ARToolKit OpenGL 3 OpenGL patt trans = r 11 r 12 r 13 t x r 21 r 22 r 23 t y (1) r 31 r 32 r 33 t z argconvglpara() OpenGL gl para 4x4 argconvglpara(patt_trans, gl_para); glmatrixmode(gl_modelview); glloadmatrixd( gl_para ); r 11 r 12 r 13 t x gl para = r 21 r 22 r 23 t y r 31 r 32 r 33 t z (2) 0 0 0 1 OpenGL X c Y c Z c 1 r 11 r 12 r 13 t x X m = r 21 r 22 r 23 t y Y m r 31 r 32 r 33 t z Z m (3) 0 0 0 1 1 AR OpenGL
4.4 ARToolKit 23 double patt_width = 80.0 mm CG mm 50mm glutsolidcube(50.0) glmatrixmode(gl_modelview); gltranslatef( 0.0, 0.0, 25.0 ); glutsolidcube(50.0); 4.4 ARToolKit ARToolKit Linux ARToolKit $ cd ARToolkit $./Configure Configure 1 3 2 n make $ make ( )/ARToolkit/lib ( )/ARToolkit/bin ( )/ARToolkit/examples bin
24 4 ARToolKit 2 1. samplelite.c Hiro 2. 3. 4. ( ) http://www.hitl.washington.edu/artoolkit/documentation/devmulti.htm 10 11