点云学习笔记21——给边界点云离散点进行编号

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

// 计算极角函数(以第一个点为基准点计算极角)
struct PolarAngleComparator
{
    pcl::PointXYZ base_point;

    PolarAngleComparator(const pcl::PointXYZ& base) : base_point(base) {}

    bool operator()(const pcl::PointXYZ& a, const pcl::PointXYZ& b) const
    {
        float angle_a = std::atan2(a.y - base_point.y, a.x - base_point.x);
        float angle_b = std::atan2(b.y - base_point.y, b.x - base_point.x);
        return angle_a < angle_b;
    }
};

int main(int argc, char** argv)
{
    // 创建点云对象并读取文件
    pcl::PointCloud<pcl::PointXYZ>::Ptr edge_points(new pcl::PointCloud<pcl::PointXYZ>);
    std::string file_path = "F:\\edge_points.pcd";  // 替换为你的边缘点文件路径

    if (pcl::io::loadPCDFile<pcl::PointXYZ>(file_path, *edge_points) == -1)
    {
        PCL_ERROR("Couldn't read PCD file \n");
        return -1;
    }

    std::cout << "Loaded point cloud with " << edge_points->points.size() << " edge points.\n";

    // 找到第一个点作为基准点(这里选择点云中最左下角的点)
    auto base_iter = std::min_element(edge_points->points.begin(), edge_points->points.end(), [](const pcl::PointXYZ& a, const pcl::PointXYZ& b)
    {
        return (a.y < b.y) || (a.y == b.y && a.x < b.x);
    });
    pcl::PointXYZ base_point = *base_iter;

    // 对点进行极角排序
    std::vector<pcl::PointXYZ> sorted_points(edge_points->points.begin(), edge_points->points.end());
    std::sort(sorted_points.begin(), sorted_points.end(), PolarAngleComparator(base_point));

    // 创建排序后的点云对象
    pcl::PointCloud<pcl::PointXYZ>::Ptr sorted_edge_points(new pcl::PointCloud<pcl::PointXYZ>);
    sorted_edge_points->points.assign(sorted_points.begin(), sorted_points.end());
    sorted_edge_points->width = sorted_edge_points->points.size();
    sorted_edge_points->height = 1;
    sorted_edge_points->is_dense = true;

    // 保存排序后的点云到PCD文件
    std::string output_file_path = "F:\\sorted_edge_points.pcd";  // 替换为你的保存路径
    if (pcl::io::savePCDFileASCII(output_file_path, *sorted_edge_points) == -1)
    {
        PCL_ERROR("Couldn't save sorted PCD file \n");
        return -1;
    }
    std::cout << "Saved sorted edge points to " << output_file_path << std::endl;

    // 初始化可视化器
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Edge Points Viewer"));
    viewer->setBackgroundColor(0, 0, 0);
    viewer->addPointCloud<pcl::PointXYZ>(sorted_edge_points, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(sorted_edge_points, 0, 255, 0), "sorted edge points");

    // 每隔 50 个点进行编号
    int label_counter = 1;
    for (size_t i = 0; i < sorted_edge_points->points.size(); i += 50)
    {
        pcl::PointXYZ point = sorted_edge_points->points[i];
        std::string label = std::to_string(label_counter);

        // 在点上添加编号标签
        viewer->addText3D(label, point, 0.02, 1.0, 1.0, 1.0, "label" + std::to_string(i));

        // 输出编号和坐标
        std::cout << "Point " << label_counter << ": (" << point.x << ", " << point.y << ", " << point.z << ")" << std::endl;

        label_counter++;  // 增加编号
    }

    // 开始可视化
    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);
    }

    return 0;
}

增加编号点的颜色和大小

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

// 计算极角函数(以第一个点为基准点计算极角)
struct PolarAngleComparator
{
    pcl::PointXYZ base_point;

    PolarAngleComparator(const pcl::PointXYZ& base) : base_point(base) {}

    bool operator()(const pcl::PointXYZ& a, const pcl::PointXYZ& b) const
    {
        float angle_a = std::atan2(a.y - base_point.y, a.x - base_point.x);
        float angle_b = std::atan2(b.y - base_point.y, b.x - base_point.x);
        return angle_a < angle_b;
    }
};

int main(int argc, char** argv)
{
    // 创建点云对象并读取文件
    pcl::PointCloud<pcl::PointXYZ>::Ptr edge_points(new pcl::PointCloud<pcl::PointXYZ>);
    std::string file_path = "F:\\boundary11.pcd";  // 替换为你的边缘点文件路径

    if (pcl::io::loadPCDFile<pcl::PointXYZ>(file_path, *edge_points) == -1)
    {
        PCL_ERROR("Couldn't read PCD file \n");
        return -1;
    }

    std::cout << "Loaded point cloud with " << edge_points->points.size() << " edge points.\n";

    // 找到第一个点作为基准点(最左下角的点)
    auto base_iter = std::min_element(edge_points->points.begin(), edge_points->points.end(), [](const pcl::PointXYZ& a, const pcl::PointXYZ& b)
        {
            return (a.y < b.y) || (a.y == b.y && a.x < b.x);
        });
    pcl::PointXYZ base_point = *base_iter;

    // 提取角点并排序
    std::vector<pcl::PointXYZ> sorted_points(edge_points->points.begin(), edge_points->points.end());
    std::sort(sorted_points.begin(), sorted_points.end(), PolarAngleComparator(base_point));

    // 创建一个用于标记编号点的点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr labeled_points(new pcl::PointCloud<pcl::PointXYZ>);

    // 初始化可视化器
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Edge Points Viewer"));
    viewer->setBackgroundColor(0, 0, 0);
    viewer->addPointCloud<pcl::PointXYZ>(edge_points, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(edge_points, 0, 255, 0), "edge points");

    // 每隔 50 个点进行编号,并将编号的点添加到 labeled_points 中
    int label_counter = 1;
    for (size_t i = 0; i < sorted_points.size(); i += 50)
    {
        pcl::PointXYZ point = sorted_points[i];
        labeled_points->points.push_back(point);

        std::string label = std::to_string(label_counter);

        // 在点上添加编号标签
        viewer->addText3D(label, point, 54, 1.0, 1.0, 1.0, "label" + std::to_string(i));

        // 输出编号和坐标
        std::cout << "Point " << label_counter << ": (" << point.x << ", " << point.y << ", " << point.z << ")" << std::endl;

        label_counter++;  // 增加编号
    }

    // 设置编号点的颜色为红色并添加到可视化器
    viewer->addPointCloud<pcl::PointXYZ>(labeled_points, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(labeled_points, 255, 0, 0), "labeled points");

    // 设置编号点的大小
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "labeled points");

    // 开始可视化
    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);
    }

    return 0;
}

第三版 

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

// 计算极角函数(以第一个点为基准点计算极角)
struct PolarAngleComparator
{
    pcl::PointXYZ base_point;

    PolarAngleComparator(const pcl::PointXYZ& base) : base_point(base) {}

    bool operator()(const pcl::PointXYZ& a, const pcl::PointXYZ& b) const
    {
        float angle_a = std::atan2(a.y - base_point.y, a.x - base_point.x);
        float angle_b = std::atan2(b.y - base_point.y, b.x - base_point.x);
        return angle_a < angle_b;
    }
};

int main(int argc, char** argv)
{
    // 创建点云对象并读取文件
    pcl::PointCloud<pcl::PointXYZ>::Ptr edge_points(new pcl::PointCloud<pcl::PointXYZ>);
    std::string file_path = "F:\\boundary11.pcd";  // 替换为你的边缘点文件路径

    if (pcl::io::loadPCDFile<pcl::PointXYZ>(file_path, *edge_points) == -1)
    {
        PCL_ERROR("Couldn't read PCD file \n");
        return -1;
    }

    std::cout << "Loaded point cloud with " << edge_points->points.size() << " edge points.\n";

    // 找到第一个点作为基准点(最左下角的点)
    auto base_iter = std::min_element(edge_points->points.begin(), edge_points->points.end(), [](const pcl::PointXYZ& a, const pcl::PointXYZ& b)
        {
            return (a.y < b.y) || (a.y == b.y && a.x < b.x);
        });
    pcl::PointXYZ base_point = *base_iter;

    // 提取角点并排序
    std::vector<pcl::PointXYZ> sorted_points(edge_points->points.begin(), edge_points->points.end());
    std::sort(sorted_points.begin(), sorted_points.end(), PolarAngleComparator(base_point));

    // 创建一个用于标记编号顶点的点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr labeled_points(new pcl::PointCloud<pcl::PointXYZ>);

    // 初始化可视化器
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Edge Points Viewer"));
    viewer->setBackgroundColor(0, 0, 0);
    viewer->addPointCloud<pcl::PointXYZ>(edge_points, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(edge_points, 0, 255, 0), "edge points");

    // 按每隔 (sorted_points.size() / 16) 个点获取十六边形的顶点
    int interval = sorted_points.size() / 16;
    for (int i = 0; i < 16; ++i)
    {
        int index = i * interval;
        pcl::PointXYZ vertex = sorted_points[index];
        labeled_points->points.push_back(vertex);

        std::string label = std::to_string(i + 1);

        // 在点上添加编号标签
        viewer->addText3D(label, vertex, 54, 1.0, 1.0, 1.0, "label" + std::to_string(i));

        // 输出编号和坐标
        std::cout << "Vertex " << label << ": (" << vertex.x << ", " << vertex.y << ", " << vertex.z << ")" << std::endl;
    }

    // 设置编号顶点的颜色为红色并添加到可视化器
    viewer->addPointCloud<pcl::PointXYZ>(labeled_points, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(labeled_points, 255, 0, 0), "labeled points");

    // 设置编号顶点的大小
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "labeled points");

    // 开始可视化
    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值