栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

基于PCL的点云三角化

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于PCL的点云三角化

源代码:

#include
#include
#include 
#include
#include 
#include 
#include 
#include 
#include 

int main(int argc, char** argv) {
    pcl::PointCloud::Ptr cloud(new pcl::PointCloud());
    pcl::PCLPointCloud2 cloud_blob;
    //*打开点云文件
    if (pcl::io::loadPCDFile("F:\PCL_Rabbit\ConsoleApplication1\rabbit.pcd", cloud_blob) == -1) {
        PCL_ERROR("Couldn't read file rabbit.pcdn");
        return(-1);
    }
    pcl::fromPCLPointCloud2(cloud_blob, *cloud);

    //法线估计对象
    pcl::NormalEstimation n;
    //存储估计的法线
    pcl::PointCloud::Ptr normals(new pcl::PointCloud);
    //定义kd树指针
    pcl::search::KdTree::Ptr tree(new pcl::search::KdTree);
    tree->setInputCloud(cloud);
    n.setInputCloud(cloud);
    n.setSearchMethod(tree);
    n.setKSearch(20);
    //估计法线存储到其中
    n.compute(*normals);//Concatenate the XYZ and normal fields*
    pcl::PointCloud::Ptr cloud_width_normals(new pcl::PointCloud);
    //链接字段
    pcl::concatenateFields(*cloud, *normals, *cloud_width_normals);

    //定义搜索树对象
    pcl::search::KdTree::Ptr tree2(new pcl::search::KdTree);
    //点云构建搜索树
    tree2->setInputCloud(cloud_width_normals);

    //定义三角化对象
    pcl::GreedyProjectionTriangulation gp3;
    //存储最终三角化的网络模型
    pcl::PolygonMesh triangles;//设置连接点之间的最大距离,(即是三角形最大边长)
    gp3.setSearchRadius(200.0f);
    //设置各种参数值
    gp3.setMu(2.5f);
    gp3.setMaximumNearestNeighbors(100);
    gp3.setMaximumSurfaceAngle(M_PI_4);
    gp3.setMinimumAngle(M_PI / 18);
    gp3.setMaximumAngle(2 * M_PI / 3);
    gp3.setNormalConsistency(false);

    //设置搜索方法和输入点云
    gp3.setInputCloud(cloud_width_normals);
    gp3.setSearchMethod(tree2);

    //执行重构,结果保存在triangles中
    gp3.reconstruct(triangles);

    //保存网格图  
    //pcl::io::saveOBJFile("result.obj", triangles);
    std::string output_dir = "F:\PCL_Rabbit\ConsoleApplication1\cloud_mesh.ply";
    std::string sav = "saved mesh in:";
    sav += output_dir;
    pcl::console::print_info(sav.c_str());
    std::cout << std::endl;

    pcl::io::savePLYFileBinary(output_dir.c_str(), triangles);

    // 显示结果图  
    boost::shared_ptr viewer(new pcl::visualization::PCLVisualizer("MAP3D MESH"));
    设置背景;
    viewer->setBackgroundColor(0, 0, 0);
    //设置显示的网格
    viewer->addPolygonMesh(triangles, "my");
    //viewer->initCameraParameters();
    while (!viewer->wasStopped()) {
        viewer->spin();
    }
    std::cout << "success" << std::endl;
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号