最近在做pcl库相关的东西,自己利用pcl库做项目的时候发现代码运行的特别慢,经过排查,可以有两种解决方案。
第一种读取二进制pcd文件的效率会比ascii格式的pcd文件效率高很多。
第二种是从debug模式切换到release模式,release模式的运行效率也比debug模式的运行效率高出30-40倍。
以下是我运行的代码。利用统计滤波对pcd文件进行滤波。
代码:
#include "mainwindow.h"
#include <QApplication>
#include <QDir>
#include <QTime>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/statistical_outlier_removal.h>
int main(int argc, char *argv[])
{
// QApplication a(argc, argv);
// QDir::setCurrent(a.applicationDirPath());
// MainWindow w;
// w.show();
// return a.exec();
QTime time;
time.start();
pcl::PCDReader reader;//pcd文件读取对象
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);//输入点云对象指针
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);//滤波点云对象指针
//pcl::visualization::CloudViewer viewer("input cloud viewer");//点云显示对象
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor; //创建滤波对象
if(reader.read("./pcd/defects.pcd",*cloud) == -1)
{
qDebug() << "pcd read failed";
return -1;
}
else
{
std::cerr << "cloud filtering before:" << std::endl;
std::cerr << *cloud << std::endl;
//viewer.showCloud(cloud,"cloud");//可视化输入点云
//统计滤波
sor.setInputCloud(cloud);//设置输入点云
sor.setMeanK(35);//每个点要分析的邻近点数为35个
sor.setStddevMulThresh(1.8);//标准差乘数设置为1,距离阈值 = mean + stddev_mult * stddev
sor.filter(*cloud_filtered);
std::cerr <<"cloud after filtering:" << std::endl;
std::cerr << *cloud_filtered;
pcl::PCDWriter writer;
writer.write("./pcd/defects_inliers.pcd",*cloud_filtered,false);//保存滤波之后的点
sor.setNegative(true);
sor.filter(*cloud_filtered);
writer.write("./pcd/defects_outliers.pcd",*cloud_filtered,false);
//viewer.showCloud(cloud_filtered);
qDebug() << time.elapsed()/1000 << "s";
// while(!viewer.wasStopped())
// {
// }
}
return 0;
}
debug模式运行时间:为259s。
release模式运行时间:为7s。
这里我更改了运行模式,然后运行时间就提高了30多倍。