一、如何从范围图像中提取NARF关键点
??这篇博文简单介绍什么是NARF,并使用官网代码演示了如何从深度图像的NARF关键点位置提取NARF描述符,官方原版链接点击这里。
1.NARF(法线对齐的径向特征)简要介绍 ?? NARF 全称 normal aligned radial feature(法线对齐的径向特征) 。是一种3D关键点检测的描述子,常用于物体匹配的关键点特征计算。可以类比于二维图像关键点检测算法 SIFT (Scale Invariant Feature Transform)和 SURF (Speeded Up Robust Features)
??NARF提取关键点算法的目标有两点:
?? ①在表面稳定的区域(where the surface is stable)提取关键点,以确保该关键点对法线的估计具有较强的鲁邦性,同时在其邻域点的法线应具有明显的变化。 ?? ②提取出的点应尽可能的位于物体的边缘处,从而使其得在不同的视角下都能提取到大部分相同的特征点。 ?? ?? 2.NARF数学原理及算法思想 ??NARF的具体数学原理可以参考mysticalwing前辈的这篇博客。或直接参阅源论文《Point feature extraction on 3D range scans taking into account object boundaries》 ??论文中证明了NARF在物体识别时所计算出的特征在不同视角下匹配结果都很好。 这里简要概述一下NARF的算法思想: ??步骤①:遍历每个深度图像点(深度图即以深度值代替像素值的图像,在PCL中可以将有序点云直接转换成深度图,下面代码中有转换方法),通过寻找在近邻区域有深度突变的位置进行边缘检测。 ??步骤②:遍历每个深度图像点,根据近邻区域的表面变化决定一种测度表面变化的系数,以及变化的主 方向。 ??步骤③:根据第二步找到的主方向计算兴趣值,表征该方向与其他方向的不同,以及该处表面的变化情况,即该点的稳定情况。 ??步骤④:对兴趣值进行平滑过滤。 ??步骤⑤:对关键点周围的关键点进行非极大值抑制算法(NMS),从而找到最终的关键点,即为 NARF 关键点。 ?? ??
二、PCL示例代码分析
??在pcl::feature中有关于NARF的具体实现,对于使用者而言,我们只需要考虑如何使用这一算子。 ??以下面的示例代码为例,在开始处,代码做了命令行解析,并从磁盘读取点云(如果没有点云则创造点云),利用这些点云创建深度图像,并利用深度图像提取NARF关键点。
#include <iostream>
#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/features/narf_descriptor.h>
#include <pcl/console/parse.h>
#include <pcl/common/file_io.h>
typedef pcl::PointXYZ PointType;
float angular_resolution = 0.5f;
float support_size = 0.2f;
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
bool setUnseenToMaxRange = false;
bool rotation_invariant = true;
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"
<< "-s <float> support size for the interest points (diameter of the used sphere - "
"default "<<support_size<<")\n"
<< "-o <0/1> switch rotational invariant version of the feature on/off"
<< " (default "<< (int)rotation_invariant<<")\n"
<< "-h this help\n"
<< "\n\n";
}
void
setViewerPose (pcl::visualization::PCLVisualizer& viewer, const Eigen::Affine3f& viewer_pose)
{
Eigen::Vector3f pos_vector = viewer_pose * Eigen::Vector3f (0, 0, 0);
Eigen::Vector3f look_at_vector = viewer_pose.rotation () * Eigen::Vector3f (0, 0, 1) + pos_vector;
Eigen::Vector3f up_vector = viewer_pose.rotation () * Eigen::Vector3f (0, -1, 0);
viewer.setCameraPosition (pos_vector[0], pos_vector[1], pos_vector[2],
look_at_vector[0], look_at_vector[1], look_at_vector[2],
up_vector[0], up_vector[1], up_vector[2]);
}
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;
std::cout << "Setting unseen values in range image to maximum range readings.\n";
}
if (pcl::console::parse (argc, argv, "-o", rotation_invariant) >= 0)
std::cout << "Switching rotation invariant feature version "<< (rotation_invariant ? "on" : "off")<<".\n";
int tmp_coordinate_frame;
if (pcl::console::parse (argc, argv, "-c", tmp_coordinate_frame) >= 0)
{
coordinate_frame = pcl::RangeImage::CoordinateFrame (tmp_coordinate_frame);
std::cout << "Using coordinate frame "<< (int)coordinate_frame<<".\n";
}
if (pcl::console::parse (argc, argv, "-s", support_size) >= 0)
std::cout << "Setting support size to "<<support_size<<".\n";
if (pcl::console::parse (argc, argv, "-r", angular_resolution) >= 0)
std::cout << "Setting angular resolution to "<<angular_resolution<<"deg.\n";
angular_resolution = pcl::deg2rad (angular_resolution);
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");
if (!pcd_filename_indices.empty ())
{
std::string filename = argv[pcd_filename_indices[0]];
if (pcl::io::loadPCDFile (filename, point_cloud) == -1)
{
std::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;
std::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.push_back (point);
}
}
point_cloud.width = point_cloud.size (); point_cloud.height = 1;
}
float noise_level = 0.0;
float min_range = 0.0f;
int border_size = 1;
pcl::RangeImage::Ptr 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 ();
pcl::visualization::PCLVisualizer viewer ("3D Viewer");
viewer.setBackgroundColor (1, 1, 1);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> range_image_color_handler (range_image_ptr, 0, 0, 0);
viewer.addPointCloud (range_image_ptr, range_image_color_handler, "range image");
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "range image");
viewer.initCameraParameters ();
setViewerPose (viewer, range_image.getTransformationToWorldSystem ());
pcl::visualization::RangeImageVisualizer range_image_widget ("Range image");
range_image_widget.showRangeImage (range_image);
pcl::RangeImageBorderExtractor range_image_border_extractor;
pcl::NarfKeypoint narf_keypoint_detector;
narf_keypoint_detector.setRangeImageBorderExtractor (&range_image_border_extractor);
narf_keypoint_detector.setRangeImage (&range_image);
narf_keypoint_detector.getParameters ().support_size = support_size;
pcl::PointCloud<int> keypoint_indices;
narf_keypoint_detector.compute (keypoint_indices);
std::cout << "Found "<<keypoint_indices.size ()<<" key points.\n";
pcl::PointCloud<pcl::PointXYZ>::Ptr keypoints_ptr (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>& keypoints = *keypoints_ptr;
keypoints.resize (keypoint_indices.size ());
for (std::size_t i=0; i<keypoint_indices.size (); ++i)
keypoints[i].getVector3fMap () = range_image[keypoint_indices[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, 7, "keypoints");
std::vector<int> keypoint_indices2;
keypoint_indices2.resize (keypoint_indices.size ());
for (unsigned int i=0; i<keypoint_indices.size (); ++i)
keypoint_indices2[i]=keypoint_indices[i];
pcl::NarfDescriptor narf_descriptor (&range_image, &keypoint_indices2);
narf_descriptor.getParameters ().support_size = support_size;
narf_descriptor.getParameters ().rotation_invariant = rotation_invariant;
pcl::PointCloud<pcl::Narf36> narf_descriptors;
narf_descriptor.compute (narf_descriptors);
std::cout << "Extracted "<<narf_descriptors.size ()<<" descriptors for "
<<keypoint_indices.size ()<< " keypoints.\n";
while (!viewer.wasStopped ())
{
range_image_widget.spinOnce ();
viewer.spinOnce ();
pcl_sleep(0.01);
}
}
代码效果: ??这段代码首先创建NarfDescriptor对象,并给它输入数据(关键点索引和深度图像)。然后设置两个重要参数。support_size,它决定了计算描述符所在区域(球形半径范围)的大小;rotation_invariant,它决定了是否应该使用NARF描述符的旋转不变性(围绕旋转主轴法线旋转)。然后我们创建输出点云并执行实际的计算。最后,输出关键点个数和提取的描述符个数。这些数字可能不同。例如,描述符的计算可能会失败,因为范围图像中没有足够的点(分辨率过低)。或者可能在同一个地方有多个描述符(不同的旋转主轴创造多个描述符)。 ??得到的PointCloud包含类型Narf36(参见common/include/pcl/point_types.h),并将描述符存储为36个元素float和x、y、z、roll、pitch、yaw,以描述提取特征的本地坐标系。现在可以在特征坐标系上比较描述符,例如,按照曼哈顿距离(绝对差的和)进行比较。
总结:本章节简要介绍了NARF,并给出了NARF原理的源论文地址,在后半部分分析了NARF示例代码的结构。在下一篇文章将介绍基于转动惯量和偏心量的描述符。
【博主简介】 ??斯坦福的兔子,男,天津大学机械工程工学硕士。毕业至今从事光学三维成像及点云处理相关工作。因工作中使用的三维处理库为公司内部库,不具有普遍适用性,遂自学开源PCL库及其相关数学知识以备使用。谨此将自学过程与君共享。 博主才疏学浅,尚不具有指导能力,如有问题还请各位在评论处留言供大家共同讨论。 若前辈们有工作机会介绍欢迎私信。
|