一、利用python保留自己想要的点云
1.python读取pcd点云文件,需要使用python的库open3d读取
在pycharm中打开终端,输入pip install open3d
(用外网,超快)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201118205954683.png#pic_center)
2.读取pcd文件后,遍历循环每个点,将满足自己要求的写入一个txt形式的文件里。(这里试过放进pcd文件里,但是好像不支持新建一个空白的pcd)将txt作为中间转换媒介。
python完整代码如下:
import open3d as o3d
import numpy as np
def read_pcd(file_path):
pcd = o3d.io.read_point_cloud(file_path)
#print(np.asarray(pcd.points))
#colors = np.asarray(pcd.colors) * 255
points = np.asarray(pcd.points)
#print(points.shape, colors.shape)
#return np.concatenate([points, colors], axis=-1)
return points
point=read_pcd('1.pcd') #原始点云的pcd文件
with open('2.txt','w',encoding='utf-8') as f:#新建一个txt,并读取
for i in range(len(point)): #遍历循环点云,获取坐标
x = point[i][0]
y = point[i][1]
z = point[i][2]
if x**2 + y**2 + z**2 <= 2: #需要满足的条件
points = str(x)+' '+str(y)+' '+str(z) + '\n'
f.write(points) #保留满足条件的点云,并写入txt文件里
f.close()
二、将txt转换为pcd
此时是在linux系统,用到了点云库
txt2pcd代码如下:
#include<iostream>
#include<fstream>
#include <string>