You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Open3D 0.18.0、numpy 2.0.2与pyrealsense2兼容问题引发段错误

问题分析与解决方案

1. 代码中的未定义变量错误

你的代码片段存在明显的变量未定义问题:

# Attempt to create point cloud
depth_o3d = o3d.geometry.Image(depth_image)  # depth_image从未定义

此前已经通过depth_o3d = o3d.geometry.Image(np.asarray(depth_frame.get_data()))创建了正确的Open3D深度图像,这行多余的赋值会调用未定义的depth_image,直接触发内存访问异常,导致段错误。

修复:删除这行错误的赋值语句,直接使用之前创建的depth_o3d。

2. NumPy 2.0与Open3D 0.18.0的兼容性问题

Open3D 0.18.0发布于2023年3月,而NumPy 2.0是2024年推出的版本,两者底层API存在兼容性差异(如数组内存布局、类型转换逻辑变化),这会导致Open3D处理NumPy数组时出现内存访问错误。

修复:降级NumPy到Open3D官方兼容的1.x版本:

pip install numpy==1.26.4

3. 深度帧数据的内存生命周期问题

pyrealsense2的depth_frame.get_data()返回的是原始内存缓冲区的视图,若直接转换为NumPy数组并创建Open3D Image,后续操作中原缓冲区可能被释放,引发野指针访问。

修复:显式复制数据,确保内存独立:

# 原代码
depth_o3d = o3d.geometry.Image(np.asarray(depth_frame.get_data()))
# 修改为
depth_np = np.asarray(depth_frame.get_data()).copy()
depth_o3d = o3d.geometry.Image(depth_np)

4. Open3D CUDA版本的潜在问题

你的depth_o3d类型为open3d.cuda.pybind.geometry.Image,说明使用了CUDA版本的Open3D。若CUDA环境与Open3D版本不匹配,可能导致GPU内存访问错误。

排查:切换到CPU版本测试:

pip uninstall open3d -y
pip install open3d-cpu==0.18.0

完整修复后的代码

import open3d as o3d
import numpy as np
import pyrealsense2 as rs

# Configure RealSense pipeline
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
pipeline.start(config)

# Wait for a frame
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()

# Convert depth frame to Open3D image(显式复制数据)
depth_np = np.asarray(depth_frame.get_data()).copy()
depth_o3d = o3d.geometry.Image(depth_np)

# Intrinsic parameters
depth_intrin = depth_frame.profile.as_video_stream_profile().intrinsics
width, height = depth_intrin.width, depth_intrin.height
intrinsic = o3d.camera.PinholeCameraIntrinsic(width, height, 
                                              depth_intrin.fx, depth_intrin.fy, 
                                              depth_intrin.ppx, depth_intrin.ppy)

# Create point cloud
pcd = o3d.geometry.PointCloud.create_from_depth_image(depth_o3d, intrinsic)

voxel_size = 0.05
voxel_grid = o3d.geometry.VoxelGrid.create_from_point_cloud(pcd, voxel_size=voxel_size)

o3d.visualization.draw_geometries([voxel_grid])

pipeline.stop()

内容的提问来源于stack exchange,提问作者Ariel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 22:18:11