OpenGL OpenCV根据视差图重建三维信息

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:     代码如下:   // disparity_to_3d_reconstruction.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" //Huang,Haiqiao coded on Dec.

 

 

代码如下:

 

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



 

 

效果如下:

 


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
3月前
|
监控 API 计算机视觉
OpenCV这么简单为啥不学——1.8、threshold阈值0-4效果对照图
OpenCV这么简单为啥不学——1.8、threshold阈值0-4效果对照图
36 0
|
2月前
|
机器学习/深度学习 API vr&ar
Qt, OpenCV与OpenGL协同作战:图像处理与三维图形界面的完美结合
Qt, OpenCV与OpenGL协同作战:图像处理与三维图形界面的完美结合
144 4
|
5月前
|
XML 小程序 Java
【Android App】三维投影OpenGL ES的讲解及着色器实现(附源码和演示 超详细)
【Android App】三维投影OpenGL ES的讲解及着色器实现(附源码和演示 超详细)
55 0
|
5月前
|
XML 前端开发 Java
【Android App】三维处理中三维投影OpenGL功能的讲解及实战(附源码和演示 超详细必看)
【Android App】三维处理中三维投影OpenGL功能的讲解及实战(附源码和演示 超详细必看)
35 1
|
5月前
|
XML Java Android开发
Android App开发中OpenGL三维投影的讲解及实现(附源码和演示 简单易懂)
Android App开发中OpenGL三维投影的讲解及实现(附源码和演示 简单易懂)
38 1
|
7月前
|
计算机视觉 C++
OpenCV-用图像处理作出素描图(给你的另一半试试吧)
OpenCV-用图像处理作出素描图(给你的另一半试试吧)
|
8月前
|
C++ 计算机视觉 Python
C++ VS OpenGL绘制教室三维立体旋转图像
C++ VS OpenGL绘制教室三维立体旋转图像
78 0
C++ VS OpenGL绘制教室三维立体旋转图像
|
Linux
LINUX查看OpenGL信息
LINUX查看OpenGL信息
349 0
|
算法 Ubuntu Linux
红胖子网络科技博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中...
红胖子网络科技博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中...
【解决方案】成功解决ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects报错信息
成功解决ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects报错信息
【解决方案】成功解决ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects报错信息