#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/console/parse.h>
#include <pcl/common/file_io.h> // for getFilenameWithoutExtension
typedef pcl::PointXYZ PointType;
// -----Parameters-----
float angular_resolution = 0.5f;
float support_size = 0.2f;
bool setUnseenToMaxRange = true;
int main()
{
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;
pcl::io::loadPCDFile("sampled.pcd", point_cloud);
// -----------------------------------------------
// -----Create RangeImage from the PointCloud-----
// -----------------------------------------------
angular_resolution = pcl::deg2rad(angular_resolution);
Eigen::Affine3f scene_sensor_pose(Eigen::Affine3f::Identity());
//sensorPose将虚拟传感器的6DOF位置定义为滚动=俯仰=偏航=0的原点。
//Eigen::Affine3f sensorPose = (Eigen::Affine3f)Eigen::Translation3f(0.0f, 0.0f, 0.0f);
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_);
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME; //坐标系方向,x向右的,y向下,z向前
float noise_level = 0.0; //如果想平均同一单元格内的分数,你可以使用更高的值。0.05表示使用所有到最近点的最大距离为5cm的点来计算范围。
float min_range = 0.0f; //如果minRange大于0,所有靠近的点都将被忽略。
int border_size = 1; //如果borderSize大于0,在裁剪图像时,图像周围会留下一个未被观察点的边界。
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();
// --------------------------------------------
// -----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, 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.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.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_extractor; //边缘提取
pcl::NarfKeypoint narf_keypoint_detector(&range_image_border_extractor); //创建NarfKeypoint对象
narf_keypoint_detector.setRangeImage(&range_image);
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); //计算得到的关键点保存在keypoint_indices
std::cout << "Found " << keypoint_indices.size() << " key points.\n";
// ----------------------------------------------
// -----Show keypoints in range image widget-----
// ----------------------------------------------
//for (std::size_t i=0; i<keypoint_indices.size (); ++i)
//range_image_widget.markPoint (keypoint_indices[i]%range_image.width,
//keypoint_indices[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.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");
//--------------------
// -----Main loop-----
//--------------------
while (!viewer.wasStopped())
{
range_image_widget.spinOnce(); // process GUI events
viewer.spinOnce();
pcl_sleep(0.01);
}
}
提取NARF关键点
最新推荐文章于 2023-05-11 19:36:23 发布