代码如下:

// 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. XMPP系列(七)---获取群组列表

    上一篇介绍了如何创建群组,这一篇就介绍一下,如何获取自己的群组列表. 在上一篇有提到,如果我们创建的群组是公共的群组,那么获取自己的群组列表时,会获取到自己的群组列表和那些公共的群组.而实际做社交的应 ...

  2. Linux命令—压缩及其他

     (1)为了更好的传送和保存文件,需要对某些文件和目录进行压缩和解压缩操作,Linux 提供了强大的压缩.解压缩命令,常用的tar命令. (2)在Linux中,如果要使用储存设备(硬盘.光驱.移动 ...

  3. SQL基本函数

    字符型函数 函数名称 描述 LOWER 将特定的字符串转化为小写,只影响字母字符串. UPPER 将整个字符串转换成大写,只影响字母字符串. INITCAP 将字符串中每一个单词的第一个字母转换为大写 ...

  4. 【NPR】漫谈轮廓线的渲染

    写在前面 好久没写文章.最近在看<Real Time Rendering, third edition>这本书,看到了NPR这一章就想顺便记录下一些常见的轮廓线渲染的方法. 在非真实感渲染 ...

  5. 1086. Tree Traversals Again (25)

    题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...

  6. Android进阶(二十七)Android原生扰人烦的布局

    Android原生扰人烦的布局 在开发Android应用时,UI布局是一件令人烦恼的事情.下面主要讲解一下Android中的界面布局. 一.线性布局(LinearLayout) 线性布局分为: (1) ...

  7. C++ Primer 有感(标准库set类型)

    set容器只是单纯的键的集合,键必须为一.set容器不支持下标操作,而且没有定义maped_type类型.在set容器中,value_type不是pair类型,而是与key_type类型相同的类型. ...

  8. byte和长度为8的boolean数组互相转换

    由于byte是一个8位字节 所以可以用它来存放数组为8的boolean数组,这些在通信协议会经常用到.这里给出一个java代码对其互相转换的. package com.udpdemo.test2; i ...

  9. C++ Primer 有感(标准库类型)

    1.当进行string对象和字符串字面值混合连接操作时,+操作符的左右操作数必须至少有一个是string类型的: string s1= "hello"; sring s2=&quo ...

  10. python面向对象小练习

    就是几个动物,自动排列生成什么的 class Animal(object): def __init__(self,name,weight): self.name = name self.weight ...