一、Harris关键点检测
C++
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/io.h>
#include <pcl/keypoints/harris_3d.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/common/common_headers.h>
using namespace std;
int main(int, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);//要配准变化的点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_target(new pcl::PointCloud<pcl::PointXYZ>);//目标点云(不变的)
if (pcl::io::loadPCDFile<pcl::PointXYZ>("pcd/pig_view1.pcd", *cloud) == -1)
{
PCL_ERROR("加载点云失败\n");
}
if (pcl::io::loadPCDFile<pcl::PointXYZ>("pcd/pig_view2.pcd", *cloud_target) == -1)
{
PCL_ERROR("加载点云失败\n");
}
pcl::PointCloud<pcl::PointXYZI>::Ptr keypoints1(new pcl::PointCloud<pcl::PointXYZI>);
pcl::PointCloud<pcl::PointXYZI>::Ptr keypoints2(new pcl::PointCloud<pcl::PointXYZI>);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
pcl::HarrisKeypoint3D<pcl::PointXYZ, pcl::PointXYZI> harris1;
harris1.setSearchMethod(tree);
harris1.setInputCloud(cloud);
harris1.setNumberOfThreads(8); //初始化调度器并设置要使用的线程数
harris1.setRadius(4); //方块半径
harris1.setRadiusSearch(4);
harris1.setNonMaxSupression(true);
//harris1.setThreshold(1E-6);
harris1.compute(*keypoints1);
pcl::HarrisKeypoint3D<pcl::PointXYZ, pcl::PointXYZI> harris2;
harris2.setSearchMethod(tree);
harris2.setInputCloud(cloud_target);
harris2.setNumberOfThreads(8); //初始化调度器并设置要使用的线程数
harris2.setRadius(4); //方块半径
harris2.setRadiusSearch(4);
harris2.setNonMaxSupression(true);
//harris2.setThreshold(1E-6);
harris2.compute(*keypoints2);
pcl::PointIndicesConstPtr keypoints1_indices = harris1.getKeypointsIndices();
pcl::PointCloud<pcl::PointXYZ>::Ptr keys1(new pcl::PointCloud<pcl::PointXYZ>);
pcl::copyPointCloud(*cloud, *keypoints1_indices, *keys1);
pcl::PointIndicesConstPtr keypoints2_indices = harris2.getKeypointsIndices();
pcl::PointCloud<pcl::PointXYZ>::Ptr keys2(new pcl::PointCloud<pcl::PointXYZ>);
pcl::copyPointCloud(*cloud_target, *keypoints2_indices, *keys2);
关键点显示
boost::shared_ptr<pcl::visualization::PCLVisualizer>viewer1(new pcl::visualization::PCLVisualizer("v1"));
viewer1->setBackgroundColor(0, 0, 0);
viewer1->setWindowName("Harris");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> in_color1(keys1, 0.0, 255.0, 0.0);
viewer1->addPointCloud<pcl::PointXYZ>(keys1, in_color1, "key_color");//特征点
viewer1->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "key_color");
while (!viewer1->wasStopped())
{
viewer1->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100));
}
boost::shared_ptr<pcl::visualization::PCLVisualizer>viewer2(new pcl::visualization::PCLVisualizer("v2"));
viewer2->setBackgroundColor(0, 0, 0);
viewer2->setWindowName("Harris");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> in_color2(keys2, 0.0, 255.0, 0.0);
viewer2->addPointCloud<pcl::PointXYZ>(keys2, in_color2, "key_color");//特征点
viewer2->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "key_color");
while (!viewer2->wasStopped())
{
viewer2->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100));
}
boost::shared_ptr<pcl::visualization::PCLVisualizer>viewer3(new pcl::visualization::PCLVisualizer("v3"));
viewer3->setBackgroundColor(0, 0, 0);
viewer3->setWindowName("Harris");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> in_color3(cloud, 0.0, 255.0, 0.0);
viewer3->addPointCloud<pcl::PointXYZ>(cloud, in_color3, "in_color");
viewer3->addPointCloud<pcl::PointXYZ>(keys1, "key_color");//特征点
viewer3->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "key_color");
viewer3->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 0.0, 0.0, "key_color");
while (!viewer3->wasStopped())
{
viewer3->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100));
}
boost::shared_ptr<pcl::visualization::PCLVisualizer>viewer4(new pcl::visualization::PCLVisualizer("v4"));
viewer4->setBackgroundColor(0, 0, 0);
viewer4->setWindowName("Harris");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> in_color4(cloud_target, 0.0, 255.0, 0.0);
viewer4->addPointCloud<pcl::PointXYZ>(cloud_target, in_color4, "in_color");
viewer4->addPointCloud<pcl::PointXYZ>(keys2, "key_color");//特征点
viewer4->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "key_color");
viewer4->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 0.0, 0.0, "key_color");
while (!viewer4->wasStopped())
{
viewer4->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100));
}
return 0;
}
关键代码解析:
pcl::PointCloudpcl::PointXYZI::Ptr keypoints1(new pcl::PointCloudpcl::PointXYZI);
pcl::search::KdTreepcl::PointXYZ::Ptr tree(new pcl::search::KdTreepcl::PointXYZ());
pcl::HarrisKeypoint3D<pcl::PointXYZ, pcl::PointXYZI> harris1;
harris1.setSearchMethod(tree);
harris1.setInputCloud(cloud);
harris1.setNumberOfThreads(8); //初始化调度器并设置要使用的线程数
harris1.setRadius(4); //方块半径
harris1.setRadiusSearch(4);
harris1.setNonMaxSupression(true);
harris1.setThreshold(1E-6);
harris1.compute(*keypoints1);
pcl::PointIndicesConstPtr keypoints1_indices = harris1.getKeypointsIndices();
pcl::PointCloudpcl::PointXYZ::Ptr keys1(new pcl::PointCloudpcl::PointXYZ);
pcl::copyPointCloud(*cloud, *keypoints1_indices, *keys1);
-
pcl::PointCloud<pcl::PointXYZI>::Ptr keypoints1(new pcl::PointCloud<pcl::PointXYZI>);
:创建一个指向包含带有强度信息的三维关键点的点云的指针。harris1.compute()成员函数只能放入pcl::PointCloud<pcl::PointXYZI>类型的变量。
-
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
:创建一个KD树搜索对象的指针,用于在点云中搜索最近邻点。 -
pcl::HarrisKeypoint3D<pcl::PointXYZ, pcl::PointXYZI> harris1;
:创建一个Harris角点检测对象。这里指定输入点的类型为pcl::PointXYZ
,输出点的类型为pcl::PointXYZI
,即带有强度信息的点。 -
harris1.setSearchMethod(tree);
:设置Harris角点检测中使用的搜索方法为KD树搜索。 -
harris1.setInputCloud(cloud);
:设置输入点云,即要在其上执行Harris角点检测的点云。 -
harris1.setNumberOfThreads(8);
:初始化并设置用于计算的线程数。在这里,设置为8个线程。 -
harris1.setRadius(4);
:设置用于Harris角点检测的立方体的半边长。这个参数用于确定局部区域以计算Harris角点响应。 -
harris1.setRadiusSearch(4);
:设置用于搜索最近邻点的球形邻域的半径。这个参数控制着点云中每个点的邻域大小。 -
harris1.setNonMaxSupression(true);
:设置是否进行非最大抑制,以在检测到的角点之间进行抑制,只保留局部最大值。 -
harris1.setThreshold(1E-6);
:设置Harris响应阈值。只有大于此阈值的角点才会被保留。 -
harris1.compute(*keypoints1);
:执行Harris角点检测,并将检测到的角点存储在keypoints1
中。 -
pcl::PointIndicesConstPtr keypoints1_indices = harris1.getKeypointsIndices();
:获取检测到的角点的索引。 -
pcl::PointCloud<pcl::PointXYZ>::Ptr keys1(new pcl::PointCloud<pcl::PointXYZ>);
:创建一个新的点云对象,用于存储没有强度信息的角点。 -
pcl::copyPointCloud(*cloud, *keypoints1_indices, *keys1);
:将检测到的角点从原始点云中复制到新创建的点云对象中。
这些参数的设置会影响Harris角点检测的性能和结果。例如,调整半径、阈值和是否进行非最大抑制会影响检测到的角点数量和质量。增加线程数可以加速计算,但也会增加计算资源的消耗。
结果:
输入点云的关键点
输出点云的关键点
输入点云的关键点与输入点云一起展示
输出点云的关键点与输出点云一起展示
二、Harris关键点检测及SAC-IA粗配准
C++
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/io.h>
#include <pcl/keypoints/harris_3d.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/features/fpfh_omp.h>
#include <pcl/common/common_headers.h>
#include <pcl/registration/ia_ransac.h>
using namespace std;
void extract_keypoint(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, pcl::PointCloud<pcl::PointXYZI>::Ptr& keypoint)
{
pcl::HarrisKeypoint3D<pcl::PointXYZ, pcl::PointXYZI> harris;
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
harris.setSearchMethod(tree);
harris.setInputCloud(cloud);
harris.setNumberOfThreads(8); //初始化调度器并设置要使用的线程数
harris.setRadius(4); //方块半径
harris.setRadiusSearch(4);
harris.setNonMaxSupression(true);
//harris.setThreshold(1E-6);
harris.compute(*keypoint);
}
pcl::PointCloud<pcl::FPFHSignature33>::Ptr compute_fpfh_feature(pcl::PointCloud<pcl::PointXYZI>::Ptr& keypoint)
{
pcl::search::KdTree<pcl::PointXYZI>::Ptr tree;
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
pcl::NormalEstimation<pcl::PointXYZI, pcl::Normal> n;
n.setInputCloud(keypoint);
n.setSearchMethod(tree);
n.setKSearch(10);
n.compute(*normals);
pcl::PointCloud<pcl::FPFHSignature33>::Ptr fpfh(new pcl::PointCloud<pcl::FPFHSignature33>);
pcl::FPFHEstimationOMP<pcl::PointXYZI, pcl::Normal, pcl::FPFHSignature33> f;
f.setNumberOfThreads(8);
f.setInputCloud(keypoint);
f.setInputNormals(normals);
f.setSearchMethod(tree);
f.setRadiusSearch(50);
f.compute(*fpfh);
return fpfh;
}
pcl::PointCloud<pcl::PointXYZ>::Ptr sac_align(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, pcl::PointCloud<pcl::PointXYZI>::Ptr s_k, pcl::PointCloud<pcl::PointXYZI>::Ptr t_k, pcl::PointCloud<pcl::FPFHSignature33>::Ptr sk_fpfh, pcl::PointCloud<pcl::FPFHSignature33>::Ptr tk_fpfh)
{
pcl::SampleConsensusInitialAlignment<pcl::PointXYZ, pcl::PointXYZ, pcl::FPFHSignature33> scia;
pcl::PointCloud<pcl::PointXYZ>::Ptr s_k1(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr t_k1(new pcl::PointCloud<pcl::PointXYZ>);
pcl::copyPointCloud(*s_k, *s_k1);
pcl::copyPointCloud(*t_k, *t_k1);
scia.setInputSource(s_k1);
scia.setInputTarget(t_k1);
scia.setSourceFeatures(sk_fpfh);
scia.setTargetFeatures(tk_fpfh);
scia.setMinSampleDistance(7);///参数:设置采样点之间的最小距离,满足的被当做采样点
scia.setNumberOfSamples(100);设置每次迭代设置采样点的个数(这个参数多可以增加配准精度)
scia.setCorrespondenceRandomness(6);//设置选择随机特征对应点时要使用的邻域点个数。值越大,特征匹配的随机性就越大
pcl::PointCloud<pcl::PointXYZ>::Ptr sac_result(new pcl::PointCloud<pcl::PointXYZ>);
scia.align(*sac_result);
pcl::transformPointCloud(*cloud, *sac_result, scia.getFinalTransformation());
return sac_result;
}
int main(int, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);//要配准变化的点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_target(new pcl::PointCloud<pcl::PointXYZ>);//目标点云(不变的)
if (pcl::io::loadPCDFile<pcl::PointXYZ>("pcd/pig_view1.pcd", *cloud) == -1)
{
PCL_ERROR("加载点云失败\n");
}
if (pcl::io::loadPCDFile<pcl::PointXYZ>("pcd/pig_view2.pcd", *cloud_target) == -1)
{
PCL_ERROR("加载点云失败\n");
}
boost::shared_ptr<pcl::visualization::PCLVisualizer>viewer1(new pcl::visualization::PCLVisualizer("v2"));
viewer1->setWindowName("Harris");
viewer1->setBackgroundColor(0, 0, 0); //设置背景颜色为黑色
// 对目标点云着色可视化 (red).
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>target_color1(cloud_target, 255, 0, 0);
viewer1->addPointCloud<pcl::PointXYZ>(cloud_target, target_color1, "target cloud");
viewer1->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "target cloud");
// 对源点云着色可视化 (green).
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>input_color1(cloud, 0, 255, 0);
viewer1->addPointCloud<pcl::PointXYZ>(cloud, input_color1, "input cloud");
viewer1->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "input cloud");
while (!viewer1->wasStopped())
{
viewer1->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100));
}
///粗配准
pcl::PointCloud<pcl::PointXYZI>::Ptr s_k(new pcl::PointCloud<pcl::PointXYZI>);
pcl::PointCloud<pcl::PointXYZI>::Ptr t_k(new pcl::PointCloud<pcl::PointXYZI>);
extract_keypoint(cloud, s_k);
extract_keypoint(cloud_target, t_k);
pcl::PointCloud<pcl::FPFHSignature33>::Ptr sk_fpfh = compute_fpfh_feature(s_k);
pcl::PointCloud<pcl::FPFHSignature33>::Ptr tk_fpfh = compute_fpfh_feature(t_k);
pcl::PointCloud<pcl::PointXYZ>::Ptr result(new pcl::PointCloud<pcl::PointXYZ>);
result = sac_align(cloud, s_k, t_k, sk_fpfh, tk_fpfh);
//可视化
cout << "读取点云个数: " << cloud->points.size() << endl;
cout << "Harris_3D points 的提取结果为 " << s_k->points.size() << endl;
//配准可视化
boost::shared_ptr<pcl::visualization::PCLVisualizer>viewer2(new pcl::visualization::PCLVisualizer("v2"));
viewer2->setWindowName("Harris");
viewer2->setBackgroundColor(0, 0, 0); //设置背景颜色为黑色
// 对目标点云着色可视化 (red).
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>target_color2(cloud_target, 255, 0, 0);
viewer2->addPointCloud<pcl::PointXYZ>(cloud_target, target_color2, "target cloud");
viewer2->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "target cloud");
// 对源点云着色可视化 (green).
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>input_color2(result, 0, 255, 0);
viewer2->addPointCloud<pcl::PointXYZ>(result, input_color2, "input cloud");
viewer2->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "input cloud");
while (!viewer2->wasStopped())
{
viewer2->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100));
}
return 0;
}
关键代码解析:
我之前在iss关键点检测以及SAC-IA粗配准-CSDN博客
和本章第一部分已经解释了大部分函数,这里就不赘述了
结果:
输入点云与输出点云
配准后的输入点云与输出点云,实际效果相对较好,运行不算慢,只要一两分钟就能出结果