import pcl import numpy as np from mayavi import mlab cloud = pcl.load('laser.pcd') vox = cloud.make_voxel_grid_filter() vox.set_leaf_size(0.01, 0.01, 0.01) cloud_downsampled = vox.filter() seg = cloud_downsampled.make_segmenter() seg.set_model_type(pcl.SACMODEL_PLANE) seg.set_method_type(pcl.SAC_RANSAC) seg.set_distance_threshold(0.01) indices, model = seg.segment() obstacles_cloud = cloud_downsampled.extract(indices, negative=True) mlab.figure(bgcolor=(0.1, 0.1, 0.1)) mlab.points3d(cloud_downsampled[:, 0], cloud_downsampled[:, 1], cloud_downsampled[:, 2], mode='point', color=(1, 1, 1), scale_factor=0.05) mlab.points3d(obstacles_cloud[:, 0], obstacles_cloud[:, 1], obstacles_cloud[:, 2], mode='point', color=(1, 0, 0), scale_factor=0.05) mlab.show() 发生如下错误:[pcl::VoxelGrid::applyFilter] Leaf size is too small for the input dataset. Integer indices would overflow.Traceback (most recent call last): File "D:\pythonProject3\障碍物识别2.py", line 25, in <module> mlab.points3d(cloud_downsampled[:, 0], cloud_downsampled[:, 1], cloud_downsampled[:, 2], mode='point', color=(1, 1, 1), scale_factor=0.05) File "pcl\pxi/PointCloud_PointXYZ_180.pxi", line 233, in pcl._pcl.PointCloud.__getitem__ TypeError: an integer is required
时间: 2023-06-17 17:06:48 浏览: 191
1 cloud_viewer.rar_PCL1.8_cloud_pcl1.8.1_pointcloud_下载Cloud_View
5星 · 资源好评率100%
这个错误是因为你的点云经过体素滤波后点云数量变少,使用下标索引访问时可能会出现索引越界的问题。可以在代码中加入如下判断来解决这个问题:
```python
if cloud_downsampled.size > 0:
mlab.points3d(cloud_downsampled[:, 0], cloud_downsampled[:, 1], cloud_downsampled[:, 2], mode='point', color=(1, 1, 1), scale_factor=0.05)
if obstacles_cloud.size > 0:
mlab.points3d(obstacles_cloud[:, 0], obstacles_cloud[:, 1], obstacles_cloud[:, 2], mode='point', color=(1, 0, 0), scale_factor=0.05)
```
这段代码会先判断点云是否为空,如果不为空再进行可视化。这样可以避免索引越界的问题。同时也建议在代码中加入一些打印语句来输出点云的数量和其他信息,方便调试。
阅读全文