opencv与opengl混用实现三维点云图像
生活随笔
收集整理的这篇文章主要介绍了
opencv与opengl混用实现三维点云图像
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
/*
灰度图转换为高度图,为双目视觉三维重建做准备。
*/
#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;
}
总结
以上是生活随笔为你收集整理的opencv与opengl混用实现三维点云图像的全部内容,希望文章能够帮你解决所遇到的问题。