基于OpenGL和OpenCV的三维显示

  最近想用OpenCV+OpenGL+QT实现三维显示,但是一直都么有弄出来,今天看了一篇博客,感觉很不错,拿来分享下。

      简而言之,这段代码是如何从disparity image获得点云数据(Point cloud)并利用OpenGL显示出来。 如果想使用代码,请确认OpenGL和OpenCV都安装在电脑中。我使用的是Visual Studio 2008...(关于如何安装OPENGL 请参http://techhouse.brown.edu/~dmorris/cs148_summer_2005/handouts/OpenGL-Install-Guide.pdf)


  1. //   
  2.   
  3. //Huang,Haiqiao coded on Dec.2009  
  4.   
  5.   
  6. #include "stdafx.h"  
  7. #include <iostream>  
  8.   
  9. #include <stdlib.h>  
  10.   
  11. #include <cv.h>  
  12. #include <cxcore.h>  
  13. #include <highgui.h>  
  14. #include <math.h>  
  15.   
  16. #include <GL/glut.h>    
  17.   
  18. using namespace std;  
  19.   
  20.   
  21. float imgdata[500][500];  
  22. int w=0;  
  23. int h=0;  
  24. float scalar=50;//scalar of converting pixel color to float coordinates  
  25.   
  26. void renderScene(void) {  
  27.      
  28.    glClear (GL_COLOR_BUFFER_BIT);  
  29.    glLoadIdentity();// Reset the coordinate system before modifying   
  30.    gluLookAt (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);  
  31.    //glRotatef(-30, 0.0, 1.0, 0.0); //rotate about the x axis  
  32.    glRotatef(-180, 0.0, 0.0, 1.0); //rotate about the z axis  
  33.    glRotatef(-180, 0.0, 1.0, 0.0); //rotate about the y axis  
  34.      
  35.    float imageCenterX=w*.5;  
  36.    float imageCenterY=h*.5;  
  37.    float x,y,z;  
  38.   
  39.    glPointSize(1.0);   
  40.    glBegin(GL_POINTS);//GL_POINTS  
  41.       for (int i=0;i<h;i++){   
  42.          for (int j=0;j<w;j++){  
  43.             // color interpolation   
  44.             glColor3f(1-imgdata[i][j]/255, imgdata[i][j]/255, imgdata[i][j]/255);  
  45.             x=((float)j-imageCenterX)/scalar;  
  46.             y=((float)i-imageCenterY)/scalar;  
  47.             z=imgdata[i][j]/scalar;   
  48.             glVertex3f(x,y,z);   
  49.          }  
  50.       }  
  51.    glEnd();  
  52.    glFlush();  
  53. }  
  54.   
  55.   
  56.   
  57. void reshape (int w, int h) {  
  58.     glViewport (0, 0, (GLsizei)w, (GLsizei)h);  
  59.     glMatrixMode (GL_PROJECTION);  
  60.     glLoadIdentity ();  
  61.     gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);  
  62.     glMatrixMode (GL_MODELVIEW);  
  63. }  
  64.   
  65.   
  66. void displayDisparity(IplImage* disparity){  
  67.   
  68.    double xyscale=100;  
  69.    int j=0;  
  70.    int i=0;  
  71.    CvScalar s;  
  72.     
  73.    //accessing the image pixels  
  74.    for (i=0;i<h;i++){   
  75.       for (j=0;j<w;j++){  
  76.          s=cvGet2D(disparity,i,j);  
  77.          imgdata[i][j]=s.val[0];//for disparity is a grey image.  
  78.       }  
  79.    }   
  80. }  
  81.   
  82.   
  83. int main(int argc, char *argv[])   
  84. {    
  85.    cout << "OpenCV and OpenGL working together!"<<endl;   
  86.    char* filename="D:\\OpenCV_stuff\\SampleImages\\tsuDisparity.bmp";  
  87.    IplImage* imgGrey = cvLoadImage(filename,0); //read image as a grey one   
  88.    if (imgGrey==NULL){  
  89.       cout << "No valid image input."<<endl;   
  90.       char c=getchar();  
  91.       return 1;  
  92.    }   
  93.     w=imgGrey->width;  
  94.     h=imgGrey->height;  
  95.   
  96.    displayDisparity(imgGrey);   
  97.    cvNamedWindow("original", CV_WINDOW_AUTOSIZE );  
  98.    cvShowImage( "original", imgGrey );  
  99.   
  100.    //------------------OpenGL-------------------------  
  101.    glutInit(&argc, argv);  
  102.    glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);  
  103.    glutInitWindowPosition(100,100);  
  104.    glutInitWindowSize(500,500);  
  105.    glutCreateWindow("3D disparity image");  
  106.    glutDisplayFunc(renderScene);  
  107.   
  108.    glutReshapeFunc (reshape);  
  109.    glutMainLoop();  
  110.   
  111.   
  112.    cvWaitKey(0);   
  113.    //release opencv stuff.  
  114.    cvReleaseImage(&imgGrey);  
  115.    cvDestroyWindow("Original");  
  116.      
  117.     return 0;   
  118. }  



在此基础上,我又综合了一小段代码,随着鼠标移动,可以从多个视角观看生成的三维点云图(VS中还要加上预编译头):)

  1. #include <iostream>  
  2. #include <stdlib.h>  
  3. #include <cv.h>  
  4. #include <cxcore.h>  
  5. #include <highgui.h>  
  6. #include <math.h>  
  7. #include <gl/glut.h>  
  8. using namespace std;  
  9.   
  10. float imgdata[500][500];  
  11. int w=0;  
  12. int h=0;  
  13. float scalar=50;//scalar of converting pixel color to float coordinates  
  14.   
  15. #define pi 3.1415926  
  16. bool mouseisdown=false;  
  17. bool loopr=false;  
  18. int mx,my;  
  19. int ry=10;  
  20. int rx=10;  
  21.   
  22. void timer(int p)  
  23. {  
  24.      ry-=5;  
  25.      //marks the current window as needing to be redisplayed.  
  26.   glutPostRedisplay();  
  27.      if (loopr)  
  28.          glutTimerFunc(200,timer,0);  
  29. }  
  30.   
  31.   
  32. void mouse(int button, int state, int x, int y)  
  33. {  
  34.     if(button == GLUT_LEFT_BUTTON)  
  35.      {  
  36.         if(state == GLUT_DOWN)  
  37.          {  
  38.    mouseisdown=true;  
  39.    loopr=false;  
  40.   }  
  41.         else  
  42.   {  
  43.    mouseisdown=false;  
  44.   }  
  45.     }  
  46.   
  47.      if (button== GLUT_RIGHT_BUTTON)  
  48.      if(state == GLUT_DOWN)  
  49.      {  
  50.         loopr=true; glutTimerFunc(200,timer,0);  
  51.      }  
  52. }  
  53.   
  54. void motion(int x, int y)  
  55. {  
  56.     if(mouseisdown==true)  
  57.     {  
  58.        ry+=x-mx;  
  59.         rx+=y-my;  
  60.         mx=x;  
  61.         my=y;  
  62.         glutPostRedisplay();  
  63.     }  
  64. }  
  65.   
  66. void special(int key, int x, int y)  
  67. {  
  68.     switch(key)  
  69.     {  
  70.     case GLUT_KEY_LEFT:  
  71.         ry-=5;  
  72.         glutPostRedisplay();  
  73.         break;  
  74.     case GLUT_KEY_RIGHT:  
  75.        ry+=5;  
  76.         glutPostRedisplay();  
  77.         break;  
  78.     case GLUT_KEY_UP:  
  79.         rx+=5;  
  80.         glutPostRedisplay();  
  81.         break;  
  82.     case GLUT_KEY_DOWN:  
  83.         rx-=5;  
  84.         glutPostRedisplay();  
  85.         break;  
  86.     }  
  87. }  
  88. void renderScene(void) {  
  89.   
  90.        glClear (GL_COLOR_BUFFER_BIT);  
  91.        glLoadIdentity();// Reset the coordinate system before modifying  
  92.        gluLookAt (0.0, 0.0, 7.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0);  
  93.       //to invert the image  
  94.         glRotatef(ry,0,1,0);  
  95.         glRotatef(rx-180,1,0,0);  
  96.   
  97.        float imageCenterX=w*.5;  
  98.        float imageCenterY=h*.5;  
  99.        float x,y,z;  
  100.   
  101.        glPointSize(1.0);  
  102.        glBegin(GL_POINTS);//GL_POINTS  
  103.           for (int i=0;i<h;i++){  
  104.              for (int j=0;j<w;j++){  
  105.                 // color interpolation  
  106.                 glColor3f(1-imgdata[i][j]/255, imgdata[i][j]/255, imgdata[i][j]/255);//red,green,blue  
  107.                 x=((float)j-imageCenterX)/scalar;  
  108.                 y=((float)i-imageCenterY)/scalar;  
  109.                 z=(255-imgdata[i][j])/scalar;  
  110.                 glVertex3f(x,y,z);  
  111.              }  
  112.           }  
  113.        glEnd();  
  114.        glFlush();  
  115.     }  
  116.   
  117. void reshape (int w, int h) {  
  118.    glViewport (0, 0, (GLsizei)w, (GLsizei)h);//set viewpoint  
  119.    glMatrixMode (GL_PROJECTION);//specify which matrix is the current matrix  
  120.    glLoadIdentity ();//replace the current matrix with the identity matrix  
  121.    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);  
  122.    glMatrixMode (GL_MODELVIEW);  
  123. }  
  124.   
  125. void displayDisparity(IplImage* disparity){  
  126.    //double xyscale=100;  
  127.    int j=0;  
  128.    int i=0;  
  129.    CvScalar s;  
  130.   
  131.    //accessing the image pixels  
  132.    for (i=0;i<h;i++){  
  133.       for (j=0;j<w;j++){  
  134.          s=cvGet2D(disparity,i,j);  
  135.          imgdata[i][j]=s.val[0];//for disparity is a grey image.  
  136.          //cout << imgdata[i][j]<<endl;  
  137.           }  
  138.        }  
  139.     }  
  140.   
  141.   
  142.     int main(int argc, char *argv[])  
  143.     {  
  144.        cout << "OpenCV and OpenGL works together!"<<endl;  
  145.        char* filename=argv[1];  
  146.        IplImage* imgGrey = cvLoadImage(filename,0); //read image as a grey one  
  147.        if (imgGrey==NULL){  
  148.           cout << "No valid image input."<<endl;  
  149.           return 1;  
  150.        }  
  151.         w=imgGrey->width;  
  152.         h=imgGrey->height;  
  153.   
  154.        displayDisparity(imgGrey);  
  155.        cvNamedWindow("original", CV_WINDOW_AUTOSIZE );  
  156.        cvShowImage( "original", imgGrey );  
  157.   
  158.        //------------------OpenGL-------------------------  
  159.        glutInit(&argc, argv);//initialize the GLUT library  
  160.        glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);//sets the initial display mode  
  161.        glutInitWindowPosition(100,100);  
  162.        glutInitWindowSize(500,500);  
  163.        glutCreateWindow("3D disparity image");  
  164.        glutDisplayFunc(renderScene);  
  165.   
  166.        glutReshapeFunc (reshape);  
  167.        glutMouseFunc(mouse);  
  168.        glutMotionFunc(motion);  
  169.        glutSpecialFunc(special);  
  170.        glutMainLoop();  
  171.   
  172.   
  173.        cvWaitKey(0);  
  174.        //release opencv stuff.  
  175.        cvReleaseImage(&imgGrey);  
  176.        cvDestroyWindow("Original");  
  177.   
  178.         return 0;  
  179.     }  


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值