代码如下:

// disparity_to_3d_reconstruction.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" //Huang,Haiqiao coded on Dec.2009代码出处:
//http://www.opencv.org.cn/forum.php?mod=viewthread&tid=8722&extra=&page=1
#include "stdafx.h"
#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" #pragma comment(lib,"opencv_highgui2410d.lib")
#pragma comment(lib,"opencv_core2410d.lib")
#pragma comment(lib,"opencv_imgproc2410d.lib") #include <math.h>
#include <GL/glut.h>
#include <iostream>
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;//scalar of converting pixel color to float coordinates void renderScene(void)
{ glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity(); // Reset the coordinate system before modifying
gluLookAt (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(-30, 0.0, 1.0, 0.0); //rotate about the x axis
glRotatef(-180, 0.0, 0.0, 1.0); //rotate about the z axis
glRotatef(-180, 0.0, 1.0, 0.0); //rotate about the y axis float imageCenterX = w*.5;
float imageCenterY = h*.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)
{
cout << "OpenCV and OpenGL working together!"<<endl;
//char* filename = "tsuDisparity.bmp;"; string image_name;
cout<<"input image name:"<<endl;
cin>>image_name;
IplImage* imgGrey = cvLoadImage(image_name.c_str(),0); //read image as a grey one
if (imgGrey==NULL)
{
cout << "No valid image input."<<endl;
char c=getchar();
return 1;
}
w = imgGrey->width;
h = imgGrey->height; displayDisparity(imgGrey);
cvNamedWindow("original", CV_WINDOW_AUTOSIZE );
cvShowImage("original", imgGrey ); //------------------OpenGL-------------------------
glutInit(&argc,(char**)argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("3D disparity image");
glutDisplayFunc(renderScene);
glutReshapeFunc (reshape);
glutMainLoop();
cvWaitKey(0);
//release opencv stuff.
cvReleaseImage(&imgGrey);
cvDestroyWindow("Original"); return 0;
}

效果:

添加鼠标移动事件,代码如下:

// disparity_to_3d_reconstruction.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" //Huang,Haiqiao coded on Dec.2009代码出处:
//http://www.opencv.org.cn/forum.php?mod=viewthread&tid=8722&extra=&page=1
#include "stdafx.h"
#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" #pragma comment(lib,"opencv_highgui2410d.lib")
#pragma comment(lib,"opencv_core2410d.lib")
#pragma comment(lib,"opencv_imgproc2410d.lib") #include <math.h>
#include <GL/glut.h>
#include <iostream>
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;//scalar of converting pixel color to float coordinates #define pi 3.1415926
bool mouseisdown=false;
bool loopr=false;
int mx,my;
int ry=10;
int rx=10; void timer(int p)
{
ry-=5;
//marks the current window as needing to be redisplayed.
glutPostRedisplay();
if (loopr)
glutTimerFunc(200,timer,0);
} void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON)
{
if(state == GLUT_DOWN)
{
mouseisdown=true;
loopr=false;
}
else
{
mouseisdown=false;
}
} if (button== GLUT_RIGHT_BUTTON)
if(state == GLUT_DOWN)
{
loopr=true;
glutTimerFunc(200,timer,0);
}
} void motion(int x, int y)
{
if(mouseisdown==true)
{
ry+=x-mx;
rx+=y-my;
mx=x;
my=y;
glutPostRedisplay();
}
} void special(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_LEFT:
ry-=5;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT:
ry+=5;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
rx+=5;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
rx-=5;
glutPostRedisplay();
break;
}
} void renderScene(void)
{ glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity(); // Reset the coordinate system before modifying
gluLookAt (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//gluLookAt (0.0, 0.0, 7.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0);
//glRotatef(-30, 0.0, 1.0, 0.0); //rotate about the x axis
//glRotatef(-180, 0.0, 0.0, 1.0); //rotate about the z axis
//glRotatef(-180, 0.0, 1.0, 0.0); //rotate about the y axis glRotatef(ry,0,1,0);
glRotatef(rx-180,1,0,0); float imageCenterX = w*.5;
float imageCenterY = h*.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)
{
cout << "OpenCV and OpenGL working together!"<<endl;
//char* filename = "tsuDisparity.bmp;"; string image_name;
cout<<"input image name:"<<endl;
cin>>image_name;
IplImage* imgGrey = cvLoadImage(image_name.c_str(),0); //read image as a grey one
if (imgGrey==NULL)
{
cout << "No valid image input."<<endl;
char c=getchar();
return 1;
}
w = imgGrey->width;
h = imgGrey->height; displayDisparity(imgGrey);
cvNamedWindow("original", CV_WINDOW_AUTOSIZE );
cvShowImage("original", imgGrey ); //------------------OpenGL-------------------------
glutInit(&argc,(char**)argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("3D disparity image");
glutDisplayFunc(renderScene);
glutReshapeFunc (reshape); glutMouseFunc(mouse);
glutMotionFunc(motion);
glutSpecialFunc(special); glutMainLoop(); cvWaitKey(0);
//release opencv stuff.
cvReleaseImage(&imgGrey);
cvDestroyWindow("Original"); return 0;
}

效果如下:

OpenGL OpenCV根据视差图重建三维信息的更多相关文章

  1. opencv估计两图的三维坐标变换矩阵

    cv::estimateAffine3D(MatFrom, MatTo, Transfrom, inlier); Transform得到的是重MatFrom到MatTo的变换矩阵.inlier给一个空 ...

  2. python+openCV实现双目视差图及测距

    通过matlab标定得到相机参数放到stereoconfig.py import numpy as np import cv2 #双目相机参数 class stereoCameral(object): ...

  3. android ndk调用OpenGL 实现纹理贴图Texture

    android ndk调用OpenGL 实现纹理贴图Texture 时间 2014-06-25 05:24:39  CSDN博客 原文  http://blog.csdn.net/chrisfxs/a ...

  4. OpenCV使用标定图

    本文由 @lonelyrains 出品,转载请注明出处.  文章链接: http://blog.csdn.net/lonelyrains/article/details/46915705 上一步生成标 ...

  5. OpenGL 获取当前屏幕坐标对应的三维坐标

    转自原文 OpenGL 获取当前屏幕坐标对应的三维坐标,使用很简单glu库中的一个函数 #include <GL/glut.h> #include <stdlib.h> #in ...

  6. 开源免费跨平台opengl opencv webgl gtk blender, opengl贴图程序

    三维图形的这是opengl的强项,大型3D游戏都会把它作为首选.图像处理,是opencv的锁定的目标,大多都是C的api,也有少部分是C++的,工业图像表现,图像识别,都会考虑opencv的.webg ...

  7. opengl学习笔记(四):openCV读入图片,openGL实现纹理贴图

    在opengl中实现三维物体的纹理贴图的第一步就是要读入图片,然后指定该图片为纹理图片. 首先利用opencv的cvLoadImage函数把图像读入到内存中 img = cvLoadImage(); ...

  8. OpenGL+OpenCV实现立方体贴图

    我屮艸芔茻,转眼就7月份了. 今天试了一下立方体贴图,比较简单,大概说下和平面贴图的区别. 1. 平面贴图需要的是纹理坐标vec2:立方体贴图需要的是一个方向向量vec3,长度没有关系,重要的是方向, ...

  9. OpenGL——OpenCV与SOIL读取图片进行纹理贴图

    使用OpenCV读取图片代码如下 img = imread(m_fileName); if (img.empty()) { fprintf(stderr, "Can not load ima ...

随机推荐

  1. [Python监控]psutil模块简单使用

    安装很简单 pip install psutil 官网地址为 https://pythonhosted.org/psutil/ (文档上有详细的api) github地址为 https://githu ...

  2. SQLite 创建表(http://www.w3cschool.cc/sqlite/sqlite-create-table.html)

    SQLite 创建表 SQLite 的 CREATE TABLE 语句用于在任何给定的数据库创建一个新表.创建基本表,涉及到命名表.定义列及每一列的数据类型. 语法 CREATE TABLE 语句的基 ...

  3. springmvc注解形式的开发参数接收

    springmvc基于注解的开发 注解第一个例子 1. 创建web项目 springmvc-2 2. 在springmvc的配置文件中指定注解驱动,配置扫描器 <!-- sprimgmvc 注解 ...

  4. Android动态换肤(二、apk免安装插件方式)

    在上一篇文章Android动态换肤(一.应用内置多套皮肤)中,我们了解到,动态换肤无非就是调用view的setBackgroundResource(R.drawable.id)等方法设置控件的背景或者 ...

  5. jvm java虚拟机 新生代的配置

    1.1.1.1. -Xmn参数 参数-Xmn1m可以用于设置新生代的大小.设置一个较大的新生代会影响老生代的大小,因为这两者的总和是一定的,这个系统参数对于系统性能以及GC行为有很大的影响,新生代一般 ...

  6. 浅谈Android布局

    在前面的博客中,小编介绍了Android的极光推送以及如何实现登录的一个小demo,对于xml布局页面,摆控件这块的内容,小编还不是很熟练,今天小编主要简单总结一下在Android中的布局,学习过An ...

  7. Android View框架总结(一)

    View和Activity的区别 View有哪些? ViewGroup是什么? 为什么Google产生ViewGroup? View的层级结构是什么? View的onMeasure()/onLayou ...

  8. java的四种引用类型

    java的引用分为四个等级:4种级别由高到低依次为:强引用.软引用.弱引用和虚引用. ⑴强引用(StrongReference) 强引用是使用最普遍的引用.如果一个对象具有强引用,那垃圾回收器绝不会回 ...

  9. 开源项目——小Q聊天机器人V1.4

    小Q聊天机器人V1.0 http://blog.csdn.net/baiyuliang2013/article/details/51386281 小Q聊天机器人V1.1 http://blog.csd ...

  10. gradle2.0笔记——让项目升级到gradle2.0

    昨晚看到QQ群消息说gradle2.0发布了,今天去看了一下,确实是昨天发布的,为rc版本:Gradle 2.0-rc-2.于是决定试一下. gradle可以在官网上下载,地址如下:http://ww ...