3d激光雷达开发(sift关键点)

时间:2022-11-23 22:57:22


        关键点在数据匹配、三维重建和物体识别方面发挥了巨大的作用。和narf关键点相比较,sift点用的更多。不光是点云数据,sift最早是用在图像方面,相信有过图像开发经验的朋友应该不陌生。这个算法是受专利保护的算法,这一点需要注意下。

1、准备sift.cpp

// STL
#include <iostream>

// PCL
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/io.h>
#include <pcl/keypoints/sift_keypoint.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/time.h>

/* This examples shows how to estimate the SIFT points based on the
* z gradient of the 3D points than using the Intensity gradient as
* usually used for SIFT keypoint estimation.
*/

namespace pcl
{
template<>
struct SIFTKeypointFieldSelector<PointXYZ>
{
inline float
operator () (const PointXYZ &p) const
{
return p.z;
}
};
}

int
main(int, char** argv)
{
std::string filename = argv[1];
std::cout << "Reading " << filename << std::endl;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>(filename, *cloud_xyz) == -1) // load the file
{
PCL_ERROR("Couldn't read file");
return -1;
}
std::cout << "points: " << cloud_xyz->points.size() << std::endl;

// Parameters for sift computation
const float min_scale = 0.005f; //the standard deviation of the smallest scale in the scale space
const int n_octaves = 6;//the number of octaves (i.e. doublings of scale) to compute
const int n_scales_per_octave = 4;//the number of scales to compute within each octave
const float min_contrast = 0.001f;//the minimum contrast required for detection


pcl::console::TicToc time;
time.tic();
// Estimate the sift interest points using z values from xyz as the Intensity variants
pcl::SIFTKeypoint<pcl::PointXYZ, pcl::PointWithScale> sift;
pcl::PointCloud<pcl::PointWithScale> result;
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
sift.setSearchMethod(tree);
sift.setScales(min_scale, n_octaves, n_scales_per_octave);
sift.setMinimumContrast(min_contrast);
sift.setInputCloud(cloud_xyz);
sift.compute(result);
std::cout << "Computing the SIFT points takes " << time.toc() / 1000 << "seconds" << std::endl;
std::cout << "No of SIFT points in the result are " << result.points.size() << std::endl;


// Copying the pointwithscale to pointxyz so as visualize the cloud
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_temp(new pcl::PointCloud<pcl::PointXYZ>);
copyPointCloud(result, *cloud_temp);
std::cout << "SIFT points in the result are " << cloud_temp->points.size() << std::endl;
// Visualization of keypoints along with the original cloud
pcl::visualization::PCLVisualizer viewer("PCL Viewer");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> keypoints_color_handler(cloud_temp, 0, 255, 0);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color_handler(cloud_xyz, 255, 0, 0);
viewer.setBackgroundColor(0.0, 0.0, 0.0);
viewer.addPointCloud(cloud_xyz, cloud_color_handler, "cloud");//add point cloud
viewer.addPointCloud(cloud_temp, keypoints_color_handler, "keypoints");//add the keypoints
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "keypoints");

while (!viewer.wasStopped())
{
viewer.spinOnce();
}


return 0;

}

2、准备CMakeLists.txt

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(sift)

find_package(PCL 1.2 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable (sift sift.cpp)
target_link_libraries (sift ${PCL_LIBRARIES})

3、创建sln工程,准备编译

3d激光雷达开发(sift关键点)

4、准备执行sift.exe

        为了查看效果,可以准备两个点云文件。比如,一个是bunny.pcd,一个是dragon.pcd。输入sift.exe bunny.pcd的效果如下所示,

3d激光雷达开发(sift关键点)

        后者的效果如下所示,

3d激光雷达开发(sift关键点)

         随着功能越来越多,相关的dll也要及时添加,不然很有可能执行不起来。

3d激光雷达开发(sift关键点)