【问题标题】:How to properly read a Point Cloud File in C++ and ROS如何在 C++ 和 ROS 中正确读取点云文件
【发布时间】:2019-10-23 09:04:47
【问题描述】:

我刚开始使用Point Cloud Library,首先我想从文件中读取点云。我关注了与此相关的tutorial。这只是我正在构建的主要CMake 项目的一个小例子。与教程略有不同,我将项目划分为更适合CMakeCMake 运行良好,项目似乎井井有条。但是,当我尝试运行该项目时,我得到以下/home/emanuele/catkin_ws/src/map_ros/src/pointcloud_reader_node.cpp:6:10: fatal error: ../map_ros/include/cloud.h: No such file or directory #include "../map_ros/include/cloud.h" error::Cloud::readPCloud(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) 并且我不知道如何解释这个错误。

在我正在使用的代码的 sn-p 下方:

cloud.h

#ifndef CLOUD_H
#define CLOUD_H

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <string>

class Cloud
{
public:
    void readPCloud(std::string filename);
private:
    std::string path;
};

#endif// CLOUD_H

cloud.cpp

#include "cloud.h"

void Cloud::readPCloud(std::string filename)
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    if(pcl::io::loadPCDFile<pcl::PointXYZ> (filename, *cloud) == -1) // load point cloud file
    {
        PCL_ERROR("Could not read the file");
        return;
    }
    std::cout<<"Loaded"<<cloud->width * cloud->height
             <<"data points from filename with the following fields: "
             <<std::endl;

    for(size_t i = 0; i < cloud->points.size(); ++i)
        std::cout << "    " << cloud->points[i].x
                  << " "    << cloud->points[i].y
                  << " "    << cloud->points[i].z << std::endl;
}

pointcloud_reader_node.cpp

#include <ros/ros.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include "../map_ros/include/cloud.h"

using namespace std;

int main()
{
    std::string fstring = "/home/to/Desktop/file.pcd";
    Cloud p;
    p.readPCloud(fstring); // <-- Error Here
    return 0;
}

为了完整起见,我在下面添加了CMake 文件:

cmake_minimum_required(VERSION 2.8.3)
project(map_ros)

add_compile_options(-std=c++11)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
find_package(catkin REQUIRED COMPONENTS
    // ....
    )

catkin_package(
  INCLUDE_DIRS include
  LIBRARIES ${PROJECT_NAME}
  CATKIN_DEPENDS
    // ......
)

###########
## Build ##
###########

include_directories(${catkin_INCLUDE_DIRS})
add_executable(pointcloud_reader_node src/pointcloud_reader_node.cpp ${SRCS})
target_link_libraries(pointcloud_reader_node ${catkin_LIBRARIES})

【问题讨论】:

  • @drescherjm,感谢您阅读问题。我发布了整个错误
  • ../map_ros/include/cloud.h 即使它说不包括在内,它也包括在内。所以我不知道为什么会收到error::Cloud::readPCloud(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;) 错误
  • 我认为这意味着您的文件夹结构不是项目所期望的。
  • pointcloud_reader_node.cpp文件夹..map_ros而不是包含map_ros的文件夹
  • #include "../map_ros/include/cloud.h" 可能需要是#include "../include/cloud.h"

标签: c++ c++11 cmake point-cloud-library point-clouds


【解决方案1】:

前段时间我发现了我的问题的问题,但想分享它以防有人遇到我同样的问题。 所以同时出现了两个问题,让我认为这只是CMake 问题:

1) catkin_make 没有正确编译不是因为CMake 我想了很长时间,而是因为缓存文件catkin_ws.workspace 导致CMake 本身出现问题。所以这个问题的第一个解决方案是擦除缓存文件catkin_ws.workspace 并重新编译。所有CMake 问题都消失了。

2)第二个问题:读取point-cloud的正确伪代码是这样的:

main()
{
    init node
    create pointcloud publisher
    create rate object with 1 second duration
    load point cloud from file
    while(ros::ok())
    {
        rate.sleep

        publish point cloud message
    }
}

我意识到输入上没有发布任何内容,并且执行了回调。 在从文件中读取point-cloud 并将所有点的输出提供给 .txt 文件的完整代码下方。我希望这对可能遇到此问题的任何人都有帮助:

test.cpp

#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl/io/pcd_io.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>

void loadFromFile(std::string filename)
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    if(pcl::io::loadPCDFile<pcl::PointXYZ> (filename, *cloud) == -1) // load point cloud file
    {
        PCL_ERROR("Could not read the file");
        return;
    }
    std::cout<<"Loaded"<<cloud->width * cloud->height
             <<"data points from /home/to/Desktop/point_cloud/yourFile.pcd with the following fields: "
             <<std::endl;

// Write entire point clouds to a .txt file
        std::ofstream myfile;
        myfile.open ("/home/to/Desktop/exampleCloud.txt");
        if (myfile.is_open()) {
            for(size_t i = 0; i <   cloud->points.size(); ++i)
                   myfile << " " << cloud->points[i].x
                          << " " << cloud->points[i].y
                          << " " << cloud->points[i].z << std::endl;
            myfile.close();
    }
}

int main (int argc, char** argv)
{
    // Initialize ROS
    ros::init (argc, argv, "pcl_tutorial_cloud");
    ros::NodeHandle nh;
    ros::Publisher pub = nh.advertise<sensor_msgs::PointCloud2>("output", 1000);
    ros::Rate loop_rate(1);
    loadFromFile("/home/to/Desktop/yourFile.pcd");
    int count = 0;
    while(ros::ok())
    {
        sensor_msgs::PointCloud2 pcloud2;
        pub.publish(pcloud2);
        ros::spinOnce();
        loop_rate.sleep();
        count ++;
    }
    return 0;
}

这是运行后的结果:

1) catkin_make

2)rosrun yourProject test

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2023-01-09
    • 1970-01-01
    相关资源
    最近更新 更多