PCL xyz文件转成pcd文件

xyz文件转成pcd文件

有时候点云数据会存储到txt文件当中。

20.623 40.276 -1.999 -1031 127 141 154
20.362 40.375 -2.239 -941 130 141 159
20.36 40.376 -2.402 -1083 139 151 165
20.374 40.367 -2.405 -1122 131 147 163
20.372 40.366 -2.405 -1165 132 145 161
20.375 40.364 -2.404 -1036 133 149 165
20.358 40.371 -2.405 -1137 139 151 165
20.359 40.374 -2.404 -1086 139 151 165
.....

前三个字段为xyz,第四个字段为intensity,后三个字段为rgb。

xyz to pcd

#include <pcl/io/pcd_io.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
 
using namespace std;
using namespace pcl;
using namespace pcl::io;
using namespace pcl::console;
 
void printHelp (int, char **argv)
{
  print_error ("Syntax is: %s input.xyz output.pcd\n", argv[0]);
}
 
bool loadCloud (const string &filename, PointCloud<PointXYZRGB> &cloud)
{
  ifstream fs;
  fs.open (filename.c_str (), ios::binary);
  if (!fs.is_open () || fs.fail ())
  {
    PCL_ERROR ("Could not open file '%s'! Error : %s\n", filename.c_str (), strerror (errno)); 
    fs.close ();
    return (false);
  }
 
  string line;
  vector<string> st;
 
  while (!fs.eof ())
  {
    getline (fs, line);
    // Ignore empty lines
    if (line.empty())
      continue;
 
    // Tokenize the line
    boost::trim (line);
    boost::split (st, line, boost::is_any_of ("\t\r "), boost::token_compress_on);
 
    if (st.size () != 7)
      continue;
 
    pcl::PointXYZRGB point;
    point.x = float (atof (st[0].c_str ())); 
    point.y = float (atof (st[1].c_str ())); 
    point.z = float (atof (st[2].c_str ()));
    point.r = uint8_t (atof (st[4].c_str ()));
    point.g = uint8_t (atof (st[5].c_str ()));
    point.b = uint8_t (atof (st[6].c_str ()));
    cloud.push_back (point);
  }
  fs.close ();
 
  cloud.width = uint32_t (cloud.size ()); cloud.height = 1; cloud.is_dense = true;
  return (true);
}
 
int
main (int argc, char** argv)
{
  print_info ("Convert a simple XYZ file to PCD format. For more information, use: %s -h\n", argv[0]);
 
  if (argc < 3)
  {
    printHelp (argc, argv);
    return (-1);
  }
 
  // Parse the command line arguments for .pcd and .ply files
  vector<int> pcd_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
  vector<int> xyz_file_indices = parse_file_extension_argument (argc, argv, ".xyz");
  if (pcd_file_indices.size () != 1 || xyz_file_indices.size () != 1)
  {
    print_error ("Need one input XYZ file and one output PCD file.\n");
    return (-1);
  }
 
  // Load the first file
  PointCloud<PointXYZRGB> cloud;
  if (!loadCloud (argv[xyz_file_indices[0]], cloud)) 
    return (-1);
 
  // Convert to PCD and save
  pcl::io::savePCDFileASCII (argv[pcd_file_indices[0]], cloud);
}

CMakeList.txt

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
 
project(xyz2pcd)
 
find_package(PCL 1.9 REQUIRED)
 
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
 
add_executable (xyz2pcd xyz2pcd.cpp)
target_link_libraries (xyz2pcd ${PCL_LIBRARIES})

测试数据

smalldata.xyz

20.623 40.276 -1.999 -1031 127 141 154
20.362 40.375 -2.239 -941 130 141 159
20.36 40.376 -2.402 -1083 139 151 165
20.374 40.367 -2.405 -1122 131 147 163
20.372 40.366 -2.405 -1165 132 145 161
20.375 40.364 -2.404 -1036 133 149 165
20.358 40.371 -2.405 -1137 139 151 165
20.359 40.374 -2.404 -1086 139 151 165
20.354 40.375 -2.404 -1106 139 148 163
20.356 40.374 -2.404 -1059 139 151 165
20.359 40.374 -2.399 -1059 138 150 166
20.359 40.374 -2.395 -1014 138 150 166
20.354 40.375 -2.395 -1042 138 150 166
20.356 40.374 -2.396 -1050 138 150 166
20.354 40.374 -2.393 -1031 137 149 165
20.356 40.374 -2.393 -1045 137 149 165
20.359 40.374 -2.392 -957 137 149 165
20.374 40.365 -2.402 -1093 133 149 165
20.375 40.367 -2.401 -1096 133 149 165
20.374 40.367 -2.399 -1158 133 149 165
20.371 40.367 -2.399 -1093 133 146 162
20.365 40.371 -2.405 -1283 133 145 159
20.361 40.371 -2.404 -1211 133 145 159
20.362 40.372 -2.405 -1314 133 145 159
20.36 40.373 -2.405 -1213 139 151 165
20.361 40.375 -2.404 -1116 139 151 165
20.363 40.375 -2.402 -1063 133 145 159
20.367 40.371 -2.405 -1231 133 145 159
20.365 40.375 -2.405 -1311 133 145 159
20.367 40.372 -2.404 -1173 133 145 159

Build

cd xyz2pcd
mkdir build
cd build
 cmake .. -G "Visual Studio 14 Win64"  -DCMAKE_TOOLCHAIN_FILE=D:\vcpkg\scripts\buildsystems\vcpkg.cmake
 cmake --build .
 .\Debug\xyz2pcd.exe ..\smalldata.xyz ..\smalldata.pcd

文件组织:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OII5yKxf-1592804680988)(filetree.png)]

生成的PCD文件

# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z rgb
SIZE 4 4 4 4
TYPE F F F U
COUNT 1 1 1 1
WIDTH 30
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 30
DATA ascii
20.622999 40.276001 -1.999 4286549402
20.362 40.375 -2.2390001 4286746015
20.360001 40.375999 -2.402 4287338405
20.374001 40.367001 -2.405 4286813091
20.372 40.366001 -2.405 4286878113
20.375 40.363998 -2.404 4286944677
20.358 40.370998 -2.405 4287338405
20.358999 40.374001 -2.404 4287338405
20.354 40.375 -2.404 4287337635
20.356001 40.374001 -2.404 4287338405
20.358999 40.374001 -2.3989999 4287272614
20.358999 40.374001 -2.395 4287272614
20.354 40.375 -2.395 4287272614
20.356001 40.374001 -2.3959999 4287272614
20.354 40.374001 -2.3929999 4287206821
20.356001 40.374001 -2.3929999 4287206821
20.358999 40.374001 -2.392 4287206821
20.374001 40.365002 -2.402 4286944677
20.375 40.367001 -2.401 4286944677
20.374001 40.367001 -2.3989999 4286944677
20.371 40.367001 -2.3989999 4286943906
20.365 40.370998 -2.405 4286943647
20.361 40.370998 -2.404 4286943647
20.362 40.372002 -2.405 4286943647
20.360001 40.373001 -2.405 4287338405
20.361 40.375 -2.404 4287338405
20.363001 40.375 -2.402 4286943647
20.367001 40.370998 -2.405 4286943647
20.365 40.375 -2.405 4286943647
20.367001 40.372002 -2.404 4286943647

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值