一 ros的rviz能夠播放bag
1.運行ros: $ roscore
2.運行rviz: $ rosrun rviz rviz
3.運行rosbag: $ rosbag play XXX.bag
rviz需要添加PointCloud2,然后再PointCloud2的屬性里面的Topic選擇對應的topic。
二 bag文件轉PCD文件
參考:http://wiki.ros.org/pcl_ros
方法一:bag_to_pcd
$ rosrun pcl_ros bag_to_pcd <input_file.bag> <topic> <output_directory>
example:
$ rosrun pcl_ros bag_to_pcd data.bag /velodyne_points ./pcd
方法二:pointcloud_to_pcd
一個終端通過ros發送messages,如:$ rosbag play XXX.bag
另一個終端接收,如:$ rosrun pcl_ros pointcloud_to_pcd input:=/velodyne_points
ps:還不清楚為啥方法一生成出來的pcd有點問題,雖然后面看的效果都一樣
三 基於PCL的PCD文件的讀取與顯示
環境:Ubuntu14.04
PCL通過apt-get安裝
代碼:
參考:https://www.douban.com/group/topic/43580599/?type=like
這段代碼的作用是從@訓文 提供的PCD文件讀取點雲數據,並用PCL的可視化模塊PCLVisualizer顯示出來。
PCD文件由文件頭和數據域組成,文件頭存儲了點雲數據的字段格式信息,如:
========================================
# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z rgba
SIZE 4 4 4 4
TYPE F F F U
COUNT 1 1 1 1
WIDTH 640
HEIGHT 480
VIEWPOINT 0 0 0 0 1 0 0
POINTS 307200
DATA binary
//下面是點雲數據段
…
========================================
通過上面信息可以知道,這個pcd文件存儲了每個點的x,y,z坐標以及r,g,b,a顏色信息,共有640*480個數據點,以及它是一個二進制文件。讀取pcd時調用loadPCDFile就會自動判斷文件的格式類型,我們只需用相應的點雲類型進行實例化。點雲的類型由pcl/point_types.h這個頭文件聲明。根據文件信息,我們使用了pcl::PointXYZRGBA導入點雲數據。
讀入數據后,可以遍歷點雲數據,並訪問各個域上的值。
顯示使用的是PCL的PCLVisualizer。addCoordinateSystem函數可以加入攝像頭的坐標系統(其中紅綠藍分別代表x、y、z軸,對應了正右、正下、正前方)。setCameraPosition函數設定窗口的視角,前三個是視點坐標(x,y,z),設置為(0,0,-3.0)說明當前視點是攝像頭的后面3米。后三個是視點的向上方向,(0,-1,0)表明以y軸負方向為正上方。
我們可以通過旋轉縮放等等操作更好地觀察輸出窗口,按"h"可以查看對應的功能。
代碼:
#include <iostream>
#include <string>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
using namespace std;
int main (int argc, char** argv){
typedef pcl::PointXYZRGBA PointT;
pcl::PointCloud<PointT>::Ptr cloud (new pcl::PointCloud<PointT>);
std::string dir = "E:\\PCL\\data\\";
std::string filename = "100.pcd";
if (pcl::io::loadPCDFile<PointT> ((dir+filename), *cloud) == -1){
//* load the file
PCL_ERROR ("Couldn't read PCD file \n");
return (-1);
}
printf("Loaded %d data points from PCD\n",
cloud->width * cloud->height);
for (size_t i = 0; i < cloud->points.size (); i+=10000)
printf("%8.3f %8.3f %8.3f %5d %5d %5d %5d\n",
cloud->points[i].x,
cloud->points[i].y,
cloud->points[i].z,
cloud->points[i].r,
cloud->points[i].g,
cloud->points[i].b,
cloud->points[i].a
);
pcl::visualization::PCLVisualizer viewer("Cloud viewer");
viewer.setCameraPosition(0,0,-3.0,0,-1,0);
viewer.addCoordinateSystem(0.3);
viewer.addPointCloud(cloud);
while(!viewer.wasStopped())
viewer.spinOnce(100);
return (0);
}這個就是機器人觀察到的世界。
此時需要CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3) project(test) find_package(PCL REQUIRED) #配置PCL庫 include_directories(${PCL_INCLUDE_DIRS}) #配置頭文件路徑..例#include add_executable(test test.cpp) target_link_libraries(test ${PCL_LIBRARIES}) #鏈接PCL庫 set( CMAKE_BUILD_TYPE Debug ) #如果需要調試就加上這行
參考自:http://blog.sina.com.cn/s/blog_c263b1860102vylc.html
http://pointclouds.org/documentation/tutorials/project_inliers.php