PCL点云库使用

一、下载PCL文件,下载地址

// 如1.12.0版本
PCL-1.12.0-AllInOne-msvc2019-win64.exe   // 安装该文件可为后续编译源码提供必要的第三方库
pcl-1.12.0-pdb-msvc2019-win64.zip
source.tar.gz或source.zip

在这里插入图片描述

安装PCL-1.12.0-AllInOne-msvc2019-win64.exe,安装过程中会弹出选择窗口,选择允许,安装完成后
切换到D:\Environment\PCL\PCL1.12.0\3rdParty\OpenNI2
运行OpenNI-Windows-x64-2.2.msi,前面默认安装在了C盘,先卸载,再重新安装在该文件夹下

将pcl-1.12.0-pdb-msvc2019-win64.zip解压后的.pdb文件拷贝到PCL-1.12.0-AllInOne-msvc2019-win64.exe安装路径bin文件夹下

在这里插入图片描述
添加环境变量如下
在这里插入图片描述
添加环境变量后,运行D:\Environment\PCL\PCL1.12.0\bin下的可执行文件应该不会再提示找不到**.dll

二、编译源文件
打开cmake-gui
在这里插入图片描述
选择源文件路径及生成路径,点击configure 选择vs2019,默认x64,finished,过程中会提示:

1.找不到eigen库,如下图添加eigen3的路径
在这里插入图片描述

  1. 找不到boost
    添加entry, BOOST_ROOT,如路径为D:\Environment\PCL\PCL1.12.0\3rdParty\Boost
    添加entry, BOOST_INCLUDEDIR ,如路径为D:\Environment\PCL\PCL1.12.0\3rdParty\Boost\include
    添加entry, BOOST_LIBRARYDIR, 如路径为D:\Environment\PCL\PCL1.12.0\3rdParty\Boost\lib

再次点击configure,若无报错后,修改安装路径(自定义)
在这里插入图片描述
再次configure,然后Generate。

在D:\Environment\PCL\source_build中,确保已添加环境变量后(在终端中,确保你的构建工具路径被正确添加到 PATH 环境变量。例如,如果 MSBuild 的路径为 D:\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin),则添加如下环境变量
在这里插入图片描述

打开powershell

//如果使用的是 Debug 配置,将 Release 替换为 Debug。
cmake --build . --config Release  
// 将安装在前面设置的路径下
cmake --install . --config Release

在这里插入图片描述
然后添加环境变量 D:\Environment\PCL\source_pcl_install\bin

测试简单案例
新建main.cpp

#include <pcl/point_cloud.h>
#include <pcl/visualization/cloud_viewer.h>

int main() {
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);

    // Add some points to the cloud (replace this with your actual data loading logic)
    cloud->width = 100;
    cloud->height = 1;
    cloud->points.resize(cloud->width * cloud->height);
    for (size_t i = 0; i < cloud->points.size(); ++i) {
        cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
    }

    pcl::visualization::CloudViewer viewer("Cloud Viewer");
    viewer.showCloud(cloud);
    while (!viewer.wasStopped()) {
        // Do nothing, just keep the viewer open
    }

    return 0;
}

CMakeLists.txt内容如下

cmake_minimum_required(VERSION 3.0)
project(PCLTest)

find_package(PCL REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable(PCLTest main.cpp)
target_link_libraries(PCLTest ${PCL_LIBRARIES})
// 打开power shell或cmd ,进入项目目录:
cd path/to/your/project
// 使用 CMake 构建项目,如果使用的是 Debug 配置,将 Release 替换为 Debug
cmake -DCMAKE_BUILD_TYPE=Release .


// 使用构建工具(比如 make 或者 cmake --build .) 构建项目,如果使用的是 Debug 配置,将 Release 替换为 Debug
cmake --build . --config Release

运行生成的可执行文件:
./PCLTest
或者在 Windows 下:
PCLTest.exe
如果一切顺利,你应该能够看到一个简单的点云可视化窗口弹出,并显示生成的随机点云。这个测试案例可以确保你的 PCL 库已经正确安装并可以被你的项目使用。

或者在默认坐标系下生成一些点


#include <iostream>
#include <vector>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>

int main() {
    // 创建可视化对象
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Coordinate System"));

    // 添加 PCL 默认的坐标系
    viewer->addCoordinateSystem(200.0);

   
    // 添加几个坐标点
    pcl::PointXYZRGB point1(float(100), 100, 100); // 红色点
    pcl::PointXYZRGB point2(float(300), 255, 0); // 绿色点
    pcl::PointXYZRGB point3(float(200), 0, 255); // 蓝色点
	
	// 颜色空间范围为0-1,若使用RGB值,则使用R/255.0, G/255.0, B/255.0
    viewer->addSphere(point1, 20, 1.0, 0.0, 0.0, "point1");
    viewer->addSphere(point2, 20, 0.0, 1.0, 0.0, "point2");
    viewer->addSphere(point3, 20, 0.0, 0.0, 1.0, "point3");

    // 显示坐标系和坐标点
    while (!viewer->wasStopped()) {
        viewer->spinOnce();
    }

    return 0;
}
  • 22
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值