在这里夸夸我亲爱的小伙伴,帮助我找到了路径问题!!!
最近遇到个世界难题,用MULLS程序自带的txt2pcd代码运行出来原本五万个点却只有四万个点,打开后点数量还不一样了
在这里附上源程序的代码,我觉得很简短,不知道是不是做了滤波什么的,但是形状数量都改变了,很奇怪
#include "dataio.hpp"
#include "utility.hpp"
#include <iostream>
#include <fstream>
using namespace std;
using namespace lo;
// Brief: Do the batch txt2pcd transformation
int main(int argc, char **argv)
{
// The file to read from.
string infile = argv[1];
// The file to output to.
string outfile = argv[2];
std::cout << "Transform begin" << std::endl;
// Load point cloud
fstream input(infile.c_str(), ios::in | ios::binary);
if (!input.good())
{
cerr << "Could not read file: " << infile << endl;
exit(EXIT_FAILURE);
}
input.seekg(0, ios::beg);
pcTPtr pointCloud(new pcT);
DataIo<Point_T> dataio;
dataio.read_txt_file(infile, pointCloud);
cout << "Read txt file from [" << infile << "]: " << pointCloud->points.size() << " points, writing to [" << outfile << "]" << endl;
dataio.write_pcd_file(outfile, pointCloud);
std::cout << "Transform done" << std::endl;
}
我觉得他的批量处理代码很好,需要写一个sh文件
.sh文件内容如下
#!/bin/sh
infolder=demo_data
mkdir ${infolder}/pcd
for file in ${infolder}/txt/*.txt
do
./bin/txt2pcd $file ${infolder}/pcd/`basename $file .txt`.pcd
done
于是,我使用pcl方法修改了.cpp文件,一定注意路径!!!
运行的时候一定在数据所在的位置输入build/XXX
如果你直接在生成的结果里./XXX 会报错!!!
感谢我亲爱的小伙伴找到了我的问题,代码没问题,路径和最后运行的文件夹出现了错误!!!!
点数与txt文件对应正确
改成批量处理的代码如下
#include <iostream>
#include <fstream>
using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <Eigen/Geometry>
#include <boost/format.hpp> // for formating strings
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
int main( int argc, char** argv )
{
// The file to read from.
string infile = argv[1];
// The file to output to.
string outfile = argv[2];
std::cout << "Transform begin" << std::endl;
// Load point cloud
fstream input(infile.c_str(), ios::in | ios::binary);
if (!input.good())
{
cerr << "Could not read file: " << infile << endl;
exit(EXIT_FAILURE);
}
pcl::PointCloud<pcl::PointXYZ> cloud;
pcl::PCDWriter writer;
pcl::PointXYZ pclPnt;
while(!input.eof())
{
input>>pclPnt.x>>pclPnt.y>>pclPnt.z;
cloud.push_back(pclPnt);
}
input.close();
writer.write(outfile,cloud);
cout << "Read txt file from [" << infile << "]: " << cloud.points.size() << " points, writing to [" << outfile << "]" << endl;
std::cout << "Transform done" << std::endl;
}
sh 文件不变!!!成功运行!!!
要点:最后运行成果文件的时候最好在根目录下进行,不然会因为找不到输入文件报错!!!