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混用实现三维点云图像的全部內容,希望文章能夠幫你解決所遇到的問題。