双目视觉opencv+opengl三维重建
时间: 2023-12-10 11:35:51 浏览: 194
双目视觉是一种通过两个摄像头获取的图像来计算深度信息的技术。OpenCV和OpenGL是两个常用的库,可以用于实现双目视觉三维重建。以下是一个简单的流程:
1.使用OpenCV获取左右两个摄像头的图像,并进行立体匹配,得到视差图。
```python
import cv2
# 读取左右两个摄像头的图像
imgL = cv2.imread('left.png', 0)
imgR = cv2.imread('right.png', 0)
# 创建SGBM对象,进行立体匹配
window_size = 3
left_matcher = cv2.StereoSGBM_create(
minDisparity=0,
numDisparities=160,
blockSize=5,
P1=8 * 3 * window_size ** 2,
P2=32 * 3 * window_size ** 2,
disp12MaxDiff=1,
uniquenessRatio=10,
speckleWindowSize=100,
speckleRange=32,
preFilterCap=63,
mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY
)
right_matcher = cv2.ximgproc.createRightMatcher(left_matcher)
# 计算视差图
displ = left_matcher.compute(imgL, imgR)
dispr = right_matcher.compute(imgR, imgL)
```
2.使用三角剖分算法,根据视差图计算出点云。
```python
import numpy as np
from matplotlib import pyplot as plt
# 根据视差图计算点云
focal_length = 0.8
T = 0.5
Q = np.float32([[1, 0, 0, -imgL.shape[1] / 2],
[0, -1, 0, imgL.shape[0] / 2],
[0, 0, 0, -focal_length],
[0, 0, 1 / T, 0]])
points = cv2.reprojectImageTo3D(displ, Q)
# 去除无效点
mask = displ > displ.min()
out_points = points[mask]
out_colors = imgL[mask]
# 显示点云
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(out_points[:, 0], out_points[:, 1], out_points[:, 2],
c=out_colors / 255.0, marker='.')
ax.view_init(elev=135, azim=90)
plt.show()
```
3.使用OpenGL进行纹理贴图,显示三维重建结果。
```python
import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PIL import Image
# 初始化OpenGL窗口
glfw.init()
glfw.window_hint(glfw.VISIBLE, False)
window = glfw.create_window(640, 480, "OpenGL Window", None, None)
glfw.make_context_current(window)
# 加载纹理
texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imgL.shape[1], imgL.shape[0], 0, GL_RGB, GL_UNSIGNED_BYTE, imgL)
# 显示点云
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, 640 / 480, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(0, 0, 0, 0, 0, -1, 0, 1, 0)
glTranslatef(0, 0, -3)
glRotatef(180, 0, 1, 0)
glEnable(GL_TEXTURE_2D)
glBegin(GL_POINTS)
for i in range(out_points.shape[0]):
glColor3f(out_colors[i, 0] / 255.0, out_colors[i, 1] / 255.0, out_colors[i, 2] / 255.0)
glVertex3f(out_points[i, 0], out_points[i, 1], out_points[i, 2])
glEnd()
glfw.swap_buffers(window)
# 保存结果
data = glReadPixels(0, 0, 640, 480, GL_RGB, GL_UNSIGNED_BYTE)
image = Image.frombytes("RGB", (640, 480), data)
image.save("result.png")
```
阅读全文