我该如何管理记忆?点云库

时间:2018-04-05 11:03:09

标签: c++ ros point-cloud-library

我正在编写一个回调函数,它看起来像这样。

typedef pcl::PointXYZ PointT;

void velocity_callback(const geometry_msgs::TwistPtr cmd_vel)
{
    ros::Time now = ros::Time::now();
    laser_assembler::AssembleScans2 srv;
    srv.request.end = now;
    srv.request.begin = now - ros::Duration(0.11);
    sensor_msgs::PointCloud2 vertical_pc;
    vertical_pc = srv.response.cloud;

    pcl::PointCloud<PointT>::Ptr cloud(new pcl::PointCloud<PointT>);
    pcl::fromROSMsg(vertical_pc, *cloud);

    // Ground filter
    pcl::PassThrough<PointT> ground_filter;
    ground_filter.setNegative(true);
    ground_filter.setInputCloud (cloud);
    ground_filter.setFilterFieldName ("z");
    ground_filter.setFilterLimits (-0.7 , -0.5);
    // Filtered patient Cloud
    pcl::PointCloud<PointT>::Ptr filtered_ground_cloud(new pcl::PointCloud<PointT>);
    ground_filter.filter (*filtered_ground_cloud);

      //******* Bounding box for object
    pcl::CropBox<pcl::PointXYZ> boxFilter;
    boxFilter.setNegative(true);
    boxFilter.setMin(Eigen::Vector4f(-1.0, -0.5, -0.5, 1.0));
    boxFilter.setMax(Eigen::Vector4f( 0.0,  0.5, 0.5, 1.0));
    boxFilter.setInputCloud(filtered_ground_cloud);
    // Filtered Cloud
    pcl::PointCloud<PointT>::Ptr filtered_cloud(new pcl::PointCloud<PointT>);
    boxFilter.filter (*filtered_cloud);
}

我有vertical_pc,cloud,filtered_ground_cloud,filtered_cloud位于内存中。在我的情况下,我只需要最后的&#34; filtered_cloud。&#34;如何在每次过滤后删除点云数据。

是否可以做这样的事情

ground_filter.setInputCloud (cloud);
ground_filter.filter (*cloud);

pcl::PointCloud<PointT>::Ptr创建boost::shared_ptr

由于

1 个答案:

答案 0 :(得分:1)

您可以使用pcl::PointCloud<PointT>::clear方法。例如vertical_pc->clear。但是,clear似乎并没有真正释放内存。如果使用pcl::PointCloud<PointT>::Ptr::reset函数,则实际释放内存。参见示例:

#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <iostream>

int main(int argc, char** argv)
{
    int n = 100000000;
    std::cout<<"Creating "<<n<<" points"<<std::endl;
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    cloud->resize(n);
    std::cout<<"\n";
    std::cout<<"Cloud size: "<<cloud->size()<<std::endl;
    cloud->clear();
    std::cout<<"Cloud cleared!"<<std::endl;
    sleep(5);
    cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
    std::cout<<"Cloud swapped!"<<std::endl;
    sleep(5);
}

我在Ubuntu 14.04中尝试过并使用htop可视化我程序的内存使用情况。

此外,您可以使用相同的点云作为过滤方法的输入和输出。见这个例子:

#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>

int main(int argc, char** argv)
{    
    int n(1000);
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    cloud->resize(n);
    double max(10);
    double min(-10);
    for (int i = 0;i<cloud->size();i++)
    {
        std::cout<<i<<"/"<<cloud->size()<<"\r";
        cloud->points[i].x = (max-min) * rand () / (RAND_MAX + 1.0f) + min;
        cloud->points[i].y = (max-min) * rand () / (RAND_MAX + 1.0f) + min;
        cloud->points[i].z = (max-min) * rand () / (RAND_MAX + 1.0f) + min;
        std::cout<<i<<"/"<<cloud->size()<<"\r";
    }
    // Display cloud
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer);
    viewer->addCoordinateSystem(2);
    viewer->addPointCloud(cloud,"original");
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,1,1,1,"original");
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,1,"original");

    // Create the filtering object
    pcl::PassThrough<pcl::PointXYZ> pass;
    pass.setInputCloud (cloud);
    pass.setFilterFieldName ("z");
    pass.setFilterLimits (0.0, max);
    std::cout<<"Cloud size: "<<cloud->size()<<std::endl;
    pass.filter (*cloud);
    std::cout<<"Cloud size: "<<cloud->size()<<std::endl;
    viewer->addPointCloud(cloud,"filtered");
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,1,0,0,"filtered");
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,2,"filtered");

    // Display
    viewer->spin();
}