openGL+open3d+python 鼠标拖动画矩形框选出点云数据的感兴趣区域代码
时间: 2023-06-25 07:07:19 浏览: 137
以下是使用OpenCV和Open3D实现鼠标拖动画矩形框选出点云数据感兴趣区域的Python代码:
```python
import cv2
import open3d as o3d
import numpy as np
# 创建空点云对象
pcd = o3d.geometry.PointCloud()
# 读取点云文件
pcd = o3d.io.read_point_cloud("point_cloud.ply")
# 投影点云到图像平面
intrinsics = o3d.camera.PinholeCameraIntrinsic()
intrinsics.set_intrinsics(width=640, height=480, fx=525.0, fy=525.0, cx=319.5, cy=239.5)
depth_image = np.asarray(pcd.project_to_image(intrinsics))
depth_image = cv2.normalize(depth_image, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
# 创建窗口并显示图像
cv2.namedWindow("Point Cloud", cv2.WINDOW_NORMAL)
cv2.imshow("Point Cloud", depth_image)
# 定义鼠标事件函数
def mouse_event(event, x, y, flags, param):
global mouseX, mouseY, drawing, mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
mouseX,mouseY = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
cv2.rectangle(depth_image, (mouseX, mouseY), (x, y), (0, 255, 0), 2)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
cv2.rectangle(depth_image, (mouseX, mouseY), (x, y), (0, 255, 0), 2)
# 根据鼠标输入的矩形框选出感兴趣区域
roi = depth_image[mouseY:y, mouseX:x]
roi_points = []
for i in range(mouseY, y):
for j in range(mouseX, x):
if roi[i - mouseY, j - mouseX] > 0:
depth = pcd.get_max_bound()[2] - pcd.get_min_bound()[2]
point = pcd.get_point_cloud_bounds()[0] + np.array([j * depth / 640, i * depth / 480, depth - roi[i - mouseY, j - mouseX] * depth / 255])
roi_points.append(point)
roi_pcd = o3d.geometry.PointCloud()
roi_pcd.points = o3d.utility.Vector3dVector(roi_points)
o3d.visualization.draw_geometries([roi_pcd])
# 设置鼠标事件回调函数
cv2.setMouseCallback("Point Cloud", mouse_event)
# 等待按键按下
while cv2.waitKey(1) != ord('q'):
cv2.imshow("Point Cloud", depth_image)
# 关闭窗口
cv2.destroyAllWindows()
```
该代码使用OpenCV将点云投影到图像平面,并在窗口中显示图像。通过鼠标事件函数,可以在图像上画出矩形框并提取感兴趣区域中的点云数据。最后使用Open3D绘制感兴趣区域的点云。
阅读全文