PCL(點雲庫)學習(一)
阿新 • • 發佈:2019-01-28
最近在學習SLAM(同時定位與地圖構建),因此順帶學習了一下,PCL(Point Cloud Library,點雲庫)。
使用的作業系統為ROS(機器人作業系統),由於ROS集成了opencv和pcl,所以直接運行了PCL的示例程式碼。
下面是執行步驟、示例程式碼以及執行結果:
執行步驟:
cd到catkin_ws/src目錄下
cd catkin_ws/src
建立package
catkin_create_pkg chapter6_tutorials pcl_conversions pcl_ros pcl_msgs sensor_msgs
cd到chapter6_tutorials目錄下
cd chapter6_tutorials
建立src目錄並cd到該目錄下
mkdir src
cd src
建立pcl_sample.cpp檔案,並輸入如下程式碼
gedit pcl_sample.cpp
#include<ros/ros.h> #include<pcl/point_cloud.h> #include<pcl_ros/point_cloud.h> #include<pcl_conversions/pcl_conversions.h> #include<sensor_msgs/PointCloud2.h> int main(int argc,char** argv) { ros::init(argc,argv,"pcl_sample"); ros::NodeHandle nh; ros::Publisher pcl_pub = nh.advertise<sensor_msgs::PointCloud2>("pcl_output",1); sensor_msgs::PointCloud2 output; pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); cloud->width = 100; cloud->height= 1; cloud->points.resize(cloud->width*cloud->height); pcl::toROSMsg(*cloud,output); pcl_pub.publish(output); ros::spinOnce(); return 0; }
開啟chapter6_tutorials目錄下的CMakelist.txt,並增加如下內容:
尋找系統裡面的PCL庫
find_package(PCL REQUIRED)
include_directories(include ${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS}
生成可執行檔案和連結相應的庫add_executable(pcl_sample src/pcl_sample.cpp)
target_link_libraries(pcl_sample ${catkin_LIBRARIES} ${PCL_LIBRARIES})到 catkin_ws目錄下,
執行catkin_make
開啟新的終端:
執行 roscore
註冊程式
source ./devel/setup.bash
執行節點
rosrun chapter6_tutorials pcl_sample
以上就是執行pcl的過程。