Range_Image深度图-2 PCL学习记录-10 Range_Image深度图(提取边缘信息并显示)

从深度图中提取边界信息:

一. 这个例子,一共提取了三种边界信息:

  1. obstacle border:对象边界(属于对象的最外面的可见点)
  2. shadow border:阴影边界(在背景中与遮挡物相邻的点)
  3. Veil points:面纱点集(对象边界边界与阴影边界之间的内插点)

     以下是一个典型的激光雷达获得的3D数据对应的点云图:

                                                             _images/range_image_border_points.png

二. 注意事项

      要提取边界信息,重要的是要区分未观察到的图像点应该观察到但超出传感器范围的点。后者通常用来标记边界,而未观察到的点通常不标记边界。因此,最好可以提供这些测量信息。如果无法提供超出这些应该观察到的传感器范围的点,则可以使用setUnseenToMaxRange函数,将那些点设置为最大深度(本例添加-m参数)。

 三.代码实现

/**This .cpp file is uesd to identify the border of the range image**/
/**我们感兴趣的边界主要分为:
 * 1.obstacle border :对象边界(属于被识别的对象的最外边的可见点)
 * 2.shadow border: 阴影边界(在背景中与遮挡物想临近的点)
 * 3. Veil point: 面纱点集(对象边界与阴影边界之间的点)
 * **/
/**注意事项:
 * 要提取边界信息,重要的是要区分未观察到的图像点和应该观察到但超出传感器范围的点。
 * 后者通常用来标记边界,而未观察到的点通常不标记边界。
 * 因此,最好可以提供这些测量信息。
 * 如果无法提供超出这些应该观察到的传感器范围的点,则可以使用setUnseenToMaxRange函数。
 * 将那些点设置为最大深度(本例添加-m参数)。**/

#include <iostream>
#include <boost/thread/thread.hpp>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/features/range_image_border_extractor.h>
#include <pcl/console/parse.h>

typedef pcl::PointXYZ PointType;  //将pcl::PointXYZ类型点,转命名为PointType。

//设置一些参数
float angular_resolution =0.5f;
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::LASER_FRAME; //此处设置为laser观察坐标系
bool setUnseenToMaxRange = false;

// --------------
// -----Help-----
// --------------
void
printUsage (const char* progName)
{
    std::cout << "\n\nUsage: "<<progName<<" [options] <scene.pcd>\n\n"
              << "Options:\n"
              << "-------------------------------------------\n"
              << "-r <float>   angular resolution in degrees (default "<<angular_resolution<<")\n"
              << "-c <int>     coordinate frame (default "<< (int)coordinate_frame<<")\n"
              << "-m           Treat all unseen points to max range\n"
              << "-h           this help\n"
              << "\n\n";
}

int main(int argc, char** argv){
  //首先获取到命令行输入的参数值
    if(pcl::console::find_argument (argc,argv,"-h") >=0){
        printUsage(argv[0]);
        return 0;
    }
    if(pcl::console::find_argument(argc,argv,"-m") >=0){
        setUnseenToMaxRange = true;
        cout << "Setting unseen values in range image to maximum range readings.\n";
    }

    int tmp_coordinate_frame;

    if (pcl::console::parse (argc, argv, "-c", tmp_coordinate_frame) >= 0)
    {
        coordinate_frame = pcl::RangeImage::CoordinateFrame (tmp_coordinate_frame);
        cout << "Using coordinate frame "<< (int)coordinate_frame<<".\n";
    }
    if (pcl::console::parse (argc, argv, "-r", angular_resolution) >= 0)
        cout << "Setting angular resolution to "<<angular_resolution<<"deg.\n";
        angular_resolution = pcl::deg2rad (angular_resolution);

    //读取PCD文件,并创建点云
    pcl::PointCloud<PointType>::Ptr point_cloud_ptr (new pcl::PointCloud<PointType>);
    pcl::PointCloud<PointType>& point_cloud = *point_cloud_ptr;

    pcl::PointCloud<pcl::PointWithViewpoint> far_ranges;

    Eigen::Affine3f scene_sensor_pose (Eigen::Affine3f::Identity ());

    std::vector<int> pcd_filename_indices = pcl::console::parse_file_extension_argument (argc, argv, "pcd");//判断文件拓展名是否为pcd
    if (!pcd_filename_indices.empty ())
    {
        std::string filename = argv[pcd_filename_indices[0]];
        if (pcl::io::loadPCDFile (filename, point_cloud) == -1)
        {
            cout << "Was not able to open file \""<<filename<<"\".\n";
            printUsage (argv[0]);
            return 0;
        }
        scene_sensor_pose = Eigen::Affine3f (Eigen::Translation3f (point_cloud.sensor_origin_[0],
                                                                   point_cloud.sensor_origin_[1],
                                                                   point_cloud.sensor_origin_[2])) *
                            Eigen::Affine3f (point_cloud.sensor_orientation_);

        std::string far_ranges_filename = pcl::getFilenameWithoutExtension (filename)+"_far_ranges.pcd";
        if (pcl::io::loadPCDFile(far_ranges_filename.c_str(), far_ranges) == -1)
            std::cout << "Far ranges file \""<<far_ranges_filename<<"\" does not exists.\n";
    }
    else
    {
        cout << "\nNo *.pcd file given => Generating example point cloud.\n\n";
        for (float x=-0.5f; x<=0.5f; x+=0.01f)
        {
            for (float y=-0.5f; y<=0.5f; y+=0.01f)
            {
                PointType point;  point.x = x;  point.y = y;  point.z = 2.0f - y;
                point_cloud.points.push_back (point);
            }
        }
        point_cloud.width = (int) point_cloud.points.size ();  point_cloud.height = 1;
    }

    //由点云数据创建深度图
    float noise_level = 0.0;
    float min_range = 0.0f;
    int border_size = 1;
    boost::shared_ptr<pcl::RangeImage> range_image_ptr (new pcl::RangeImage);
    pcl::RangeImage& range_image = *range_image_ptr;
    range_image.createFromPointCloud (point_cloud, angular_resolution, pcl::deg2rad (360.0f), pcl::deg2rad (180.0f),
                                      scene_sensor_pose, coordinate_frame, noise_level, min_range, border_size);
    range_image.integrateFarRanges (far_ranges);
    if (setUnseenToMaxRange)
        range_image.setUnseenToMaxRange ();

    //创建3D viewer显示点云和深度图(区分方法:分别设置点的大小和颜色,以便于区分)
    pcl::visualization::PCLVisualizer viewer ("3D Viewer");
    viewer.setBackgroundColor (1, 1, 1);
    viewer.addCoordinateSystem (1.0f, "global");
    pcl::visualization::PointCloudColorHandlerCustom<PointType> point_cloud_color_handler (point_cloud_ptr, 0, 0, 0);
    viewer.addPointCloud (point_cloud_ptr, point_cloud_color_handler, "original point cloud");


    //!!提取深度图的边界
    pcl::RangeImageBorderExtractor border_extractor (&range_image);  //创建边界提取对象
    pcl::PointCloud<pcl::BorderDescription> border_descriptions;     //创建存储边界信息的点云
    border_extractor.compute (border_descriptions);                  //将提取出来的边界点云->存到对象里

    //在3D viewer中,显示边界点云
    //首先将不同类型的边界点云定义出来并赋值
    pcl::PointCloud<pcl::PointWithRange>::Ptr border_points_ptr(new pcl::PointCloud<pcl::PointWithRange>), 
                                              veil_points_ptr(new pcl::PointCloud<pcl::PointWithRange>),
                                              shadow_points_ptr(new pcl::PointCloud<pcl::PointWithRange>);
    pcl::PointCloud<pcl::PointWithRange>& border_points = *border_points_ptr,
                                        & veil_points = * veil_points_ptr,
                                        & shadow_points = *shadow_points_ptr;

    for (int y=0; y< (int)range_image.height; ++y)
    {
        for (int x=0; x< (int)range_image.width; ++x)
        {
            if (border_descriptions.points[y*range_image.width + x].traits[pcl::BORDER_TRAIT__OBSTACLE_BORDER])
                border_points.points.push_back (range_image.points[y*range_image.width + x]);
            if (border_descriptions.points[y*range_image.width + x].traits[pcl::BORDER_TRAIT__VEIL_POINT])
                veil_points.points.push_back (range_image.points[y*range_image.width + x]);
            if (border_descriptions.points[y*range_image.width + x].traits[pcl::BORDER_TRAIT__SHADOW_BORDER])
                shadow_points.points.push_back (range_image.points[y*range_image.width + x]);
        }
    }
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> border_points_color_handler (border_points_ptr, 0, 255, 0);//green
    viewer.addPointCloud<pcl::PointWithRange> (border_points_ptr, border_points_color_handler, "border points");
    viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "border points");
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> veil_points_color_handler (veil_points_ptr, 255, 0, 0);//red
    viewer.addPointCloud<pcl::PointWithRange> (veil_points_ptr, veil_points_color_handler, "veil points");
    viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "veil points");
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> shadow_points_color_handler (shadow_points_ptr, 0, 255, 255); //blue
    viewer.addPointCloud<pcl::PointWithRange> (shadow_points_ptr, shadow_points_color_handler, "shadow points");
    viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "shadow points");


    //在3D viewer中显示深度图
    pcl::visualization::RangeImageVisualizer* range_image_borders_widget = NULL;
    range_image_borders_widget =
    pcl::visualization::RangeImageVisualizer::getRangeImageBordersWidget (range_image, -std::numeric_limits<float>::infinity (), std::numeric_limits<float>::infinity (), false,
                                                                                  border_descriptions, "Range image with borders");

    //--------------------
    // -----Main loop-----
    //--------------------
    while (!viewer.wasStopped ())
    {
        range_image_borders_widget->spinOnce ();
        viewer.spinOnce ();
        pcl_sleep(0.01);
    }
}

 运行情况如下(绿色为:对象边界; 红色为面纱边界;蓝色为阴影边界):

猜你喜欢

转载自blog.csdn.net/weixin_42503785/article/details/112460764