pybind11封装pcl-io函数,读取PCD文件

使用python读取pcd

使用pybind11封装pcl的io接口

1、编译

interface.h

#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include "point_utils.h"
namespace py = pybind11;
py::array_t<float> LoadPCDWithLabel(const std::string& pcd_file);

interface.cpp

#include "interface.h"
#include <iostream>
// #include "interface_read_pcd.h"
// using suyunzheng_pcd_io;
// PYBIND11_MODULE(interface, m) {
//     // optional module docstring
//     m.doc() = "pcd io interface";
//     // expose add function, and add keyword arguments and default arguments
//     m.def("LoadPCDWithLabel", &LoadPCDWithLabel, "A function which load pcd xyz rgb ilf", py::arg("pcd_file")="test.pcd" );
// }
/*
*   读取pcd文件,x y z r g b intensity label frameindex
*/
py::array_t<float> LoadPCDWithLabel(const std::string& pcd_file){
    adt::hdmap::CloudXYZRGBILFPtr cloud_ptr(new adt::hdmap::CloudXYZRGBILF);
    
    if(pcl::io::loadPCDFile(pcd_file, *cloud_ptr)!=0){
        std::cerr << "***Can't load pcd file from: " << pcd_file << std::endl;
        py::array_t<float> _points = py::array_t<float>(8192*7);
        _points.resize({ssize_t(8192), ssize_t(7)});
        return _points;
    }
    float _point_num = cloud_ptr->size();
    py::array_t<float> _points = py::array_t<float>(_point_num * 7);
    _points.resize({ssize_t(_point_num), ssize_t(7)});
    auto mat = _points.mutable_unchecked<2>();
    for(int i = 0; i<cloud_ptr->size(); i++){
        mat(i, 0) = cloud_ptr->points[i].x;
        mat(i, 1) = cloud_ptr->points[i].y;
        mat(i, 2) = cloud_ptr->points[i].z;
        mat(i, 3) = static_cast<float>(cloud_ptr->points[i].r);
        mat(i, 4) = static_cast<float>(cloud_ptr->points[i].g);
        mat(i, 5) = static_cast<float>(cloud_ptr->points[i].b);
        mat(i, 6) = cloud_ptr->points[i].label;
    }
    // std::cerr << "points num: " << cloud_ptr->size() << std::endl;
    // std::cerr << "***Load succeed " << pcd_file << std::endl;
    return _points;
}

pcl_io_wrap.cpp

#include <iostream>
#include "interface.h"
// using suyunzheng_pcd_io;
PYBIND11_MODULE(interface, m) {
    // optional module docstring
    m.doc() = "pcd io interface";
    // expose add function, and add keyword arguments and default arguments
    m.def("LoadPCDWithLabel", &LoadPCDWithLabel, "A function which load pcd xyz rgb ilf", py::arg("pcd_file")="test.pcd" );
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)
project(interface)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(/usr/include/pcl-1.7  )
file(GLOB PCL_LIBRARIES "/usr/lib/x86_64-linux-gnu/libpcl*.so*")
message("\n---------------------")
message(${PCL_LIBRARIES})
message("---------------------\n")
find_package(Boost REQUIRED filesystem thread)
include_directories(${Boost_INCLUDE_DIRS})
message("\n------------------")
message(${Boost_LIBRARIES})
message("------------------\n")
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
# find_package(yaml-cpp REQUIRED)
include_directories("/usr/local/include/yaml-cpp")
file(GLOB YAML_CPP_LIBRARIES "/usr/local/lib/libyaml-cpp.so")
message("\n------------------")
message(${YAML_CPP_LIBRARIES})
message("------------------\n")
# set(COMMON_INCLUDE "./common/include")
# set(COMMON_SRC "./common/src")
# include_directories(
#     ${COMMON_INCLUDE}
#     ./include/
# )
set(COMMON_INCLUDE "common/include")
set(COMMON_SRC "common/src")
include_directories(
    ${COMMON_INCLUDE}
    ./include/
)
find_package(pybind11 REQUIRED)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
# pybind11_add_module(example example.cpp)
# 名字要对应 pybind11_add_module
# pybind11_add_module(interface 
#                     ${COMMON_SRC}/eigen_utils.cpp ${COMMON_SRC}/point_utils.cpp ${COMMON_SRC}/system_utils.cpp
#                     src/pcd_io_wrap.cpp
#                      src/interface.cpp  
#                     )
pybind11_add_module(interface 
                    src/pcd_io_wrap.cpp
                     src/interface.cpp  
                    ${COMMON_SRC}/eigen_utils.cpp ${COMMON_SRC}/point_utils.cpp ${COMMON_SRC}/system_utils.cpp)
target_link_libraries(interface PRIVATE                   
                       ${PCL_LIBRARIES}
                       ${Boost_LIBRARIES}
                       ${YAML_CPP_LIBRARIES}
                    #    ${OpenCV_LIBS}
                       )
# cmake_minimum_required(VERSION 2.8.12)
# project(example)
# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
# find_package(pybind11 REQUIRED)
# # add_subdirectory(pybind11)
# pybind11_add_module(example example.cpp)

2、使用

import sys
# sys.path.append("./../lib")
sys.path.append("interface/")
from os.path import join
import numpy as np
import random, os, sys
import os, argparse, pickle
# import lib.interface as interface
import lib.interface as interface
from pypcd import pypcd
# print("pypcd.version:{}".format(pypcd.__version__))
# cloud  = example.add(1,3)
file_path = "/media/s/zhouzixiang@baidu.com/luweixin/L3_New_City_32E/69585/69585_2_250.pcd"
cloud = interface.LoadPCDWithLabel(file_path)
print("***{}".format(cloud.shape))
print(cloud)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值