03-点云文件常用格式转换(pcd,txt,ply,obj,stl)
1. pcd转txt
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <fstream> 3 #include <pcl/io/pcd_io.h> 4 5 int main(int argc, char *argv[]) 6 { 7 8 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); 9 pcl::io::loadPCDFile("rabbit.pcd", *cloud); 10 11 std::ofstream outfile; 12 outfile.open("rabbit.txt"); 13 14 for (size_t i = 0; i < cloud->points.size(); i++) 15 { 16 outfile << cloud->points[i].x << "\t" << cloud->points[i].y << "\t" << cloud->points[i].z << "\n"; 17 } 18 19 system("pause"); 20 21 return 0; 22 }
2. txt转pcd
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <fstream> 3 #include <pcl\io\pcd_io.h> 4 5 int main() 6 { 7 std::ifstream infile; 8 infile.open("rabbit.txt"); 9 10 float x, y, z; 11 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); 12 13 while (infile >> x >> y >> z) 14 { 15 cloud->push_back(pcl::PointXYZ(x, y, z)); 16 } 17 18 // 保存为二进制格式 19 //pcl::io::savePCDFileBinary("rabbit_new.pcd", *cloud); 20 21 // 保存为ASCII格式 22 pcl::io::savePCDFileASCII("rabbit_new.pcd", *cloud); 23 24 system("pause"); 25 26 return 0; 27 }
3. pcd转ply
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <pcl\io\pcd_io.h> 3 #include <pcl\io\ply_io.h> 4 #include <pcl\point_types.h> 5 6 int main() 7 { 8 9 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); 10 pcl::io::loadPCDFile("rabbit.pcd", *cloud); 11 12 pcl::io::savePLYFile("rabbit.ply", *cloud); 13 14 system("pause"); 15 16 return 0; 17 }
4.ply 转pcd
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <pcl\io\pcd_io.h> 3 #include <pcl\io\ply_io.h> 4 #include <pcl\point_types.h> 5 6 int main() 7 { 8 9 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); 10 11 pcl::io::loadPLYFile("rabbit.ply", *cloud); 12 13 14 pcl::io::savePCDFileBinary("rabbit.pcd", *cloud); 15 16 system("pause"); 17 18 return 0; 19 }
5. obj/ply转pcd
参见:https://blog.csdn.net/taifyang/article/details/116382273?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162746031516780261991459%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162746031516780261991459&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-8-116382273.first_rank_v2_pc_rank_v29&utm_term=pcd%E8%BD%ACnpy&spm=1018.2226.3001.4187