OpenGL OpenCV根据视差图重建三维信息
代码如下:
// 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根据视差图重建三维信息的更多相关文章
- opencv估计两图的三维坐标变换矩阵
cv::estimateAffine3D(MatFrom, MatTo, Transfrom, inlier); Transform得到的是重MatFrom到MatTo的变换矩阵.inlier给一个空 ...
- python+openCV实现双目视差图及测距
通过matlab标定得到相机参数放到stereoconfig.py import numpy as np import cv2 #双目相机参数 class stereoCameral(object): ...
- android ndk调用OpenGL 实现纹理贴图Texture
android ndk调用OpenGL 实现纹理贴图Texture 时间 2014-06-25 05:24:39 CSDN博客 原文 http://blog.csdn.net/chrisfxs/a ...
- OpenCV使用标定图
本文由 @lonelyrains 出品,转载请注明出处. 文章链接: http://blog.csdn.net/lonelyrains/article/details/46915705 上一步生成标 ...
- OpenGL 获取当前屏幕坐标对应的三维坐标
转自原文 OpenGL 获取当前屏幕坐标对应的三维坐标,使用很简单glu库中的一个函数 #include <GL/glut.h> #include <stdlib.h> #in ...
- 开源免费跨平台opengl opencv webgl gtk blender, opengl贴图程序
三维图形的这是opengl的强项,大型3D游戏都会把它作为首选.图像处理,是opencv的锁定的目标,大多都是C的api,也有少部分是C++的,工业图像表现,图像识别,都会考虑opencv的.webg ...
- opengl学习笔记(四):openCV读入图片,openGL实现纹理贴图
在opengl中实现三维物体的纹理贴图的第一步就是要读入图片,然后指定该图片为纹理图片. 首先利用opencv的cvLoadImage函数把图像读入到内存中 img = cvLoadImage(); ...
- OpenGL+OpenCV实现立方体贴图
我屮艸芔茻,转眼就7月份了. 今天试了一下立方体贴图,比较简单,大概说下和平面贴图的区别. 1. 平面贴图需要的是纹理坐标vec2:立方体贴图需要的是一个方向向量vec3,长度没有关系,重要的是方向, ...
- OpenGL——OpenCV与SOIL读取图片进行纹理贴图
使用OpenCV读取图片代码如下 img = imread(m_fileName); if (img.empty()) { fprintf(stderr, "Can not load ima ...
随机推荐
- 快速索引 (对View的自定义)
快速索引 (对View的自定义) 快速索引应用场景: 微信好友列表, 联系人通讯录, 应用管理, 文件管理等. 快速索引7步曲: *1. A-Z索引的绘制. * 2. 处理Touch事件. * 3. ...
- 如何在苹果手机上安装自制的AD证书
写这篇博文的契机是有人已经实现了CRM在用自制证书部署IFD后,在手机安装上自制证书后即可登录官方移动端APP,因为之前很多人都尝试过只要是自制证书部署的IFD就无法使用官网手机APP,而本人实验下来 ...
- 带你深入理解STL之List容器
上一篇博客中介绍的vector和数组类似,它拥有一段连续的内存空间,并且起始地址不变,很好的支持了随机存取,但由于是连续空间,所以在中间进行插入.删除等操作时都造成了内存块的拷贝和移动,另外在内存空间 ...
- UE4联机多人游戏基本设置
UE4自带网络联机功能,但是似乎只有蓝图接口,而真正写功能的时候不能用C++,让人感觉相当诡异 还是作一个简单记录 1.建一个第三人称模板,为什么会用他呢,因为它自带模板的很多组件,直接支持联机功能, ...
- LM算法与非线性最小二乘问题
摘录的一篇有关求解非线性最小二乘问题的算法--LM算法的文章,当中也加入了一些我个人在求解高精度最小二乘问题时候的一些感触: LM算法,全称为Levenberg-Marquard算法,它可用于解决非线 ...
- 【安卓网络请求开源框架Volley源码解析系列】定制自己的Request请求及Volley框架源码剖析
通过前面的学习我们已经掌握了Volley的基本用法,没看过的建议大家先去阅读我的博文[安卓网络请求开源框架Volley源码解析系列]初识Volley及其基本用法.如StringRequest用来请求一 ...
- 深入剖析Tomcat会话机制
1缓存机制 Tomcat默认将Session保存到内存中.但同时,Tomcat也提供了PersistentManager配合不同的Store实现的方式,使Session可以被保存到不同地方(Datab ...
- iOS中 自定义系统相机 作者:韩俊强
需要框架: #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h> 布局如下 ...
- STL:set/multiset用法详解
集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...
- Linux IPC实践(9) --System V共享内存
共享内存API #include <sys/ipc.h> #include <sys/shm.h> int shmget(key_t key, size_t size, int ...