1、从文件中读取点云
接口1
bool open3d::io::ReadPointCloud ( const std::string & filename,
geometry::PointCloud & pointcloud,
const ReadPointCloudOption & params = {}
)
接口2
bool open3d::io::ReadPointCloud ( const std::string & filename,
geometry::PointCloud & pointcloud,
const std::string & file_format,
bool remove_nan_points,
bool remove_infinite_points,
bool print_progress
)
接口3
从扩展名中读取点云
bool open3d::io::ReadPointCloudFromPCD ( const std::string & filename,
geometry::PointCloud & pointcloud,
const ReadPointCloudOption & params
)
bool open3d::io::ReadPointCloudFromPLY ( const std::string & filename,
geometry::PointCloud & pointcloud,
const ReadPointCloudOption & params
2、保存点云
WritePointCloud()
bool open3d::io::WritePointCloud ( const std::string & filename,
const geometry::PointCloud & pointcloud,
const WritePointCloudOption & params = {}
)
bool open3d::io::WritePointCloud ( const std::string & filename,
const geometry::PointCloud & pointcloud,
bool write_ascii,
bool compressed,
bool print_progress
)
// 扩展名保存
bool open3d::io::WritePointCloudToPCD ( const std::string & filename,
const geometry::PointCloud & pointcloud,
const WritePointCloudOption & params
)
bool open3d::io::WritePointCloudToPLY ( const std::string & filename,
const geometry::PointCloud & pointcloud,
const WritePointCloudOption & params
)
3、读取点云
#include <iostream>
#include <Open3D/Open3D.h>
int main(int argc, char* argv[])
{
std::string fileName("bunny.pcd");
auto cloud_ptr = std::make_shared<open3d::geometry::PointCloud>();
if (open3d::io::ReadPointCloud(fileName, *cloud_ptr) == 0)
{
open3d::utility::LogWarning("Failed to read {}\n\n", fileName);
return -1;
}
cloud_ptr->NormalizeNormals(); // 将法向量归一化到1
open3d::visualization::DrawGeometries({ cloud_ptr }, "PointCloud", 1600, 900);
open3d::io::WritePointCloudToPCD("bunny1.pcd", *cloud_ptr, false);
return 0;
}
读取txt格式点云
#include <iostream>
#include <Open3D/Open3D.h>
int main(int argc, char* argv[])
{
std::string fileName("bunny.txt");
auto cloud_ptr = std::make_shared<open3d::geometry::PointCloud>();
if (open3d::io::ReadPointCloudFromXYZ(fileName, *cloud_ptr, {"xyz"}) == 0)
{// 或者用open3d::io::ReadPointCloud(fileName, *cloud_ptr, { "xyz" });
open3d::utility::LogWarning("Failed to read {}\n\n", fileName);
return -1;
}
cloud_ptr->NormalizeNormals(); // 将法向量归一化到1
open3d::visualization::DrawGeometries({ cloud_ptr }, "PointCloud", 1600, 900);
open3d::io::WritePointCloudToPCD("bunny1.pcd", *cloud_ptr, false);
return 0;
}
4、显示点云
#include <iostream>
#include <Open3D/Open3D.h>
int main(int argc, char* argv[])
{
auto source = std::make_shared<open3d::geometry::PointCloud>();
if (open3d::io::ReadPointCloud("1.pcd", *source) == 0)
{
open3d::utility::LogWarning("点云读取失败!!!\n");
return -1;
}
if(source->HasNormals())
source->NormalizeNormals(); // 将法向量归一化到1
auto target = std::make_shared<open3d::geometry::PointCloud>();
if (open3d::io::ReadPointCloud("2.pcd", *target) == 0)
{
open3d::utility::LogWarning("点云读取失败!!!\n\n");
return -1;
}
if (target->HasNormals())
target->NormalizeNormals(); // 将法向量归一化到1
Eigen::Vector3d color{ 0.0,0.0,1.0 };
target->PaintUniformColor(color);
open3d::visualization::DrawGeometries({ source ,target }, "PointCloud", 1600, 900);
return 0;
}