《点云库PCL从入门到精通》这本书中,虽然在第二章介绍了在 windows 和 linux 上的安装方法,但是书里面的程序都是在 windows 上运行的,所以写一下如何在 linux 中运行书中程序 ,书中程序 github 下载地址:链接 系统:Ubuntu 16.04,PCL 1.8.1 以第二章例1的程序为例,另外书中的滤波测试程序好像有点问题,我在本文最后附上了我所使用的 testfilter.cpp 和相应的 CMakeLists.txt 文件(也就是在 src 里面的两个文件)
如下图所示,程序中的文件夹命名为 source,先将其改名为 src 然后在此文件夹下打开终端,并在终端中输入:catkin_make ,会生成 build 和 devel 文件夹 编译完之后如下图,可以看到在 build 文件夹中创建了 testfilter 可执行文件 然后还是在 src 所在文件夹,输入:./build/testfilter,这一步就是执行生成的可执行文件,如下图 注意:如果执行的 .cpp 文件中有需要导入的文件,那么需要将文件复制到与 src 同一文件夹,例如第10章例1,这样就不需要修改路径了,否则还需要在 cpp 文件的导入语句中加上文件路径。
testfilter.cpp
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/project_inliers.h>
int main ( int argc, char * * argv)
{
pcl:: PointCloud< pcl:: PointXYZ> :: Ptr cloud ( new pcl:: PointCloud< pcl:: PointXYZ> ) ;
pcl:: PointCloud< pcl:: PointXYZ> :: Ptr cloud_projected ( new pcl:: PointCloud< pcl:: PointXYZ> ) ;
cloud- > width = 5 ;
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 ) ;
}
std:: cerr << "Cloud before projection: " << std:: endl;
for ( size_t i = 0 ; i < cloud- > points. size ( ) ; ++ i)
std:: cerr << " " << cloud- > points[ i] . x << " "
<< cloud- > points[ i] . y << " "
<< cloud- > points[ i] . z << std:: endl;
pcl:: ModelCoefficients:: Ptr coefficients ( new pcl:: ModelCoefficients ( ) ) ;
coefficients- > values. resize ( 4 ) ;
coefficients- > values[ 0 ] = coefficients- > values[ 1 ] = 0 ;
coefficients- > values[ 2 ] = 1.0 ;
coefficients- > values[ 3 ] = 0 ;
pcl:: ProjectInliers< pcl:: PointXYZ> proj;
proj. setModelType ( pcl:: SACMODEL_PLANE) ;
proj. setInputCloud ( cloud) ;
proj. setModelCoefficients ( coefficients) ;
proj. filter ( * cloud_projected) ;
std:: cerr << "Cloud after projection: " << std:: endl;
for ( size_t i = 0 ; i < cloud_projected- > points. size ( ) ; ++ i)
std:: cerr << " " << cloud_projected- > points[ i] . x << " "
<< cloud_projected- > points[ i] . y << " "
<< cloud_projected- > points[ i] . z << std:: endl;
system ( "pause" ) ;
return ( 0 ) ;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(testfilter)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (testfilter testfilter.cpp)
target_link_libraries (testfilter ${PCL_LIBRARIES})