opengl+opencv-WebCam as Textures-Demo

Started by
-1 comments, last by nomads 14 years, 11 months ago
hi, heres a little linux-demo. with opengl (3d-texture) + opencv(acess to the webcam) you can see, turnable 3d-cabe with mouse-movements and the textures a live-pics from your webcam. i'm new here and dont find stuff for attachedments so here is the code regards nomads /************************************************************************************/ // cvgl_3dDemo2.cpp // ----------------- // // begin : Thur. 14.May 11.45:00 GMT 2009 // copyright : (C) 2008/2009 by s.morf // email : stevenmorf@bluewin.ch // // compile with // opencv & opengl: // g++ -I/usr/include/GL -I/usr/local/include/opencv -L /usr/local/lib -lcxcore -lcv -lhighgui -lcvaux -lml -I/usr/X11R6/include -L/usr/X11R6/lib -o cvgl_3dDemo2 cvgl_3dDemo2.cpp -lglui -lglut -lGLU -lGL // // run as : ./cvgl_3dDemo2 // // for: Basics of: ComputerVisions // - basics with // - opencv(webcam /images)with logitech QuickCam(s) E3500 Plus // - opengl (3d-cube movements with mouse, you can look inside of the cube // /*********************************************************************************************** // References // // Gordon Wetzstein // The University of British Columbia // wetzste1@cs.ubc.ca // @author Gordon Wetzstein, now @ The University of British Columbia [wetzste1@cs.ubc.ca] // @date 09/11/06 // // This is just a simple test for combining OpenCV's image loadig interface // and OpenGL. // // verwendete programme: // from /root/Desktop/OPENCV/opencv-tutorial.pdf (german) // <Bildverarbeitung mit OpenCV, author Johannes Wienke, Uni Bielefeld 2008> // // OpenCV_Beispiele From RoboWeb // Mehrere Bilder in einem Fenster // from http://opencvlibrary.sourceforge.net/DisplayManyImages. Display video from webcam Author Nash License GPL Website http:/nashruddin.com /************************************************************************************************ REFERNECE STARTING WEBCAM: 1) on console if you have to reset you cam for any reason, do: 1. unplug your cam 2. modprobe -r uvcvideo 3. modprobe -r snd_usb_audio 4. replug your cam **************************************************************************************************/ #include <GL/freeglut.h> #include <GL/gl.h> #include "cv.h" #include "highgui.h" #include <stdio.h> #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cmath> #ifndef random #define random rand #endif #include <GL/glut.h> #include <ctype.h> #include "cv.h" #include "highgui.h" #include <iostream> //***********************************************************************************************/ // GLUT callbacks and functions void initGlut(int argc, char **argv); void displayFunc(void); void idleFunc(void); void reshapeFunc(int width, int height); void mouseFunc(int button, int state, int x, int y); void mouseMotionFunc(int x, int y); void keyboardFunc(unsigned char key, int x, int y); void specialFunc(int key, int x, int y); /***********************************************************************************************/ // other [OpenGL] functions void countFrames(void); void renderBitmapString(float x, float y, float z, void *font, char *string); /***********************************************************************************************/ bool bFullsreen = false; int nWindowID; /***********************************************************************************************/ // parameters for the framecounter nicht verwendet char pixelstring[30]; int cframe = 0; int timebase = 0; /***********************************************************************************************/ // camera attributes float viewerPosition[3] = { 0.0, 0.0, -50.0 }; float viewerDirection[3] = { 0.0, 0.0, 0.0 }; float viewerUp[3] = { 0.0, 1.0, 0.0 }; // rotation values for the navigation float navigationRotation[3] = { 0.0, 0.0, 0.0 }; // parameters for the navigation // position of the mouse when pressed int mousePressedX = 0, mousePressedY = 0; float lastXOffset = 0.0, lastYOffset = 0.0, lastZOffset = 0.0; // mouse button states int leftMouseButtonActive = 0, middleMouseButtonActive = 0, rightMouseButtonActive = 0; // modifier state int shiftActive = 0, altActive = 0, ctrlActive = 0; /***********************************************************************************************/ // OpenCV variables IplImage *image = 0; GLuint cameraImageTextureID; // here cv_cam-functions CvCapture *capture = 0; // IplImage *frame = 0; -> image int key = 0; /***********************************************************************************************/ bool bInit = false; /***********************************************************************************************/ // new stuff from lk_glDemo.cpp void displayFunc(void) { // new stuff IplImage *frame = 0; frame = cvQueryFrame( capture ); // initialze OpenGL texture glEnable(GL_TEXTURE_RECTANGLE_ARB); glGenTextures(1, &cameraImageTextureID); glBindTexture(GL_TEXTURE_RECTANGLE_ARB, cameraImageTextureID); glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); if(frame->nChannels == 3) { glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, frame->width, frame->height, 0, GL_BGR, GL_UNSIGNED_BYTE, frame->imageData); } else if(frame->nChannels == 4) { glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, frame->width, frame->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, frame->imageData); } if(frame) { // clear the buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glEnable(GL_TEXTURE_RECTANGLE_ARB); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.0, 1.33, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef( viewerPosition[0], viewerPosition[1], viewerPosition[2]); // add navigation rotation glRotatef( navigationRotation[0], 1.0f, 0.0f, 0.0f ); glRotatef( navigationRotation[1], 0.0f, 1.0f, 0.0f ); // bind texture glBindTexture(GL_TEXTURE_RECTANGLE_ARB, cameraImageTextureID); // draw 4 rectangles glBegin(GL_QUADS); glTexCoord2i(0,frame->height); glVertex3f(-15.0,-15.0, 15.0); glTexCoord2i(frame->width,frame->height); glVertex3f(15.0,-15.0, 15.0); glTexCoord2i(frame->width,0); glVertex3f(15.0,15.0, 15.0); glTexCoord2i(0,0); glVertex3f(-15.0,15.0, 15.0); glEnd(); glBegin(GL_QUADS); glTexCoord2i(0,frame->height); glVertex3f(15.0,-15.0, -15.0); glTexCoord2i(frame->width,frame->height); glVertex3f(15.0,-15.0, 15.0); glTexCoord2i(frame->width,0); glVertex3f(15.0,15.0, 15.0); glTexCoord2i(0,0); glVertex3f(15.0,15.0, -15.0); glEnd(); glBegin(GL_QUADS); glTexCoord2i(0,frame->height); glVertex3f(15.0,-15.0, -15.0); glTexCoord2i(frame->width,frame->height); glVertex3f(-15.0,-15.0, -15.0); glTexCoord2i(frame->width,0); glVertex3f(-15.0,15.0, -15.0); glTexCoord2i(0,0); glVertex3f(15.0,15.0, -15.0); glEnd(); glBegin(GL_QUADS); glTexCoord2i(0,frame->height); glVertex3f(-15.0,-15.0, -15.0); glTexCoord2i(frame->width,frame->height); glVertex3f(-15.0,-15.0, 15.0); glTexCoord2i(frame->width,0); glVertex3f(-15.0,15.0, 15.0); glTexCoord2i(0,0); glVertex3f(-15.0,15.0, -15.0); glEnd(); } // end image glDisable(GL_TEXTURE_RECTANGLE_ARB); countFrames(); glutSwapBuffers(); } /***********************************************************************************************/ void idleFunc(void) { glutPostRedisplay(); } /***********************************************************************************************/ void reshapeFunc(int width, int height) { glViewport(0, 0, width, height); } /***********************************************************************************************/ // mouse callback void mouseFunc(int button, int state, int x, int y) { // get the mouse buttons if(button == GLUT_LEFT_BUTTON) if(state == GLUT_DOWN) { leftMouseButtonActive += 1; } else leftMouseButtonActive -= 1; else if(button == GLUT_MIDDLE_BUTTON) if(state == GLUT_DOWN) { middleMouseButtonActive += 1; lastXOffset = 0.0; lastYOffset = 0.0; } else middleMouseButtonActive -= 1; else if(button == GLUT_RIGHT_BUTTON) if(state == GLUT_DOWN) { rightMouseButtonActive += 1; lastZOffset = 0.0; } else rightMouseButtonActive -= 1; mousePressedX = x; mousePressedY = y; } /***********************************************************************************************/ void mouseMotionFunc(int x, int y) { float xOffset = 0.0, yOffset = 0.0, zOffset = 0.0; // navigation // rotatation if(leftMouseButtonActive) { navigationRotation[0] += ((mousePressedY - y) * 180.0f) / 200.0f; navigationRotation[1] += ((mousePressedX - x) * 180.0f) / 200.0f; mousePressedY = y; mousePressedX = x; } // panning else if(middleMouseButtonActive) { xOffset = (mousePressedX + x); if(!lastXOffset == 0.0) { viewerPosition[0] -= (xOffset - lastXOffset) / 8.0; viewerDirection[0] -= (xOffset - lastXOffset) / 8.0; } lastXOffset = xOffset; yOffset = (mousePressedY + y); if(!lastYOffset == 0.0) { viewerPosition[1] += (yOffset - lastYOffset) / 8.0; viewerDirection[1] += (yOffset - lastYOffset) / 8.0; } lastYOffset = yOffset; } // depth movement else if (rightMouseButtonActive) { zOffset = (mousePressedX + x); if(!lastZOffset == 0.0) { viewerPosition[2] -= (zOffset - lastZOffset) / 5.0; viewerDirection[2] -= (zOffset - lastZOffset) / 5.0; } lastZOffset = zOffset; } } /***********************************************************************************************/ void keyboardFunc(unsigned char key, int x, int y) { switch(key) { // ----------------------------------------- #ifdef WIN32 // exit on escape case '\033': if(bInit) { if(image) cvReleaseImage(ℑ); glDeleteTextures(1, &cameraImageTextureID); } exit(0); break; #endif // ----------------------------------------- /* nicht benutzt // switch to fullscreen case 'f': bFullsreen = !bFullsreen; if(bFullsreen) glutFullScreen(); else { glutSetWindow(nWindowID); glutPositionWindow(100, 100); glutReshapeWindow(640, 480); } break; // ----------------------------------------- */ } } /***********************************************************************************************/ void specialFunc(int key, int x, int y) { //printf("key pressed: %d\n", key); } /***********************************************************************************************/ void countFrames(void) { /* wrong timing time=glutGet(GLUT_ELAPSED_TIME); cframe++; if(time - timebase > 50) { sprintf(pixelstring, "fps: %4.2f", cframe*1000.0/(time-timebase)); timebase = time; cframe = 0; // Draw status text and uni-logo: } */ glDisable(GL_LIGHTING); glColor4f(1.0,1.0,1.0,1.0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0, 200, 0, 200); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); // render the string renderBitmapString(5,5,0.0,GLUT_BITMAP_HELVETICA_10,pixelstring); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); } /***********************************************************************************************/ void renderBitmapString(float x, float y, float z, void *font, char *string) { char *c; glRasterPos3f(x, y,z); for(c=string; *c != '\0'; c++) { glutBitmapCharacter(font, *c); } } /***********************************************************************************************/ int main(int argc, char **argv) { // initialize camera capture = cvCaptureFromCAM( 0 ); // always check if( !capture ) { printf( "Cannot open initialize WebCam!\n" ); return 1; } glutInit(&argc,argv); glutInitWindowSize (640, 480); glutInitWindowPosition(100, 100); glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("FaceTracking"); glutReshapeFunc(reshapeFunc); glutKeyboardFunc(keyboardFunc); glutSpecialFunc(specialFunc); glutMouseFunc(mouseFunc); glutMotionFunc(mouseMotionFunc); // Register callbacks: glutDisplayFunc(displayFunc); glutIdleFunc(displayFunc); glutMainLoop(); return (1); } /***********************************************************************************************/

This topic is closed to new replies.

Advertisement