PCL点云处理与关键点提取

关键点简介

关键点也称为兴趣点,它是 2D 图像或 3D 点云或曲面模型上,可以通过检测标准来获取的具有稳定性、区别性的点集。从技术上来说,关键点的数量比原始点云或图像的数据量少很多,其与局部特征描述子结合组成关键点描述子。常用来构成原始数据的紧凑表示,具有代表性与描述性,从而加快后续识别、追踪等对数据的处理速度 。(也就是能够代表对象特征的子点集)
固而,关键点提取就成为了2D与3D信息处理中不可获缺的关键技术

关键点概念以及算法

NARF(Normal Aligned Radial Feature)关键点是为了从深度图像中识别物体而提出的,关键点探测的重要一步是减少特征提取时的搜索空间,对NARF关键点提取过程有以下要求:

  • 提取的过程必须考虑边缘以及物体表面变化信息
  • 即使换了不同的视角,关键点的位置必须稳定的可以被重复探测
  • 关键点所在的位置必须有稳定的支持区域,可以计算描述子和估计唯一的法向量。
    为了满足上述要求,可以通过以下探测步骤来进行关键点提取:
  1. 遍历每个深度图像点,通过寻找在邻近区域有深度突变的位置进行边缘检测
  2. 遍历每个深度图像点,根据近邻区域的表面变化决定一测度表面变化的系数,以及变化的主方向;
  3. 根据第2步找到的主方向计算兴趣值,表征该方向与其他方向的不同,以及该处表面的变化情况,即该点有多稳定;
  4. 对兴趣值进行平滑过滤;
  5. 进行非极大值抑制找到最终的关键点,即为 NARF 关键点。
  • 实践项目
    narf_keypoint_extraction.cpp
#include <iostream>

#include <boost/thread/thread.hpp>
#include <pcl/range_image/range_image.h>
#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/keypoints/narf_keypoint.h>
#include <pcl/console/parse.h>
#include <pcl/common/file_io.h>

typedef pcl::PointXYZ PointType;

// --------------------
// -----Parameters-----
// --------------------
float angular_resolution=0.5f;  //应该时求解 局部描述子的时候,对应的beam的间隔角度  这个觉得了直方差的维度
float support_size=0.2f; //这个有两个作用,一个是 support_size/2 对应的算术特征点提取的局部参考空间大小,另外一个在求解局部描述子时,对应的patch的覆盖范围
pcl::RangeImage::CoordinateFrame coordinate_frame=pcl::RangeImage::CAMERA_FRAME; //深度世界坐标系的类型
bool setUnseenToMaxRange=false;  //对于超出传感器范围的量采取的措施

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 as maximum range readings\n"
              << "-s <float>   support size for the interest points (diameter of the used sphere - "
              << "default " << support_size << ")\n"
              << "-h           this help\n"
              << "\n\n";
}

// --------------
// -----Main-----
// --------------
int main(int argc,char **argv){
    // --------------------------------------
    // -----Parse Command Line Arguments-----
    // --------------------------------------
    // 显示help参数
    if (pcl::console::find_argument(argc, argv, "-h") >= 0) {
        printUsage(argv[0]);
        return 0;
    }
    // 对待超出距离的点的处理方法,true就是最大化处理
    if (pcl::console::find_argument(argc, argv, "-m") >= 0) {
        setUnseenToMaxRange = true;
        cout << "Setting unseen values in range image to maximum range readings.\n";
    }
    // 读取坐标轴的类型,这个可以查查api的定义
    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";
    }
    //从命令参数里面获取angular_resolution
    //从命令参数里面获取support_size
    if (pcl::console::parse(argc, argv, "-s", support_size) >= 0)
        cout << "Setting support size to " << support_size << ".\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);

    // ------------------------------------------------------------------
    // -----Read pcd file or create example point cloud if not given-----
    // ------------------------------------------------------------------
    // 下面需要完成以下内容,从文件中读取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());

    //在命令行参数列表里面获取pcd文件名并加载
    std::vector<int> pcd_filename_indices = pcl::console::parse_file_extension_argument(argc, argv, "pcd");
    if (!pcd_filename_indices.empty()) {
        std::string filename = argv[pcd_filename_indices[0]];
        if (pcl::io::loadPCDFile(filename, point_cloud) == -1) {
            cerr << "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{
        setUnseenToMaxRange=true;
        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;
    }
    // -----------------------------------------------
    // -----Create RangeImage from the PointCloud-----
    // -----------------------------------------------
    float nosie_level=0.0;
    //最小范围
    float min_range=0.0f;
    int border_size=1;
    std::shared_ptr<pcl::RangeImage> range_image_ptr(new pcl::RangeImage);
    pcl::RangeImage &range_image=*range_image_ptr;
    // 将点云变成深度图
    // border_size=1 边框尺度
    // 噪声级别,如果想要更好的去除噪声,就去更高的值
    range_image.createFromPointCloud(point_cloud,angular_resolution,
                                     pcl::deg2rad(360.0f),pcl::deg2rad(180.0f),
                                     scene_sensor_pose,
                                     coordinate_frame,
                                     nosie_level,
                                     min_range,
                                     border_size);
    // 加载元景图
    range_image.integrateFarRanges(far_ranges);
    // 对于没有探测到的点的处理办法
    if(setUnseenToMaxRange)
        range_image.setUnseenToMaxRange();
    // --------------------------------------------
    // -----Open 3D viewer and add point cloud-----
    // --------------------------------------------
    pcl::visualization::PCLVisualizer viewer("3D Viewer");
    viewer.setBackgroundColor(1, 1, 1);
		//颜色处理
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> range_image_color_handler(range_image_ptr,
                                                                                                    255, 0, 0);
    //深度图加载颜色处理
    viewer.addPointCloud(range_image_ptr, range_image_color_handler, "range image");
    //设定点大小的属性
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "range image");
    //增加显示坐标轴
    viewer.addCoordinateSystem (1.0f, "global");

     //PointCloudColorHandlerCustom<PointType> point_cloud_color_handler (point_cloud_ptr, 150, 150, 150);
    //viewer.addPointCloud (point_cloud_ptr, point_cloud_color_handler, "original point cloud");
    //初始化viewer参数
    viewer.initCameraParameters();
    //setViewerPose (viewer, range_image.getTransformationToWorldSystem ());

    // --------------------------
    // -----Show range image-----
    // --------------------------
    // 显示深度图
    pcl::visualization::RangeImageVisualizer range_image_widget("Range image");
    range_image_widget.showRangeImage(range_image);
    
    // --------------------------------
    // -----Extract NARF keypoints-----
    // --------------------------------
    //边框抽取器
    pcl::RangeImageBorderExtractor range_image_border_extrator;
    pcl::NarfKeypoint narf_keypoint_detector(&range_image_border_extrator);
    narf_keypoint_detector.setRangeImage(&range_image);
    //计算NARF可信度时的范围
    narf_keypoint_detector.getParameters().support_size=support_size;
    //narf_keypoint_detector.getParameters ().add_points_on_straight_edges = true;
    //narf_keypoint_detector.getParameters ().distance_for_additional_points = 0.5;
	 // 关键点的索引
    pcl::PointCloud<int> keypoint_indices;
    //获取关键点 索引
    narf_keypoint_detector.compute(keypoint_indices);
		
    std::cout << "Found " << keypoint_indices.points.size() << " key points.\n";
    // ----------------------------------------------
    // -----Show keypoints in range image widget-----
    // ----------------------------------------------
    //for (size_t i=0; i<keypoint_indices.points.size (); ++i)
    //range_image_widget.markPoint (keypoint_indices.points[i]%range_image.width,
    //keypoint_indices.points[i]/range_image.width);

    // -------------------------------------
    // -----Show keypoints in 3D viewer-----
    // -------------------------------------
    //显示关键点
    pcl::PointCloud<pcl::PointXYZ>::Ptr keypoints_ptr(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ> &keypoints=*keypoints_ptr;
    keypoints.points.resize(keypoint_indices.points.size());
    for(size_t i=0;i<keypoint_indices.points.size();i++)
    {
        keypoints.points[i].getVector3fMap() = range_image.points[keypoint_indices.points[i]].getVector3fMap();
    }
    // 颜色处理,与点大小
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> keypoints_color_handler(keypoints_ptr, 0, 255, 0);
    viewer.addPointCloud<pcl::PointXYZ>(keypoints_ptr, keypoints_color_handler, "keypoints");
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "keypoints");

    //--------------------
    // -----Main loop-----
    //--------------------
    while (!viewer.wasStopped()) {
        range_image_widget.spinOnce();  // process GUI events
        viewer.spinOnce();
        pcl_sleep(0.01);
    }
 }

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值