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;
}

 

 

效果如下:

 

时间: 2024-09-14 06:13:49

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

视觉设计:从裸眼3D到三维信息架构

文章描述:从裸眼3D到三维信息架构.   关于  Nintendo 3DS 今年 2月 25日Nintendo 3DS 正式发卖了!这对所有的小N 党来说都是一个振奋人心的消息~相信速度快的同学已经入手新主机了吧 @@ ! 3DS 的正式公开,伴随的是肉眼可直接观看3D 效果技术的公开,让我们可以抛弃3D 眼镜.而且,任天堂也创造了一个名叫做"3D 音量(3D Volume )"新控件.以下是Nintendo 官网对裸眼功能的一些解释: "Nintendo3DS 影像是用左右

从裸眼3D到三维信息架构

关于&http://www.aliyun.com/zixun/aggregation/37954.html">nbsp; Nintendo 3DS 今年 2月 25日Nintendo 3DS 正式发卖了!这对所有的小N 党来说都是一个振奋人心的消息~相信速度快的同学已经入手新主机了吧 @@ ! 3DS 的正式公开,伴随的是肉眼可直接观看3D 效果技术的公开,让我们可以抛弃3D 眼镜.而且,任天堂也创造了一个名叫做"3D 音量(3D Volume )"新控件.以下

OpenGL ES 纹理贴图的重复与嵌位概念理解

OpenGL ES 纹理贴图的重复与嵌位概念理解 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 以下有一篇转载的Android中关于纹理贴图规则的文章,很不多. 不过我这里是想要深入研究如果让

指针-opencv学习灰度图锐化的两个函数的差别不理解

问题描述 opencv学习灰度图锐化的两个函数的差别不理解 下面是一个灰度图锐化的函数,我有两种方式实现,方式1,和方式2,居然得到的结果不一样,图片数据也不一样,请高手看一下,可能是C语言的知识掌握的不好. void my_sharpen(const cv::Mat &image, cv::Mat &result) { result.create (image.size(), image.type ()); for(int j=1; j<image.rows-1; j++) { u

在C#中使用ObjectDBX技术从未打开图形中获得图块的信息

object|图形 从未打开图形中能获得图块的信息吗?回答是肯定的.下面就来说明具体的实现方法. 要求: n 会用C#编程 n 读过我写的"利用C#进行AutoCAD的二次开发"(在csdn中有) 开始: n 在visual studio.net中新建一C#控制台程序 n 在引用选项卡中添加下列类库: l interop.AutoCAD.dll l AcadExample.dll l ObjectDBX16(在"解决方案资源管理器"中右击"引用"

opencv 二值图 坐标 阈值

问题描述 opencv 二值图 坐标 阈值 opencv为什么设置二值图阈值,保存下来的二值图阈值都?提二值图坐标如何将坐标以线条为单位保存连续坐标?求大神教我,最好有源码,拜托了,新手 解决方案 什么叫以线条为单位保存连续坐标?设置二值图是为了降噪,出去噪音,增加图像的识别率 解决方案二: 什么叫以线条为单位保存连续坐标?设置二值图是为了降噪,出去噪音,增加图像的识别率 解决方案三: 你的问题都没说清楚哦

OpenCV+OpenGL 双目立体视觉三维重建

0.绪论 这篇文章主要为了研究双目立体视觉的最终目标--三维重建,系统的介绍了三维重建的整体步骤.双目立体视觉的整体流程包括:图像获取,摄像机标定,特征提取(稠密匹配中这一步可以省略),立体匹配,三维重建.我在做双目立体视觉问题时,主要关注的点是立体匹配,本文主要关注最后一个步骤三维重建中的:三角剖分和纹理贴图以及对应的OpenCV+OpenGL代码实现. 1.视差计算 1.1基于视差信息的三维重建 特征提取 由双目立体视觉进行三位重建的第一步是立体匹配,通过寻找两幅图像中的对应点获取视差.Op

OpenCV stereo matching 代码 matlab实现视差显示

转载请注明出处:http://blog.csdn.net/wangyaninglm/article/details/44151213, 来自:shiter编写程序的艺术 基础知识 计算机视觉是一门研究使用计算机来模拟人的视觉系统的学科."一图胜千言",人类对于图像中的信息感知效率远超文字等其他媒介,人类获取的信息总量中更是有高达80%依靠视觉系统[1].相对于人类高效的图像信息提取能力,计算机在图像信息的理解上仍然效率低下. 计算机视觉作为一门交叉学科,综合了生物学,心理学,数学,计算

7个步骤教你制作出让人疯传的信息图

  随着社交媒体的兴起,全民进入读图时代.有三种图,在社交媒体中最受欢迎:1.搞笑内涵Gif图,不用太多文字,一句"你懂的"就有四两拨千斤的效果.2."亮点总在最后"长图,巧妙的伏笔和意外结局让看过的人无法抗拒.纷纷疯狂转发.3.有营养的信息图,简单图片和数字图表组合传达丰富信息,大脑最享受的轻量化阅读体验.想要做出好的Gif图和长图,要靠机遇和天分,一鸣惊人的创意往往可遇不可求,但是要做出一张被广泛分享传播.吸引公众眼球的信息图,却有迹可循. 下面这篇文章教你如何