点云处理:pcd格式转bin 【c++】

 main.cc 文件

#include <omp.h>
#include <ctime>
#include <vector>
#include <string>
#include <algorithm>

#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <iostream>
#include <fstream>

#include <pcl/io/pcd_io.h>
#include <pcl/common/common_headers.h>

#include <sys/stat.h>

#include <pcl/io/boost.h>
#include <boost/program_options.hpp>


void convert_pcd_to_bin(std::string path_input, std::string path_output)
{
    std::string datapath_in = path_input; 
    std::string datapath_out= path_output;
    
    std::vector<boost::filesystem::path> paths(boost::filesystem::directory_iterator{datapath_in}, boost::filesystem::directory_iterator{});

    sort(paths.begin(), paths.end());

    auto pcd_iter = paths.begin();

    int frames_all = paths.end() - paths.begin();

	pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);

    for (int i = 0; i < frames_all; i++) {
        pcd_iter++;
        std::string in_file = pcd_iter->string();

        if (pcl::io::loadPCDFile<pcl::PointXYZI>(in_file, *cloud) == -1) {
            std::string err = "Couldn't read file " + in_file;
            PCL_ERROR(err.c_str());
            return;// (-1);
        }

        std::cout << "Loaded "
            << cloud->width * cloud->height
            << " data points from "
            << in_file
            << " with the following fields: "
            << std::endl;

        std::string out_file = datapath_out + std::to_string(i) + ".bin";
        std::ofstream bin_file(out_file.c_str(), std::ios::out | std::ios::binary);

        for (int j = 0; j < cloud->size(); j++) {

            bin_file.write((char*)& cloud->at(j).x, sizeof(cloud->at(j).x));
            bin_file.write((char*)& cloud->at(j).y, sizeof(cloud->at(j).y));
            bin_file.write((char*)& cloud->at(j).z, sizeof(cloud->at(j).z));
            bin_file.write((char*)& cloud->at(j).intensity, sizeof(cloud->at(j).intensity));
        }
        bin_file.close();
    }

}

int main(int argc, char **argv)
{
    if (argc <= 1) {
        std::cout << "please input datapath dir and output datapath dir" << std::endl;
        std::cout << "for example, run ./pcd2bin /home/pcds/ /home/bins/" << std::endl;
        
        return 0;
    }

    std::string path_input = argv[1];
    std::string path_output= argv[2];

    std::fstream file_in;
    file_in.open(path_input, std::ios::in);
    if (!file_in) {
        std::cout << "can not find the input path:" << path_input << std::endl;
        return 0;
    }

    std::fstream file_out;
    file_out.open(path_output, std::ios::in);
    if (!file_out) {
        std::cout << "can not find the output path:" << path_output << std::endl;
        
        if (0 == mkdir(path_output.data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
            std::cout << "successfully creat output path: " << path_output << std::endl;
        
        } else {
            std::cout << "create output path failed!" << std::endl;
            return 0;
        }
        
    }
    
    convert_pcd_to_bin(path_input, path_output);

    return 0;
}

CMakeLists.txt 文件

cmake_minimum_required(VERSION 3.5)
project(pcd2bin)
 
find_package(PCL 1.2 REQUIRED)
 
# 加入Boost setting
find_package(Boost COMPONENTS program_options REQUIRED )
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
 
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
 
add_executable(pcd2bin main.cc)
 
target_link_libraries (pcd2bin ${PCL_LIBRARIES} ${Boost_LIBRARIES}) 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值