'include <pcl/io/ply_io.h>\n#include <pcl/point_types.h>\n#include <pcl/features/moment_of_inertia_estimation.h>\n#include <pcl/visualization/pcl_visualizer.h>\n\nint main()\n{\n // 加载点云数据\n pcl::PointCloudpcl::PointXYZ::Ptr cloud(new pcl::PointCloudpcl::PointXYZ);\n pcl::PLYReader reader;\n reader.read('D:\DIANYUNWENJIANJIA\茎直径切片投影_ply.ply', *cloud);\n\n // 创建 PCL 可视化对象\n pcl::visualization::PCLVisualizer viewer('PointCloud with Ellipses');\n viewer.setBackgroundColor(0.0, 0.0, 0.0);\n\n // 对每个切片进行椭圆拟合并可视化\n int numSlices = 10; // 切片数量\n for (int i = 0; i < numSlices; i++)\n {\n // 计算切片高度\n float sliceHeight = i * (cloud->height / numSlices);\n\n // 过滤出当前切片的点云数据\n pcl::PointCloudpcl::PointXYZ::Ptr sliceCloud(new pcl::PointCloudpcl::PointXYZ);\n for (const auto& point : cloud->points)\n {\n if (std::abs(point.z - sliceHeight) < 0.01) // 根据需要调整切片高度的容差值\n {\n sliceCloud->push_back(point);\n }\n }\n\n // 进行椭圆拟合\n pcl::MomentOfInertiaEstimationpcl::PointXYZ feature;\n feature.setInputCloud(sliceCloud);\n feature.compute();\n\n pcl::PointIndices::Ptr inliers(new pcl::PointIndices);\n feature.getContour(inliers->indices);\n\n Eigen::Vector3f position, majorAxis, middleAxis;\n feature.getOBB(position, majorAxis);\n\n std::vector eigenValues;\n feature.getEigenValues(eigenValues);\n float width = eigenValues[0];\n float height = eigenValues[1];\n\n Eigen::Quaternionf orientation = Eigen::Quaternionf::FromTwoVectors(Eigen::Vector3f::UnitX(), majorAxis.segment(0, 3));\n\n // 可视化当前切片点云和拟合椭圆\n pcl::visualization::PointCloudColorHandlerCustompcl::PointXYZ single_color(sliceCloud, 0, 255, 0);\n viewer.addPointCloudpcl::PointXYZ(sliceCloud, single_color, 'cloud' + std::to_string(i));\n // 使用pcl::visualization::PCLVisualizer::addSphere()方法绘制椭圆\n viewer.addSphere(position, width/2, height/2, orientation, 'ellipse' + std::to_string(i));\n\n // 输出椭圆的主轴长度\n std::cout << 'Slice ' << i+1 << ' - Width: ' << width << ', Height: ' << height << std::endl;\n }\n\n // 显示可视化结果\n while (!viewer.wasStopped())\n {\n viewer.spinOnce();\n }\n\n return 0;\n}\n'}

代码解释:

  1. addSphere() 函数替代 addEllipse(): PCL 可视化库中没有 addEllipse() 函数,可以使用 addSphere() 函数来绘制椭圆。addSphere() 函数需要提供球心位置、半径和方向信息。2. getOBB() 函数调整: getOBB() 函数应该只传入两个参数,一个是球心位置 position,另一个是主轴方向 majorAxis。3. getEigenValues() 函数调整: getEigenValues() 函数需要传入一个 std::vector<float> 类型的参数,用于保存特征值。代码中使用 eigenValues 变量接收特征值,并从中取出 width 和 height。

**完整代码:**c++#include <pcl/io/ply_io.h>#include <pcl/point_types.h>#include <pcl/features/moment_of_inertia_estimation.h>#include <pcl/visualization/pcl_visualizer.h>

int main(){ // 加载点云数据 pcl::PointCloudpcl::PointXYZ::Ptr cloud(new pcl::PointCloudpcl::PointXYZ); pcl::PLYReader reader; reader.read('D:\DIANYUNWENJIANJIA\茎直径切片投影_ply.ply', *cloud);

// 创建 PCL 可视化对象    pcl::visualization::PCLVisualizer viewer('PointCloud with Ellipses');    viewer.setBackgroundColor(0.0, 0.0, 0.0);

// 对每个切片进行椭圆拟合并可视化    int numSlices = 10; // 切片数量    for (int i = 0; i < numSlices; i++)    {        // 计算切片高度        float sliceHeight = i * (cloud->height / numSlices);

    // 过滤出当前切片的点云数据        pcl::PointCloud<pcl::PointXYZ>::Ptr sliceCloud(new pcl::PointCloud<pcl::PointXYZ>);        for (const auto& point : cloud->points)        {            if (std::abs(point.z - sliceHeight) < 0.01) // 根据需要调整切片高度的容差值            {                sliceCloud->push_back(point);            }        }

    // 进行椭圆拟合        pcl::MomentOfInertiaEstimation<pcl::PointXYZ> feature;        feature.setInputCloud(sliceCloud);        feature.compute();

    pcl::PointIndices::Ptr inliers(new pcl::PointIndices);        feature.getContour(inliers->indices);

    Eigen::Vector3f position, majorAxis, middleAxis;        feature.getOBB(position, majorAxis);

    std::vector<float> eigenValues;        feature.getEigenValues(eigenValues);        float width = eigenValues[0];        float height = eigenValues[1];

    Eigen::Quaternionf orientation = Eigen::Quaternionf::FromTwoVectors(Eigen::Vector3f::UnitX(), majorAxis.segment(0, 3));

    // 可视化当前切片点云和拟合椭圆        pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(sliceCloud, 0, 255, 0);        viewer.addPointCloud<pcl::PointXYZ>(sliceCloud, single_color, 'cloud' + std::to_string(i));        // 使用pcl::visualization::PCLVisualizer::addSphere()方法绘制椭圆        viewer.addSphere(position, width / 2, height / 2, orientation, 'ellipse' + std::to_string(i));

    // 输出椭圆的主轴长度        std::cout << 'Slice ' << i + 1 << ' - Width: ' << width << ', Height: ' << height << std::endl;    }

// 显示可视化结果    while (!viewer.wasStopped())    {        viewer.spinOnce();    }

return 0
PCL 点云数据椭圆拟合可视化 - 茎直径切片分析

原文地址: https://www.cveoy.top/t/topic/qcYr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录