PCL 三维点云轮廓提取
建一个pclfive文件夹,建一个pclfive.cpp文档如下:
#include <iostream>
#include <pcl/range_image/range_image.h>//深度图有关文件
#include <pcl/visualization/pcl_visualizer.h>//点云可视化头文件
#include <pcl/io/pcd_io.h>//pcd文件输入/输出
#include <pcl/visualization/range_image_visualizer.h>//rangeImage可视化头文件
#include <pcl/common/common_headers.h>
#include <pcl/console/parse.h>//命令行参数解析
#include <boost/thread/thread.hpp>//多线程文件
#include <pcl/visualization/common/float_image_utils.h>//保存深度图像
#include <pcl/io/png_io.h>//保存深度图像
int main (int argc, char** argv) {
//读入点云数据
// pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>);
// pcl::io::loadPCDFile("room_scan1.pcd",*cloud_in);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>);
//生成数据
for (float y=-0.5f; y<=0.5f; y+=0.01f) {
for (float z=-0.5f; z<=0.5f; z+=0.01f) {
pcl::PointXYZ point;
point.x = 2.0f - y;
point.y = y;
point.z = z;
cloud_in->points.push_back(point);
}
}
cloud_in->width = (uint32_t) cloud_in->points.size();
cloud_in->height = 1;
//以1度为角分辨率,从上面创建的点云创建深度图像。
float angularResolution = (float) ( 1.0f * (M_PI/180.0f));
// 1度转弧度
float maxAngleWidth = (float) (360.0f * (M_PI/180.0f));
// 360.0度转弧度
float maxAngleHeight = (float) (180.0f * (M_PI/180.0f));
// 180.0度转弧度
Eigen::Affine3f sensorPose = (Eigen::Affine3f)Eigen::Translation3f(0.0f, 0.0f, 0.0f);
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
float noiseLevel=0.00;
float minRange = 0.0f;
int borderSize = 1;
pcl::RangeImage rangeImage;
rangeImage.createFromPointCloud(*cloud_in, angularResolution, maxAngleWidth, maxAngleHeight, sensorPose, coordinate_frame, noiseLevel, minRange, borderSize);
std::cout << rangeImage << "\n";//重载运算符<<
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("CloudPointView"));
viewer->initCameraParameters();
int v1(0);
//viewer->createViewPort(0,0,0.5,1,v1);
viewer->setBackgroundColor(255,255,255,v1);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color1(cloud_in->makeShared(),255,0,0);
viewer->addPointCloud(cloud_in->makeShared(),color1,"pointCloud",v1);
viewer->addCoordinateSystem();
//viewer->spin();//这句话不注释掉会导致只显示点云图而不显示深度图的窗口
//save depth picture
float *ranges = rangeImage.getRangesArray();
unsigned char* rgb_image = pcl::visualization::FloatImageUtils::getVisualImage(ranges,rangeImage.width,rangeImage.height);
pcl::io::saveRgbPNGFile("saveRangeImageRGB.png",rgb_image,rangeImage.width,rangeImage.height);
std::cerr << "Picture Saved!" << std::endl;
//可视化深度图像rangeImage
pcl::visualization::RangeImageVisualizer range_image_widget("Range image");//创建Range image显示的对象
range_image_widget.setWindowTitle("RangeImage");
range_image_widget.showRangeImage(rangeImage);
while (!range_image_widget.wasStopped ()&& !viewer->wasStopped())
{
range_image_widget.spinOnce ();
viewer->spinOnce();
pcl_sleep (0.01);
}
return 0 ;
}
在同文件夹下写入配置文件:CMakeLists.txt,如下:
cmake_minimum_required(VERSION 2.6)
project(pclfive)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(pclfive pclfive.cpp)
target_link_libraries (pclfive ${PCL_LIBRARIES})
install(TARGETS pclfive RUNTIME DESTINATION bin)
在同文件夹下建立build建一个build文件夹。
开始编译:
1、在build文件夹中打开终端(在文件夹下右键+T)
2、终端输入
cmake ..
3、终端输入
make
4、终端输入
./pclfive