因为项目需要,需要把点云和点云中的直线可视化出来
当然,本文不涉及如何提取点云中的直线,这个感兴趣的可以在网上自行查找哈~
直接贴代码吧
src/visual_pcd2.cpp
#include <pcl/registration/ia_ransac.h>//采样一致性
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/features/normal_3d.h>
#include <pcl/features/fpfh.h>
#include <pcl/search/kdtree.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/filters/voxel_grid.h>//
#include <pcl/filters/filter.h>//
#include <pcl/registration/icp.h>//icp配准
#include <pcl/visualization/pcl_visualizer.h>//可视化
#include <time.h>//时间
using namespace std;
typedef pcl::PointXYZ PointT; //改个别名,用起来方便,具体可百度typedef c++用法
typedef pcl::PointCloud<PointT> PointCloud;
int main()
{
//加载点云文件
PointCloud::Ptr cloud_src_o(new PointCloud);//原点云,待配准 //定义一个点云指针文件, 点云指针为cloud_src_o
pcl::io::loadPCDFile("/home/jy/Desktop/2011_09_30/2011_09_30_drive_0027_sync/velodyne_points/out_fliter_010.pcd", *cloud_src_o);//将读取的“”里的文件的内容,给了cloud_src_o所指向的地址,*给指针取内容
pcl::visualization::PCLVisualizer viewer("registration Viewer"); //定义一个显示器
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> src_h(cloud_src_o, 0, 200, 200); //给点云定义一个颜色 RGB模式 为全G 绿色
viewer.setBackgroundColor(255, 255, 255); //设置背景颜色 255,255,255就是白色
viewer.addPointCloud(cloud_src_o, src_h, "source cloud"); //把点云加入到显示器里
// string file_name = "/home/jy/catkin_ws/src/map_fusion/config/V1_01_easy/line_3d.txt";
string file_name ="/home/jy/Desktop/pcd2txt/build/line_07_flitered_010.txt";
string fline;
ifstream in(file_name);
if (!in.is_open())
{
cout << "can not open this file" << endl;
return 0;
}
int i = 1;
while(std::getline(in,fline))
{
std::istringstream iss(fline);
vector<float>line3d(6) ;
iss>>line3d[0]>>line3d[1]>>line3d[2]>>line3d[3]>>line3d[4]>>line3d[5];//存入7个值
PointT a,b;
a.x = line3d[0];
a.y = line3d[1];
a.z = line3d[2];
b.x = line3d[3];
b.y = line3d[4];
b.z = line3d[5];
string str = to_string(i);
viewer.addLine(a,b,255,0,0,"line"+str);//把直线加到可视化界面里
// cout << a.x << " " << a.y << " " << a.z << endl;
cout << i << endl;
if(i==10000) break;
i++;
}
// 等待直到可视化窗口关闭
while (!viewer.wasStopped())
{
viewer.spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return (0);
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
set(CMAKE_BUILD_TYPE "Release")
# 添加c++ 11标准支持
set(CMAKE_CXX_FLAGS "-std=c++11 -O2")
find_package(PCL REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_executable (visual_pcd2 src/visual_pcd2.cpp) #注意这里不能替换
target_link_libraries(visual_pcd2 ${PCL_LIBRARIES})
有问题欢迎在评论区留言~