簡體   English   中英

如何在 C++ 和 ROS 中正確讀取點雲文件

[英]How to properly read a Point Cloud File in C++ and ROS

我剛開始使用Point Cloud Library ,作為開始,我想從文件中讀取點雲。 我遵循了與此相關的 教程 這只是我正在構建的一個主要CMake項目的一個小例子。 與教程略有不同,我將項目划分為更適合CMake CMake運行良好,項目似乎井井有條。 但是,當我嘗試運行該項目時,我得到以下/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> >) ,我不知道如何解釋這個錯誤。

在我正在使用的代碼片段下方:

雲.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

雲.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})

前段時間我已經解決了我的問題,但想分享它,以防有人遇到同樣的問題。 所以同時出現了兩個問題,讓我認為這只是一個CMake問題:

1) catkin_make沒有正確編譯不是因為我想了很長時間的CMake ,而是因為緩存文件catkin_ws.workspaceCMake本身造成了問題。 所以這個問題的第一個解決方案是擦除緩存文件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 文件的完整代碼下方。 我希望這對可能遇到此問題的任何人都有幫助:

測試.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

pcl_read

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM