目录
一、Python方式
1.Open3D
2.直接用python读取并保存成bin格式
3.pypcd
二、C++方式
读取pcd文件(因为我的点云是ZED相机获得的,所以是XYZRGBA格式,雷达的只用points这个属性就可以读取xyz)
import open3d as o3d
import numpy as np
file_path='/media/wangchen/KINGSTON/数据集/pcd to bin/125.pcd'
pcd = o3d.io.read_point_cloud(file_path)#Open3d读取到的点云通常存储到PointCloud类中,这个类中我们常用的属性就是points和colors
points = np.asarray(pcd.points)
colors = np.asarray(pcd.colors) * 255#colors中的RGB数据是归一化的,所以要乘以255
print(points.shape, colors.shape)
print(np.concatenate([points, colors], axis=-1))
处理numpy矩阵
import open3d as o3d
pcd = o3d.geometry.PointCloud()#创建一个PointCloud对象
pcd.points = o3d.utility.Vector3dVector(points_array)#将矩阵变为open3d里面的数据格式
保存pcd点云
o3d.io.write_point_cloud(path, pcd , write_ascii=True)#如果有需要一定要指定保存编码格式
def read_pcd(filepath):
lidar = []
with open(filepath, 'r', encoding='utf-8') as f:
line = f.readline().strip()
while line:
linestr = line.split(" ")
if len(linestr) == 4:
linestr_convert = list(map(float, linestr))
lidar.append(linestr_convert)
line = f.readline().strip()
return np.array(lidar)
lidar=read_pcd('/media/wangchen/KINGSTON/数据集/pcd to bin/125.pcd')
lidar.reshape(-1, 4).astype(np.float32)
lidar.tofile('/media/wangchen/KINGSTON/数据集/pcd to bin/125.bin')
pcd文件说白了也是一个txt,直接用python的open打开按行操作也行。而用python读取成数组之后,我们直接tofile到一个bin文件里就完成可数据格式的转换。
这个包是python里面专门处理pcd点云的,切记他的运行环境是python2,不能是3
如何切换环境请看我的这篇博客
import pypcd
## Get pcd file
pc = pypcd.PointCloud.from_path(pcd_file)
## Get data from pcd (x, y, z, intensity, ring, time)
np_x = (np.array(pc.pc_data['x'], dtype=np.float32)).astype(np.float32)
np_y = (np.array(pc.pc_data['y'], dtype=np.float32)).astype(np.float32)
np_z = (np.array(pc.pc_data['z'], dtype=np.float32)).astype(np.float32)
np_i = (np.array(pc.pc_data['intensity'], dtype=np.float32)).astype(np.float32)/256
# np_r = (np.array(pc.pc_data['ring'], dtype=np.float32)).astype(np.float32)
# np_t = (np.array(pc.pc_data['time'], dtype=np.float32)).astype(np.float32)
## Stack all data
points_32 = np.transpose(np.vstack((np_x, np_y, np_z, np_i)))
## Save bin file
points_32.tofile(bin_file_path)
附上一份转换链接如何将自己采集到的点云数据转换为与Kitti数据集相同的格式?(.pcd文件转换为.bin文件)_Demo_xxx的博客-CSDN博客_matlab pcd转bin
PCL对于点云的重要性就像Opencv对于图像一样。我用的ROS里面自带的PCL
mkdir -p workspace1/src
cd src
catkin_init_workspace
catkin_create_pkg read_pcd pcl_conversions pcl_ros roscpp sensor_msgs
进入到功能包的src文件夹下面新建.cpp文件read_pcd.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void convert_pcd_to_bin(std::string path_input, std::string path_output)
{
std::string datapath_in = path_input;
std::string datapath_out= path_output;
std::vector paths(boost::filesystem::directory_iterator{datapath_in}, boost::filesystem::directory_iterator{});
sort(paths.begin(), paths.end());
auto pcd_iter = paths.begin();
int frames_all = paths.end() - paths.begin();
pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
for (int i = 0; i < frames_all; i++) {
pcd_iter++;
std::string in_file = pcd_iter->string();
if (pcl::io::loadPCDFile(in_file, *cloud) == -1) {
std::string err = "Couldn't read file " + in_file;
PCL_ERROR(err.c_str());
return;// (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from "
<< in_file
<< " with the following fields: "
<< std::endl;
std::string out_file = datapath_out + std::to_string(i) + ".bin";
std::ofstream bin_file(out_file.c_str(), std::ios::out | std::ios::binary);
for (int j = 0; j < cloud->size(); j++) {
bin_file.write((char*)& cloud->at(j).x, sizeof(cloud->at(j).x));
bin_file.write((char*)& cloud->at(j).y, sizeof(cloud->at(j).y));
bin_file.write((char*)& cloud->at(j).z, sizeof(cloud->at(j).z));
//bin_file.write((char*)& cloud->at(j).intensity, sizeof(cloud->at(j).intensity));
}
bin_file.close();
}
}
int main(int argc, char **argv)
{
if (argc <= 1) {
std::cout << "please input datapath dir and output datapath dir" << std::endl;
std::cout << "for example, run ./pcd2bin /home/pcds/ /home/bins/" << std::endl;
return 0;
}
std::string path_input = argv[1];
std::string path_output= argv[2];
std::fstream file_in;
file_in.open(path_input, std::ios::in);
if (!file_in) {
std::cout << "can not find the input path:" << path_input << std::endl;
return 0;
}
std::fstream file_out;
file_out.open(path_output, std::ios::in);
if (!file_out) {
std::cout << "can not find the output path:" << path_output << std::endl;
if (0 == mkdir(path_output.data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
std::cout << "successfully creat output path: " << path_output << std::endl;
} else {
std::cout << "create output path failed!" << std::endl;
return 0;
}
}
convert_pcd_to_bin(path_input, path_output);
return 0;
}
将下面的编译规则写入功能包下面的CMAKE.list文件中
add_executable(read_pcd src/read_pcd.cpp)
target_link_libraries(read_pcd ${catkin_LIBRARIES})
然后编译catkin_make,得到可执行文件,运行可执行文件,同时输入两个参数(pcd路径,bin保存路径)