PCL获取点云中某点坐标

#include <string>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>

using PointT = pcl::PointXYZ;
using PointCloudT = pcl::PointCloud<PointT>;

// 用于将参数传递给回调函数的结构体
struct CallbackArgs {
    PointCloudT::Ptr clicked_points_3d;
    pcl::visualization::PCLVisualizer::Ptr viewerPtr;
};

void pickPointCallback(const pcl::visualization::PointPickingEvent& event, void* args) {
    CallbackArgs* data = (CallbackArgs*)args;
    if (event.getPointIndex() == -1)
        return;
    PointT current_point;
    event.getPoint(current_point.x, current_point.y, current_point.z);
    data->clicked_points_3d->points.push_back(current_point);
    // 绘制红色点
    pcl::visualization::PointCloudColorHandlerCustom<PointT> red(data->clicked_points_3d, 255, 0, 0);
    data->viewerPtr->removePointCloud("clicked_points");
    data->viewerPtr->addPointCloud(data->clicked_points_3d, red, "clicked_points");
    data->viewerPtr->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 10,
        "clicked_points");
    std::cout << current_point.x << " " << current_point.y << " " << current_point.z << std::endl;
}

int main() {
    std::string file_name("d://PCD//rabbit.pcd");
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));
    // 加载点云
    if (pcl::io::loadPCDFile(file_name, *cloud) == -1) {
        std::cerr << "could not load file: " << file_name << std::endl;
    }
    std::cout << cloud->points.size() << std::endl;
    // 显示点云
    viewer->addPointCloud(cloud, "cloud");
    viewer->setCameraPosition(0, 0, -2, 0, -1, 0, 0);
    // 添加点拾取回调函数
    CallbackArgs  cb_args;
    PointCloudT::Ptr clicked_points_3d(new PointCloudT);
    cb_args.clicked_points_3d = clicked_points_3d;
    cb_args.viewerPtr = pcl::visualization::PCLVisualizer::Ptr(viewer);
    viewer->registerPointPickingCallback(pickPointCallback, (void*)&cb_args);
    std::cout << "Shift+click on three floor points, then press 'Q'..." << std::endl;

    viewer->spin();
    std::cout << "done." << std::endl;

    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);
    }
    return 0;
}
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PCL(Point Cloud Library)是一个非常流行的点云处理库,可以用于点云的各种操作和处理。点云投影到像素坐标系的变换可以通过使用相机参数来实现。具体来说,需要知道相机的内部参数(比如焦距、像素大小、主点位置等)和外部参数(比如相机的位置和朝向)。通过这些参数,可以将点云的三维坐标转换为相机坐标系下的二维坐标,然后再将其转换为像素坐标系下的坐标。 具体的变换过程可以参考以下步骤: 1. 首先需要获取相机的内部参数和外部参数。内部参数可以通过相机的标定来获得,外部参数可以通过运动捕捉系统或者其他手段获取。 2. 接下来需要将点云的每个点从世界坐标系转换为相机坐标系下的坐标。这个过程可以通过以下公式实现: Xc = R * Xw + t 其Xc是点在相机坐标系下的坐标,Xw是点在世界坐标系下的坐标,R和t分别是相机的旋转矩阵和平移矩阵。 3. 将相机坐标系下的坐标转换为像素坐标系下的坐标。这个过程需要使用相机的内部参数,具体来说,可以通过以下公式实现: u = fx * Xc(1) / Xc(3) + cx v = fy * Xc(2) / Xc(3) + cy 其u和v是点在像素坐标系下的坐标,fx和fy是相机的焦距,cx和cy是主点位置。 4. 最后,将像素坐标系下的坐标四舍五入到整数值,并将其限制在图像的范围内,就可以得到该点在图像的像素位置了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值