C++【PCL】利用矩阵对点云进行刚体变换

cpp 复制代码
/*
 * 功能: 点云刚体变换
 * 头文件: #include <pcl/common/transforms.h>
 * 功能函数: pcl::transformPointCloud(*pPointCloudIn, *pPointCloudOut, transform_1);
 */


#include <pcl/io/ply_io.h>

#include <iostream>
 // pcl
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/common/transforms.h>

int main()
{
    // 1. 读入点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr pPointCloudIn(new pcl::PointCloud<pcl::PointXYZ>());
    //pcl::io::loadPCDFile("Box.pcd", *pPointCloudIn);
    pcl::io::loadPLYFile<pcl::PointXYZ>("bunny.ply", *pPointCloudIn);

    // 2. 可视窗口初始化
    pcl::visualization::PCLVisualizer viewer;
    viewer.setCameraFieldOfView(0.785398);      // fov 大概45度
    viewer.setBackgroundColor(0.5, 0.5, 0.5);   // 背景设为灰色
    viewer.setCameraPosition(
        0, 0, 3,                                // camera位置
        0, 0, -1,                               // view向量--相机朝向
        0, 1, 0                                 // up向量
    );

    // 3. 点云旋转
    /* 提示: 变换矩阵工作原理 :

    |-------> 这一列表示平移向量
    | 1 0 0 x |  \
    | 0 1 0 y |   }-> 左侧的 3x3 matrix表示旋转,这里用无旋转举例
    | 0 0 1 z |  /
    | 0 0 0 1 |    -> 这行没有实际意义只是为了计算补充的 (始终为 0,0,0,1)

    方法 #1: 使用一个Matrix4f类型矩阵(f代表float)
    这是手工方式,易于理解但是容易出错!
    */
    Eigen::Matrix4f transform_1 = Eigen::Matrix4f::Identity();      // 单位阵

    // 定义一个旋转矩阵表示绕z轴转PI/4 (从这里可以查看旋转矩阵 https://en.wikipedia.org/wiki/Rotation_matrix)
    float theta = M_PI / 4; // 旋转的角度,以弧度为单位
    transform_1(0, 0) = cos(theta);
    transform_1(0, 1) = -sin(theta);
    transform_1(1, 0) = sin(theta);
    transform_1(1, 1) = cos(theta);
    // 圆括号内的意义(元素在矩阵中的行, 元素在矩阵中的列)

    // 定义沿着x轴平移2.5m
    transform_1(0, 3) = 2.5;

    // 打印变换矩阵
    printf("Method #1: using a Matrix4f\n");
    std::cout << transform_1 << std::endl;

    /*  方法 #2: 使用一个Affine3f类型
    这个方法更简单也不容易出错
    */
    Eigen::Affine3f transform_2 = Eigen::Affine3f::Identity();

    // 定义沿着x轴平移2.5m
    transform_2.translation() << 2.5, 0.0, 0.0;

    // 与之前的旋转定义一样,绕z轴旋转一个theta角
    transform_2.rotate(Eigen::AngleAxisf(theta, Eigen::Vector3f::UnitZ()));

    // 打印变换矩阵
    printf("\nMethod #2: using an Affine3f\n");
    std::cout << transform_2.matrix() << std::endl;

    // 执行变换
    pcl::PointCloud<pcl::PointXYZ>::Ptr pPointCloudOut(new pcl::PointCloud<pcl::PointXYZ>());
    // 你可以使用 transform_1 或者 transform_2; 因为他们都一样
    pcl::transformPointCloud(*pPointCloudIn, *pPointCloudOut, transform_1);

    // 4. 点云可视化
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> inColorHandler(pPointCloudIn, 255, 255, 255);// 白色
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> outColorHandler(pPointCloudOut, 230, 20, 20); // 红色
    viewer.addPointCloud(pPointCloudIn, inColorHandler, "In");
    viewer.addPointCloud(pPointCloudOut, outColorHandler, "Out");
    viewer.addCoordinateSystem(1.0, "cloud", 0);
    while (!viewer.wasStopped()) { // 显示,直到'q'键被按下
        viewer.spinOnce();
    }

    std::system("pause");
    return 0;
}
相关推荐
辞旧 lekkk6 小时前
【Qt】信号和槽
linux·开发语言·数据库·qt·学习·mysql·萌新
2zcode6 小时前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩19976 小时前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
Aaswk7 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite8 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
王老师青少年编程9 小时前
csp信奥赛C++高频考点专项训练之字符串 --【子串查找】:[NOIP 2009 提高组] 潜伏者
c++·字符串·csp·高频考点·信奥赛·子串查找·潜伏者
Cyber4K9 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
初願致夕霞9 小时前
基于系统调用的Linux网络编程——UDP与TCP
linux·网络·c++·tcp/ip·udp
Le_ee9 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
yong999010 小时前
MATLAB读取高光谱图像
开发语言·matlab