关闭

opencv与opengl混用实现三维点云图像

464人阅读 评论(0) 收藏 举报
分类:

/*

灰度图转换为高度图,为双目视觉三维重建做准备。

*/

#include <iostream>  

#include <stdlib.h>  
//#include <cv.h>  
//#include <cxcore.h>  
//#include <highgui.h>  
#include "opencv2/calib3d/calib3d.hpp"    
#include "opencv2/imgproc/imgproc.hpp"    
#include "opencv2/highgui/highgui.hpp"    
#include "opencv2/contrib/contrib.hpp"   
#include <cmath>  
#include <glut.h>    
#include <iostream>  

#pragma comment(lib,"opencv_highgui248.lib")    
#pragma comment(lib,"opencv_core248.lib")    
#pragma comment(lib,"opencv_imgproc248.lib")    




using namespace cv;
using namespace std;


#define MAX_SIZE 1024  


float imgdata[MAX_SIZE][MAX_SIZE];


int w = 0;
int h = 0;
float scalar = 50;


void renderScene(void)
{


glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();              
gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(-90, 0.0, 1.0, 0.0);
glRotatef(-90, 0.0, 0.0, 1.0); 
glRotatef(-180, 0.0, 1.0, 0.0);


float imageCenterX = w*0.5;
float imageCenterY = h*0.5;
float x, y, z;
glPointSize(1.0);
glBegin(GL_POINTS);//GL_POINTS  
for (int i = 0; i<h; i++)
{
for (int j = 0; j<w; j++)
{
// color interpolation  
glColor3f(1 - imgdata[i][j] / 255, imgdata[i][j] / 255, imgdata[i][j] / 255);
x = ((float)j - imageCenterX) / scalar;
y = ((float)i - imageCenterY) / scalar;
z = imgdata[i][j] / scalar;
glVertex3f(x, y, z);
}
}
glEnd();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}


void displayDisparity(IplImage* disparity)
{
double xyscale = 100;
int j = 0;
int i = 0;
CvScalar s;


//accessing the image pixels  
for (i = 0; i<h; i++)
{
for (j = 0; j<w; j++)
{
s = cvGet2D(disparity, i, j);
imgdata[i][j] = s.val[0];//for disparity is a grey image.  
}
}
}
int main(int argc,char *argv[])
{
string imgname="1.jpg";
IplImage* grey = cvLoadImage(imgname.c_str(), 1); //read image as a grey one  
if (grey == NULL)
{
cout << "No valid image input." << endl;
char c = getchar();
return 1;
}
w = grey->width;
h = grey->height;


displayDisparity(grey);
cvNamedWindow("original", CV_WINDOW_AUTOSIZE);
cvShowImage("original", grey);


//------------------OpenGL-------------------------  
glutInit(&argc, (char**)argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("3D disparity image");
glutDisplayFunc(renderScene);
glutReshapeFunc(reshape);
glutMainLoop();
cvWaitKey(0);
//release opencv stuff.  
cvReleaseImage(&grey);
cvDestroyWindow("Original");


return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:65172次
    • 积分:1029
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:58篇
    • 译文:0篇
    • 评论:9条
    文章分类
    最新评论