1 24 (2) 3DCG TA 2012 10 12 1 OpenGL USB (3DCG) OpenCV 1.1 http://www.cyber.t.u-tokyo.ac.jp/~tani/class/mech_enshu/ 1.2 TA 1.3 USB (2012/11/19( ),20( )) USB
2 2 2 OpenGL (R,G,B,A) 2.1 OpenGL (x y) width height pixels void glreadpixels(glint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels); format (RGBA ) type GL COLOR INDEX GL RGB GL RED GL GREEN GL BLUE GL ALPHA GL LUMINANCE GL LUMINANCE ALPHA GL STENCIL INDEX GL DEPTH COMPONENT 1 glreadpixels(),gldrawpixels() glrasterpos*() width height
2.1 OpenGL 3 GL UNSIGNED BYTE GL BYTE GL UNSIGNED SHORT GL SHORT GL UNSIGNED INT GL INT GL UNSIGNED BYTE 8 8 16 16 32 32 2 glreadpixels(),gldrawpixels() void gldrawpixels(glsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels); void glrasterpos234sifd(type x, TYPE y, TYPE z, TYPE w); void glrasterpos234sifdv(type *coords); format type glreadpixels() glreadpixels() gldrawpixels() void glcopypixels(glint x, GLint y, GLsizei width, GLsizei height, GLenum buffer); buffer GL COLOR, GL STENCIL, GL DEPTH 1 4 format 2 3 glpixelstore*()
4 2 void glpixelstoreif(glenum pname TYPE param); GL UNPACK* gldrawpixels(),glteximage1d(),glteximage2d(),gltexsubimage1d(), gltexsubimage2d() GL PACK* glreadpixels(),glgetteximage() GL UNPACK SWAP BYTES, GL PACK SWAP BYTES GLboolean FALSE TRUEFALSE GL UNPACK LSB BYTES, GL PACK LSB BYTES GLboolean FALSE TRUEFALSE GL UNPACK ROW LENGTH, GL PACK ROW LENGTH GLint 0 GL UNPACK SKIP ROWS, GL PACK SKIP ROWS GLint 0 GL UNPACK SKIP PIXELS, GL PACK SKIP PIXELS GLint 0 GL UNPACK ALIGNMENT, GL PACK ALIGNMENT GLint 4 1,2,3,4 *ROW_LENGTH *SKIP_PIXELS ª æ œ *SKIP_ROWS æ œ 1 void glpixelzoom(glfloat zoom_x GLfloat zoom_y); (gldrawpixels(),glcopypixels()) x y zoom x zoom y 1.0 2.0 4
2.2 5 2.2 gldrawpixels() sample-image.c #include <GL/glut.h> #include <stdlib.h> #define checkimagewidth 64 #define checkimageheight 64 GLubyte checkimage[checkimageheight][checkimagewidth][3]; static GLdouble zoomfactor = 1.0; static GLint height; void makecheckimage(void) int i, j, c; for (i = 0; i < checkimageheight; i++) for (j = 0; j < checkimagewidth; j++) c = ((((i&0x8)==0)^((j&0x8)==0)))*255; checkimage[i][j][0] = (GLubyte) c; checkimage[i][j][1] = (GLubyte) c; checkimage[i][j][2] = (GLubyte) c; void init(void) glclearcolor (0.0, 0.0, 0.0, 0.0); glshademodel(gl_flat); makecheckimage(); glpixelstorei(gl_unpack_alignment, 1);
6 2 void display(void) glclear(gl_color_buffer_bit); glrasterpos2i(0, 0); gldrawpixels(checkimagewidth, checkimageheight, GL_RGB, GL_UNSIGNED_BYTE, checkimage); glflush(); 2.3 Independent JPEG Group s software JPEG Linux (http://www.ijg.org/) JPEG JPEG OpenGL viewjpeg.c: OpenGL readjpeg.h, readjpeg.c: JPEG testimg.jpg: JPEG Makefile TARGET OBJS = viewjpeg = viewjpeg.o readjpeg.o CC = gcc CFLAGS = -g -Wall -O2 LIBS = -lglut -lglu -lgl -ljpeg TARGET:$(OBJS) $(CC) -o $(TARGET) $(OBJS) $(LIBS).c.o: $(CC) $(FLAGS) -c $< clean: rm -f *.o *~ $(TARGET)
7 3 OpenGL 6 A N J K H A 2 H E E J E L A @ A 2 5? A A I K H B =? A B > K E @ E C I K H B =? A B M = J H A A 3.1 3 0.0...1.0 gltexcoord() void gltexcoord1,2,3,4s,i,d,f(type coords); void gltexcoord1,2,3,4s,i,d,fv(type *coords); 3.2 (sample-checker.c) glteximage2d()
8 3 3 #include <GL/glut.h> #include <stdlib.h> /* Create checkerboard texture */ #define checkimagewidth 64 #define checkimageheight 64 GLubyte checkimage[checkimagewidth][checkimageheight][3]; void makecheckimage(void) int i, j, c; for (i = 0; i < checkimagewidth; i++) for (j = 0; j < checkimageheight; j++) c = ((((i&0x8)==0)^((j&0x8)==0)))*255; checkimage[i][j][0] = (GLubyte) c; checkimage[i][j][1] = (GLubyte) c; checkimage[i][j][2] = (GLubyte) c; void myinit(void) glclearcolor (0.0, 0.0, 0.0, 0.0); glenable(gl_depth_test); gldepthfunc(gl_less); makecheckimage(); glpixelstorei(gl_unpack_alignment, 1);
3.2 9 glteximage2d(gl_texture_2d, 0, 3, checkimagewidth, checkimageheight, 0, GL_RGB, GL_UNSIGNED_BYTE, &checkimage[0][0][0]); gltexparameterf(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_CLAMP); gltexparameterf(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_CLAMP); gltexparameterf(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_LINEAR); gltexparameterf(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_LINEAR); gltexenvf(gl_texture_env, GL_TEXTURE_ENV_MODE, GL_DECAL); glenable(gl_texture_2d); glshademodel(gl_flat); void display(void) glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glbegin(gl_quads); gltexcoord2f(0.0, 0.0); glvertex3f(-2.0, -1.0, 0.0); gltexcoord2f(0.0, 1.0); glvertex3f(-2.0, 1.0, 0.0); gltexcoord2f(1.0, 1.0); glvertex3f(0.0, 1.0, 0.0); gltexcoord2f(1.0, 0.0); glvertex3f(0.0, -1.0, 0.0); gltexcoord2f(0.0, 0.0); glvertex3f(1.0, -1.0, 0.0); gltexcoord2f(0.0, 1.0); glvertex3f(1.0, 1.0, 0.0); gltexcoord2f(1.0, 1.0); glvertex3f(2.41421, 1.0, -1.41421); gltexcoord2f(1.0, 0.0); glvertex3f(2.41421, -1.0, -1.41421); glend(); glutswapbuffers(); void myreshape(int w, int h) glviewport(0, 0, w, h); glmatrixmode(gl_projection); glloadidentity(); gluperspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(0.0, 0.0, -3.6);
10 3 int main(int argc, char** argv) glutinit(&argc, argv); glutinitdisplaymode(glut_double GLUT_RGB GLUT_DEPTH); glutcreatewindow("checker"); myinit(); glutreshapefunc (myreshape); glutdisplayfunc(display); glutmainloop(); return 0; /* ANSI C requires main to return int. */ makecheckimage GLubyte (unsigned char) WIDTH HEIGHT 3 1 RGB 3 2 64 64 makecheckimage myinit glpixelstorei() image[ ] gltexparameteri, gltexparameterf GL TEXTURE MAG FILTER GL TEXTURE MIN FILTER (MAG) (MIN) GL LINEAR GL NEAREST GL TEXTURE WRAP S(T) s, t GL REPAET (GL CLAMP) 3 0.0..3.0 gltexenvf GL DECAL GL MODULATE
3.2 11 glteximage2d void glteximage2d(glenum target, GLint level, GLint format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels); target GL TEXTURE 2D GL PROXY TEXTURE 2D level 0 format (RGBA )GL RGB GL RGBA width height OpenGL 2 1 2 4 8 16 32 64 128 256 64x64 1024x1024 border 0 format pixels RGB GL RGB PNG GL RGBA type pixels GL UNSIGNED BYTE width,height,pixel width,height 2 pixels ( image) display() glenable(gl TEXTURE 2D) gldisable(gl TEXTURE 2D)
12 3 3.3 OpenGL ( ) 1. 2. 3. 4. OpenGL void glgentextures(glsizei n, GLuint *texturenames); glgentextures() n texturenames void glbindtexture(glenum target, GLuint texturename); target GL TEXTURE 1D GL TEXTURE 2D texturename glteximage2d() 2 gltexsubimage2d()
3.3 13 void gltexsubimage2d( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels ); target GL TEXTURE 2D level 0 xoffset x yoffset y (0, 0) width height format type gldrawpixels() format type pixels gltexsubimage2d() glteximage2d() 2 2 n GLU gluscaleimage() int gluscaleimage( GLenum format, GLint widthin, GLint heightin, GLenum typein, const void * datain, GLint widthout, GLint heightout, GLenum typeout, void * dataout ); format widthin heightin typein datain widthout heightout typeout dataout
14 3 1 1. sampel-checker.c sample-texbind.c 2. JPEG viewjpeg.c, readjpeg.c 3. sample-image.c JPEG 4. glut- SolidTeapot 5. GIMP
15 4 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 4.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/
16 4 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 3 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.1 17 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>
18 4 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
4.1 19 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 4 ID 1 CvCapture 1 cvqueryframe() AVI 1 IplImage IplImage NULL IplImage* cvqueryframe( CvCapture* capture ); capture
20 4 cvcreatecameracapture() cvcreatefilecapture() cvreleasecapture() void cvreleasecapture( CvCapture** capture ); capture 4.2 Window 4) 4 OpenGL OpenCV /* OpenGL */ #include <GL/glut.h> /* OpenCV */ #include <opencv/cv.h> #include <opencv/highgui.h>
4.2 21 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);
22 4 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 glrasterpos234sifd(type x, TYPE y, TYPE z, TYPE w);
4.3 23 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(); 4.3 5 : sample-texture.c 5 3 glteximage2d() 2 gltexsubimage2d()
24 4 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 6 640 480 1024 512 gltexcoord() 0.625(=640/1024) 0.9375(=480/512)
4.3 25 void gltexcoord1,2,3,4s,i,d,f(type coords); void gltexcoord1,2,3,4s,i,d,fv(type *coords); 6 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();
26 4 3 ( 5) 3 ( 7) 2 1. opencv-sample USB v4l2-sample 2. opencv-texture 5 3. opencv-divtex TODO 2.2 4. 7 5. opencv-dynamic 7 3