C++ 使用 Hough 变换检测 PLY 点云数据中的直线并可视化

下面是一个使用 Hough 变换检测直线并将其可视化的 C++ 代码。假设我们有一个 PLY 点云数据文件,其中包含了一些直线,我们想要将这些直线检测出来并将其可视化。

#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/common/transforms.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/features/normal_3d.h>
#include <pcl/kdtree/kdtree.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/filters/passthrough.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/segmentation/min_cut_segmentation.h>
#include <pcl/segmentation/euclidean_cluster_comparator.h>
#include <pcl/segmentation/organized_multi_plane_segmentation.h>
#include <pcl/features/integral_image_normal.h>
#include <pcl/segmentation/progressive_morphological_filter.h>
#include <pcl/segmentation/conditional_euclidean_clustering.h>
#include <pcl/segmentation/extract_polygonal_prism_data.h>
#include <pcl/features/moment_of_inertia_estimation.h>
#include <pcl/features/normal_3d_omp.h>
#include <pcl/console/parse.h>

int main(int argc, char** argv)
{
    std::string input_filename = 'input.ply';
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PLYReader reader;
    reader.read(input_filename, *cloud);

    // Voxel downsample the cloud
    pcl::VoxelGrid<pcl::PointXYZ> voxel_filter;
    voxel_filter.setInputCloud(cloud);
    voxel_filter.setLeafSize(0.01f, 0.01f, 0.01f);
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
    voxel_filter.filter(*cloud_filtered);

    // Create a PCL visualizer
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer('3D Viewer'));
    viewer->initCameraParameters();

    // Add the filtered cloud to the viewer
    viewer->addPointCloud<pcl::PointXYZ>(cloud_filtered, 'cloud');

    // Perform RANSAC to detect lines in the cloud
    pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
    pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
    pcl::SACSegmentation<pcl::PointXYZ> seg;
    seg.setOptimizeCoefficients(true);
    seg.setModelType(pcl::SACMODEL_LINE);
    seg.setMethodType(pcl::SAC_RANSAC);
    seg.setMaxIterations(10000);
    seg.setDistanceThreshold(0.01);
    seg.setInputCloud(cloud_filtered);
    seg.segment(*inliers, *coefficients);

    // Extract the inliers from the cloud
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inliers(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::ExtractIndices<pcl::PointXYZ> extract;
    extract.setInputCloud(cloud_filtered);
    extract.setIndices(inliers);
    extract.filter(*cloud_inliers);

    // Add the detected line to the viewer
    pcl::PointXYZ p1(coefficients->values[0], coefficients->values[1], coefficients->values[2]);
    pcl::PointXYZ p2(coefficients->values[3], coefficients->values[4], coefficients->values[5]);
    viewer->addLine<pcl::PointXYZ>(p1, p2, 1.0, 0.0, 0.0, 'line');

    // Spin the viewer
    viewer->spin();

    return 0;
}

在这个代码中,我们首先读入了一个 PLY 点云数据文件,然后对其进行了降采样,将其添加到了 PCL 可视化器中,并使用 RANSAC 算法检测了其中的直线。最后,我们将检测到的直线添加到了 PCL 可视化器中,并通过调用 spin() 方法来展示可视化结果。

C++ 使用 Hough 变换检测 PLY 点云数据中的直线并可视化

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

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