使用库:PCL
环境:乌班图18.04
语言:c++
程序如下:
#include <iostream>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/point_types.h>
#include <pcl/common/common.h>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <vector>
#include <pcl/visualization/pcl_visualizer.h>
#include <dirent.h>
#include <string>
#include <math.h>
#include <pcl/filters/random_sample.h>
void CreateCloudFromTxt(const std::string& file_path,pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
std::ifstream file(file_path.c_str());
std::string line;
std::string no_use;
pcl::PointXYZ point;
while (getline(file, line)) {
string::iterator it;
for (it = line.begin(); it < line.end(); it++)
{
if (*it == ',')
{
line.erase(it);
line.insert(it, ' ');
it--;
}
}
std::stringstream ss(line);
ss >> point.x;
ss >> point.y;
ss >> point.z;
cloud->push_back(point);
}
file.close();
}
void visualization(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("viewer"));
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 255, 0, 0);
viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color, "example");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "example");
while (!viewer->wasStopped()) {
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
int main(int argc, char *argv[]) {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_sample(new pcl::PointCloud<pcl::PointXYZ>);
CreateCloudFromTxt(argv[1],cloud_in);
pcl::RandomSample<pcl::PointXYZ> rs;
rs.setInputCloud(cloud);
//设置输出点的数量
rs.setSample(points_num);
//下采样并输出到cloud_sample
rs.filter(*cloud_sample);
visualization(cloud_sample);
}