我正在开发一个使用点云库进行 3D 对象识别的系统。我想将 ESF 描述符应用于我本地存储的数据集。我将包含数据的文件夹作为参数提供给程序。我附上代码供参考。
#include <pcl/io/pcd_io.h>
#include <pcl/features/esf.h>
#include <pcl/visualization/histogram_visualizer.h>
#include <pcl/visualization/pcl_plotter.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/console/print.h>
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <flann/flann.h>
#include <fstream>
typedef std::pair<std::string, std::vector<float> > esf_model;
void calculateESFDescriptor(const boost::filesystem::path &path){
pcl::console::print_highlight ("Executing function: calculateESFDescriptor() \n");
// const boost::filesystem::path &path = base_dir/it->path().filename();
// Cloud for storing the object.
pcl::PointCloud<pcl::PointXYZ>::Ptr object(new pcl::PointCloud<pcl::PointXYZ>);
// Object for storing the ESF descriptor.
pcl::PointCloud<pcl::ESFSignature640>::Ptr descriptor(new pcl::PointCloud<pcl::ESFSignature640>);
// Note: you should have performed preprocessing to cluster out the object
// from the cloud, and save it to this individual file.
// Read a PCD file from disk.
if (pcl::io::loadPCDFile<pcl::PointXYZ>(path.string(), *object) != 0)
{
pcl::console::print_highlight ("Unable to read .pcd file from the directory\n");
// return -1;
}
// ESF estimation object.
pcl::ESFEstimation<pcl::PointXYZ, pcl::ESFSignature640> esf;
esf.setInputCloud(object);
esf.compute(*descriptor);
// Writing esf description file to disk
std::string output_file_name = path.string().substr(0, path.string().find_last_of('.')) + "_esf.pcd";
pcl::io::savePCDFileASCII (output_file_name, *descriptor);
std::cerr << "Saved data points to " << output_file_name << std::endl;
// return (0);
}
/** \brief Load a set of ESF features that will act as the model (training data)
* \param argc the number of arguments (pass from main ())
* \param argv the actual command line arguments (pass from main ())
* \param extension the file extension containing the ESF features
*/
void traverseDirectory(const boost::filesystem::path &base_dir, const std::string &extension){
pcl::console::print_highlight ("Executing function: traverseDirectory() \n");
if (!boost::filesystem::exists(base_dir) && !boost::filesystem::is_directory(base_dir)){
pcl::console::print_highlight ("Quitting as the directory does not exists.\n");
return;
}
int i = 0;
for (boost::filesystem::directory_iterator it (base_dir); it != boost::filesystem::directory_iterator (); ++it)
{
if (boost::filesystem::is_directory(it->status()))
{
std::stringstream ss;
ss << it->path ();
pcl::console::print_highlight ("Loading %s \n", ss.str().c_str());
traverseDirectory(it->path(), extension);
}
// Calculate ESF Descriptor for each file
if (boost::filesystem::is_regular_file(it->status()) && boost::filesystem::extension(it->path())==extension)
{
calculateESFDescriptor(base_dir/it->path().filename());
pcl::console::print_highlight ("Loop execution: %d\n", i++);
}
// continue;
}
}
int main(int argc, char** argv)
{
if (argc < 2)
{
PCL_ERROR ("Need at least two parameters! Syntax is: %s [model_directory] [options]\n", argv[0]);
return (-1);
}
std::string extension(".pcd");
transform(extension.begin(), extension.end(), extension.begin(), (int(*)(int))tolower);
// std::vector<esf_model> models;
// Traversing directory to find all .pcd files and applying esf descriptor to each file
traverseDirectory(argv[1], extension);
return (0);
}
问题是函数calculateESFDescriptor()只运行一次,然后程序关闭而没有任何错误,而我的目录中有多个.pcd文件,并且该函数应该对目录中的所有文件执行。我想计算所有文件的 ESF 描述符,以便稍后将它们与用户给定的输入进行比较,以计算准确性。 我无法调试出错的地方,因为程序无法对目录中的所有文件执行。当我注释掉以下行时,该程序会在整个目录中运行。
calculateESFDescriptor(base_dir/it->path().filename());
可以帮助根据此代码的输出绘制直方图吗?