前言
代码是网上找的(基于ubuntu的),我改成了windows可用的,更改输入输出目录后,可直接用在kitti的点云文件上,将bin格式转成pcd格式。 需要用到pcl库。
运行环境
window10 +vs2019 debug x64
一、代码
#include <ctime>
#include <string>
#include <vector>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <iostream>
#include <fstream>
#include <io.h>
using namespace std;
static std::vector<std::string> file_lists;
bool computePairNum(std::string pair1, std::string pair2)
{
return pair1 < pair2;
}
void sort_filelists(std::vector<std::string>& filists, std::string type)
{
if (filists.empty())return;
std::sort(filists.begin(), filists.end(), computePairNum);
}
void readKittiPclBinData(std::string& in_file, std::string& out_file)
{
// load point cloud
std::fstream input(in_file.c_str(), std::ios::in | std::ios::binary);
if (!input.good()) {
std::cerr << "Could not read file: " << in_file << std::endl;
exit(EXIT_FAILURE);
}
input.seekg(0, std::ios::beg);
pcl::PointCloud<pcl::PointXYZI>::Ptr points(new pcl::PointCloud<pcl::PointXYZI>);
int i;
for (i = 0; input.good() && !input.eof(); i++) {
pcl::PointXYZI point;
input.read((char*)&point.x, 3 * sizeof(float));
input.read((char*)&point.intensity, sizeof(float));
points->push_back(point);
}
input.close();
std::cout << "Read KTTI point cloud with " << i << " points, writing to " << out_file << std::endl;
pcl::PCDWriter writer;
// Save DoN features
writer.write< pcl::PointXYZI >(out_file, *points);
//pcl::io::savePCDFileASCII(out_file, *points);
}
//获取所有的文件名
void GetAllFiles(string path, vector<string>& files)
{
//long hFile = 0;
intptr_t hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
GetAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
//获取特定格式的文件名
void GetAllFormatFiles(string path, vector<string>& files, string format)
{
//文件句柄
//long hFile = 0;
intptr_t hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
//files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
GetAllFormatFiles(p.assign(path).append("\\").append(fileinfo.name), files, format);
}
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
int main(int argc, char** argv)
{
std::string bin_path = "E:\\kitti\\data_object_velodyne\\testing\\velodyne";
std::string pcd_path = "E:\\kitti\\data_object_velodyne\\testing\\pcd";
GetAllFormatFiles(bin_path, file_lists, "bin");
sort_filelists(file_lists, "bin");
for (int i = 0; i < file_lists.size(); ++i)
{
std::string bin_file = file_lists[i];
int pos = bin_file.rfind("\\");
std::string file_name_bin = bin_file.substr(pos, 11);
std::string file_name = file_name_bin.substr(0, 7);
std::string pcd_file = pcd_path+ file_name+ ".pcd";
readKittiPclBinData(bin_file, pcd_file);
}
return 0;
}
二、问题及解决方案
1.使用pcdWriter的时候报错,link2019,无法解析的外部命令。
原因是没有包含pcl的lib库,将这两个库(pcl_iod.ilb和pcl_commond.lib)加入到链接器的输入中(debug模式)
如果是release的,就将pcl_io.ilb和pcl_common.lib加入到链接器的输入中
这些文件都在pcl的安装目录lib下面
链接器常规中的附加库目录记得设置一下
2.生成成功,但是运行的时候显示没有OpenNI2.dll
解决办法:在pcl的目录下找到这个OpenNI2.dll,复制粘贴到.exe的路径下即可。