代码如下:

// 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. 源码篇——Handler消息机制

    Handler消息机制 Message 消息 Message.obtain() Message msg = new Message() Handler new Handler(){ handlerMe ...

  2. [openresty]安装nginx_lua

    这种方式是直接安装openresty ,不是通过重新编译nginx Ubuntu 安装 安装依赖包 $ sudo apt-get install libreadline-dev libncurses5 ...

  3. Markdown-----Markdown使用文档

    最近才接触Markdown,为了快速记忆,整理了这个文档,欢迎补充. Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接和图片上传 LaTex数学公式 UML序列图和流程图 离线写 ...

  4. 安卓TextView完美展示html格式代码

    对于TextView展示html格式代码,最简单的办法就是使用textview.setText(Html.fromHtml(html));,即便其中有img标签,我们依然可以使用ImageGetter ...

  5. H5、React Native、Native应用对比分析

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博!iOS开发者交流QQ群: 446310206 "存在即合理".凡是存在的,都是合乎规律的.任何新 ...

  6. Android初级教程理论知识(第八章网络编程二)

    HttpClient 发送get请求 创建一个客户端对象 HttpClient client = new DefaultHttpClient(); 创建一个get请求对象 HttpGet hg = n ...

  7. 【嵌入式开发】 ARM 汇编 (指令分类 | 伪指令 | 协处理器访问指令)

    作者 : 韩曙亮 博客地址 : http://blog.csdn.net/shulianghan/article/details/42408137 转载请著名出处 本博客相关文档下载 :  -- AR ...

  8. UNIX环境高级编程——守护进程

    一.守护进程简介 守护进程,也就是通常说的Daemon进程,是Linux中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程常常在系 ...

  9. Cocos2D:塔防游戏制作之旅(十五)

    Yes,貌似添加了好多的代码啊 ;] ,在你添加更多代码时,你可能注意到一些Xcode中的一些警告.首先你先忽略这些警告,我们先添加少量最终缺失的部分,然后再来解释上面代码做了什么! 在Enemy.m ...

  10. Cocos2D:塔防游戏制作之旅(六)

    现在,创建一个新的类用来表示炮塔.添加新的类文件,名称为Tower,继承于CCNode. 替换Tower.h文件为如下内容: #import "cocos2d.h" #import ...