微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

读取 .las 文件,用 PCL 处理和显示它

如何解决读取 .las 文件,用 PCL 处理和显示它

我使用 liblas 库读取 .las file 的浊点。然后我将点存储在 PCL 点云变量中,以便使用点云库处理和显示点云。

这是我使用的代码

class PointCloud
{
    public:
        //PointCloud(const std::string& path);

        uint32_t getVertsCount();
        float4* getVertsData();

        template<typename PointT>
        typename pcl::PointCloud<PointT>::Ptr read(const std::string& path);//void read(const std::string &path);
}

template<typename PointT>
typename pcl::PointCloud<PointT>::Ptr PointCloud::read(const string& path)
{
    typename pcl::PointCloud<PointT>::Ptr lasCloud(new pcl::PointCloud<PointT>);

    std::ifstream ifs;
    ifs.open(path,std::ios::in | std::ios::binary);
    //std::ifstream inf(path,std::ios::in | std::ios::binary);

    liblas::ReaderFactory f;
    liblas::Reader reader = f.CreateWithStream(ifs);
    liblas::Header const& header = reader.GetHeader();

    std::cout << "Compressed: " << (header.Compressed() == true) ? "true" : "false";
    std::cout << "Signature: " << header.GetFileSignature() << '\n';
    std::cout << "Points count: " << header.GetPointRecordsCount() << '\n';

    while (reader.ReadNextPoint())
    {
        liblas::Point const& p = reader.GetPoint();

        PointT cloudPoint;
        cloudPoint.x = float(p.GetX()) * 0.001 + 590284.000; // (double)(x * scaleX) + offsetX;
        cloudPoint.y = float(p.GetY()) * 0.001 + 4339456.000; // (double)(y * scaleY) + offsetY;
        cloudPoint.z = float(p.GetZ()) * 0.001 + 157.000; // (double)(z * scaleZ) + offsetZ;
        std::cout << p.GetX() << "," << p.GetY() << "," << p.GetZ() << "\n";
        //cloudPoint.intensity = p.GetIntensity(); // (double)(intensity) / 65536.0;
        lasCloud->points.push_back(cloudPoint);
    }

    if (!ifs.good())
        throw runtime_error("Reading went wrong!");
        
    
    lasCloud->width = lasCloud->points.size();
    lasCloud->height = 1;
    lasCloud->is_dense = true;
    std::cout << "Cloud size = " << lasCloud->points.size() << endl;
    return lasCloud;
    

}

int main (int argc,char** argv)
{
    
    std::cout << "starting enviroment" << std::endl;
    pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
    CameraAngle setAngle = FPS; //XY,FPS,Side,TopDown
    initCamera(setAngle,viewer);
    pcl::PointCloud<pcl::PointXYZ>::Ptr inputCloudI; //
    inputCloudI = pcd.read<pcl::PointXYZ>("C:/Users/hedey/OneDrive/Documents/Research_papers/STDF/10_4231_MFQF-Q141/I-65/LiDAR/RoadSurface/NB/20180524_I65_NB_RoadSurface_1_50.5.las");
    std::cout << "Cloud size = " << inputCloudI->points.size() << endl;
    renderPointCloud(viewer,inputCloudI,"lasCloud");
    while (!viewer->wasstopped())
    {
        viewer->spinOnce();
    }
}

但是,使用 PCL 查看器显示的云看起来像一个点。我注意到的是,当我打印出我使用 liblas 读取的坐标时,x 和 y 坐标没有十进制值,这与存储在 las 文件中的实际坐标相比是不准确的。我从命令提示符使用 las2txt 获得了实际点坐标。 This is the txt file 包含实际坐标。这是显示 cout 结果的图像:

enter image description here

此外,这是我使用 CloudCompare 打开点云时的样子。当我将其读入 PCL 点云变量并使用 PCL 查看器显示结果时,我期待得到相同的显示,因为我需要做进一步的处理以进行传感器融合(相机和激光雷达)。

enter image description here

解决方法

std::cout 的默认精度为 6 位十进制数字。请添加类似

    std::cout.precision(12);

while 循环之前。

此外,将 p.GetX() 等转换为 float 是没有意义的:如果将其乘以 0.001,operator* 参数的左侧自然会提升为至少double。但是,float 只有大约 7 位的精度,因此对于存储在双精度内的 9 位整数(是的!),这种截断是灾难性的。

还有另一个(小)错误,正确的行是

    std::cout << "Compressed: " << ((header.Compressed() == true) ? "true\n" : "false\n");

请注意条件表达式(和 \n)周围的 () 大括号。请使用标准编译器选项,以针对此类简单问题发出警告。

另请阅读https://stackoverflow.com/help/minimal-reproducible-example

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。