PCL-01-点云文件读取、上色、可视化类和矩阵变换

1. 可视化pcd文件
pcl_viewer test.pcd
2. 加载并显示一个点云文件
//若无色彩的点云可使用PointCloud<pcl::PointXYZ>
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::loadPCDFile("./data/pcl_logo.pcd", *cloud);
pcl::visualization::CloudViewer viewer("Cloud Viewer");

//这里会一直阻塞直到点云被渲染
viewer.showCloud(cloud);
while (!viewer.wasStopped()) {
    //所有的处理都添加在这里
}
3. pcl_viewer工具的使用
  • pcl_viewer工具
    基本使用
    进入:pcl_viewer xxxxx.pcd
    帮助:在界面中输入h,可以在控制台看到帮助信息
    退出:界面中输入q
    放大缩小:鼠标滚轮 或 Alt + [+/-]
    平移:Shift+鼠标拖拽
    旋转:Ctrl+鼠标拖拽
  • 其他技巧
    修改点颜色:数字1,2,3,4,5…9,重复按1可切换不同颜色方便观察
    放大缩小点:放大Ctrl+Shift+加号,缩小 Ctrl+减号
    保存截图:j;显示颜色尺寸:u
    显示比例尺寸:g
    在控制列出所有几何和颜色信息:l
  • 鼠标选点打印坐标
    选点模式进入:pcl_viewer -use_point_picking bunny.pcd
    选择指定点:shift+鼠标左键
4. 阻塞显示之前做的一些显式初始化
viewerOneOff(pcl::visualization::PCLVisualizer &viewer) {
	 // 设置背景色为粉红色
	 viewer.setBackgroundColor(1.0, 0.5, 1.0);
	 pcl::PointXYZ o;
	 o.x = 1.0;
	 o.y = 0;
	 o.z = 0;
	 //    添加一个圆心为o,半径为0.25m的球体
	 viewer.addSphere(o, 0.25, "sphere", 0);
	 std::cout << "i only run once" << std::endl;
}

void viewerPsycho(pcl::visualization::PCLVisualizer &viewer) {
    static unsigned count = 0;
    std::stringstream ss;
    ss << "Once per viewer loop: " << count++;
    // 每次刷新时,移除text,添加新的text
    viewer.removeShape("text", 0);
    viewer.addText(ss.str(), 200, 300, "text", 0);  //文本放置的位置
}

// 只会调用一次 (非必须)
viewer.runOnVisualizationThreadOnce (viewerOneOff);
// 每次可视化迭代都会调用一次(频繁调用) (非必须)
viewer.runOnVisualizationThread (viewerPsycho);
4. PCLVisualizer类的使用
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>

int main(int argc, char **argv) {
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::io::loadPCDFile("./data/bunny.pcd", *cloud);

    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_milk(new pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::io::loadPCDFile("./data/milk_color.pcd", *cloud_milk);

    // 创建PCLVisualizer
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));

    // 设置背景色为灰色(非必须)
    viewer->setBackgroundColor (0.05, 0.05, 0.05, 0);

    // 添加一个普通点云 (可以设置指定颜色,也可以去掉single_color参数不设置)
    //pcl::visualization::PointCloudColorHandlerRandom<pcl::PointXYZ> single_color(cloud);   生成随机的颜色
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0, 255, 0);
    viewer->addPointCloud<pcl::PointXYZ> (cloud, single_color, "sample cloud");
    viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "sample cloud");

    // 再添加一个彩色点云及配置
    pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud_milk);
    viewer->addPointCloud<pcl::PointXYZRGB> (cloud_milk, rgb, "sample cloud milk");
    viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud milk");

    // 添加一个0.5倍缩放的坐标系(非必须)
    viewer->addCoordinateSystem (0.5);

    // 直到窗口关闭才结束循环
    while (!viewer->wasStopped()) {
        // 每次循环调用内部的重绘函数
        viewer->spinOnce();  // 需要添加这行
    }
    return 0;
}
5. 矩阵变换
// 创建仿射变换对象
Eigen::Affine3f transform_2 = Eigen::Affine3f::Identity();
// 在x轴平移0.8m
transform_2.translation() << 0.8, 0.0, 0.0;
// 绕Z轴先转45度(逆时针)
transform_2.rotate(Eigen::AngleAxisf(M_PI / 4, Eigen::Vector3f::UnitZ()));
// 打印仿射变换矩阵
printf("\nMethod #2: using an Affine3f\n");
std::cout << transform_2.matrix() << std::endl;
// 创建变换后的点云
pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud(new pcl::PointCloud<pcl::PointXYZ>());

// 执行变换, source_cloud为变换前的点云
pcl::transformPointCloud(*source_cloud, *transformed_cloud, transform_2);
//手动实现矩阵变换
Eigen::Matrix4f transform_1 = Eigen::Matrix4f::Identity();

// 定义旋转矩阵
float theta = M_PI / 4; // The angle of rotation in radians
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;
// Print the transformation
printf("Method #1: using a Matrix4f\n");
std::cout << transform_1 << std::endl;
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值