二进制点云文件读取格式[x_1y_1z_1x_2y_2z_2...],最后保存为ply格式点云

循环读取存储格式为32位单精度浮点数的二进制三维点数据文件   转换为点云数据并保存



#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/common/io.h>
#include <pcl/impl/point_types.hpp>
#include <pcl/point_cloud.h>

#include <fstream>
#include <vector>
using namespace std;
// int main()
// {
//     BYTE s[4];

//     // s[3] = 0x00;
//     // s[2] = 0x00;
//     // s[1] = 0x10;
//     // s[0] = 0x2c;
//     //ff8f 0843
//     s[3] = 0x43;
//     s[2] = 0x77;
//     s[1] = 0xb1;
//     s[0] = 0x01;

//     float *pf = (float *)s;

//     printf("10进制%g\n", *pf);
// }

class Point_3d
{

public:
    float m_x;
    float m_y;
    float m_z;

public:
    Point_3d(float &x, float &y, float &z) : m_x(x), m_y(y), m_z(z)
    {
    }
};

vector<Point_3d> get_fs()
{
    ifstream ifs("scan.bin", std::ios::binary);
    float xyz[3] = {};
    // float xyz_1[3] = {};
    vector<Point_3d> pts;
    if (!ifs)
    {
        cerr << "open error !" << endl;
        abort();
    }

    while (!ifs.eof())
    {
        ifs.read((char *)xyz, sizeof(xyz));

        Point_3d pt(xyz[0], xyz[1], xyz[2]);
        pts.push_back(pt);
        // ifs.seekg(sizeof(xyz), ios::cur);
        // ifs.read((char *)xyz_1, sizeof(xyz_1));
    }
    ifs.clear();
    ifs.close();

    return pts;
}

int main(int argc, char **argv)
{
    pcl::PointCloud<pcl::PointXYZ> cloud;
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
    // Fill in the cloud data
    cloud.width = 1000;
    cloud.height = 1000;
    cloud.is_dense = false;
    cloud.points.resize(cloud.width * cloud.height);

    cloud_ptr->width = cloud.width;
    cloud_ptr->height = cloud.height;
    cloud_ptr->is_dense = cloud.is_dense;
    // cloud_ptr->points.resize (cloud.width * cloud.height);

    //   for (size_t i = 0; i < cloud.points.size (); ++i)
    //   {
    //    // cloud.points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
    //    // cloud.points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
    //    // cloud.points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);

    //         pcl::PointXYZ p;
    //         p.x = 1024 * rand () / (RAND_MAX + 1.0f);
    //         p.y = 1024 * rand () / (RAND_MAX + 1.0f);
    //         p.z = 1024 * rand () / (RAND_MAX + 1.0f);
    //         cloud.points[i].x = p.x;
    //         cloud.points[i].y = p.y;
    //         cloud.points[i].z = p.z;

    //         cloud_ptr->points.push_back(p);
    //   }

    vector<Point_3d> pts = get_fs();
    for (int i = 0; i < pts.size(); i++)
    {
        cloud.points[i].x = pts[i].m_x;
        cloud.points[i].y = pts[i].m_y;
        cloud.points[i].z = pts[i].m_z;

        pcl::PointXYZ p;
        p.x = pts[i].m_x;
        p.y = pts[i].m_y;
        p.z = pts[i].m_z;
        cloud_ptr->points.push_back(p);
    }

    std::cerr << "Saving to pcd file " << std::endl;
    pcl::io::savePCDFileASCII("test_pcd0.pcd", cloud);
    pcl::io::savePCDFile("test_pcd1.pcd", *cloud_ptr);
    std::cerr << "Saved " << cloud.points.size() << " data points to test_pcd.pcd." << std::endl;

    std::cerr << "Saving to ply file " << std::endl;
    pcl::io::savePLYFile("test_ply.ply", cloud);

    for (size_t i = 0; i < cloud.points.size(); ++i)
    {
        //  std::cerr << "    " << cloud.points[i].x << " " << cloud.points[i].y << " " << cloud.points[i].z << std::endl;
        std::cerr << "    " << cloud_ptr->points[i].x << " " << cloud_ptr->points[i].y << " " << cloud_ptr->points[i].z << std::endl;
    }

    return (0);
}

//采用CPP模式读二进制文件
// void DataRead_CPPMode()
// {
// 	double pos[200];
// 	ifstream f("binary.dat", ios::binary);
// 	if(!f)
// 	{
// 		cout << "读取文件失败" <<endl;
// 		return;
// 	}
// 	f.read((char*)pos,200*sizeof(double));
// 	for(int i = 0; i < 200; i++)
// 		cout << pos[i] <<endl;
// 	f.close();

// }

点云图效果

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
读取二进制lvx格式点云文件保存为pcd格式,可以通过以下步骤实现: 1. 打开lvx文件读取文件头,获取点云数据的起始位置和点云数据的总大小。 2. 读取点云数据,将其解析为点的(x,y,z)坐标和颜色信息。 3. 将点云数据保存为pcd格式文件。 以下是示例代码: ```c++ #include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; #pragma pack(push, 1) struct PointXYZRGB { float x; float y; float z; uint8_t r; uint8_t g; uint8_t b; }; #pragma pack(pop) int main() { string lvx_file_path = "input.lvx"; string pcd_file_path = "output.pcd"; // 打开lvx文件读取文件头 ifstream lvx_file(lvx_file_path, ios::binary); if (!lvx_file.is_open()) { cerr << "Error: Unable to open file " << lvx_file_path << endl; return -1; } lvx_file.seekg(80, ios::beg); // 文件头固定长度为80字节 uint64_t data_start_pos = 0; uint64_t data_size = 0; lvx_file.read((char*)&data_start_pos, sizeof(data_start_pos)); lvx_file.read((char*)&data_size, sizeof(data_size)); // 读取点云数据 lvx_file.seekg(data_start_pos, ios::beg); vector<PointXYZRGB> points(data_size / sizeof(PointXYZRGB)); lvx_file.read((char*)points.data(), data_size); lvx_file.close(); // 将点云数据保存为pcd格式文件 ofstream pcd_file(pcd_file_path); pcd_file << "# .PCD v0.7 - Point Cloud Data file format\n"; pcd_file << "VERSION 0.7\n"; pcd_file << "FIELDS x y z rgb\n"; pcd_file << "SIZE 4 4 4 4\n"; pcd_file << "TYPE F F F U\n"; pcd_file << "COUNT 1 1 1 1\n"; pcd_file << "WIDTH " << points.size() << endl; pcd_file << "HEIGHT 1\n"; pcd_file << "POINTS " << points.size() << endl; pcd_file << "DATA ascii\n"; for (const auto& p : points) { uint32_t rgb = (p.r << 16) | (p.g << 8) | p.b; pcd_file << p.x << " " << p.y << " " << p.z << " " << rgb << endl; } pcd_file.close(); return 0; } ``` 注意:此代码仅供参考,具体实现可能因文件格式和数据结构而异。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值