ISS+SAC-IA配准代码
时间: 2023-12-12 11:03:13 浏览: 98
这是我学习PCL点云配准的代码
以下是使用PCL库实现ISS+SAC-IA点云配准的代码示例:
```C++
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/features/normal_3d.h>
#include <pcl/features/iss_3d.h>
#include <pcl/registration/ia_ransac.h>
int main(int argc, char** argv)
{
// 加载点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_out(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile<pcl::PointXYZ>("input_cloud.pcd", *cloud_in);
pcl::io::loadPCDFile<pcl::PointXYZ>("target_cloud.pcd", *cloud_out);
// 计算法线
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
pcl::PointCloud<pcl::Normal>::Ptr normals_in(new pcl::PointCloud<pcl::Normal>);
pcl::PointCloud<pcl::Normal>::Ptr normals_out(new pcl::PointCloud<pcl::Normal>);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
tree->setInputCloud(cloud_in);
ne.setInputCloud(cloud_in);
ne.setSearchMethod(tree);
ne.setRadiusSearch(0.02);
ne.compute(*normals_in);
tree->setInputCloud(cloud_out);
ne.setInputCloud(cloud_out);
ne.setSearchMethod(tree);
ne.compute(*normals_out);
// 计算ISS关键点
pcl::ISSKeypoint3D<pcl::PointXYZ, pcl::PointXYZ> iss_detector;
pcl::PointCloud<pcl::PointXYZ>::Ptr keypoints_in(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr keypoints_out(new pcl::PointCloud<pcl::PointXYZ>);
iss_detector.setInputCloud(cloud_in);
iss_detector.setNormals(normals_in);
iss_detector.setSalientRadius(6 * 0.01);
iss_detector.setNonMaxRadius(4 * 0.01);
iss_detector.setThreshold21(0.99);
iss_detector.setThreshold32(0.99);
iss_detector.compute(*keypoints_in);
iss_detector.setInputCloud(cloud_out);
iss_detector.setNormals(normals_out);
iss_detector.compute(*keypoints_out);
// SAC-IA配准
pcl::SampleConsensusInitialAlignment<pcl::PointXYZ, pcl::PointXYZ, pcl::FPFHSignature33> sac_ia;
pcl::PointCloud<pcl::FPFHSignature33>::Ptr fpfhs_in(new pcl::PointCloud<pcl::FPFHSignature33>);
pcl::PointCloud<pcl::FPFHSignature33>::Ptr fpfhs_out(new pcl::PointCloud<pcl::FPFHSignature33>);
pcl::FPFHEstimation<pcl::PointXYZ, pcl::Normal, pcl::FPFHSignature33> fpfh;
fpfh.setInputCloud(keypoints_in);
fpfh.setInputNormals(normals_in);
fpfh.setSearchMethod(tree);
fpfh.setRadiusSearch(0.05);
fpfh.compute(*fpfhs_in);
fpfh.setInputCloud(keypoints_out);
fpfh.setInputNormals(normals_out);
fpfh.compute(*fpfhs_out);
sac_ia.setInputSource(keypoints_in);
sac_ia.setSourceFeatures(fpfhs_in);
sac_ia.setInputTarget(keypoints_out);
sac_ia.setTargetFeatures(fpfhs_out);
pcl::PointCloud<pcl::PointXYZ>::Ptr aligned(new pcl::PointCloud<pcl::PointXYZ>);
sac_ia.align(*aligned);
// 输出变换矩阵
std::cout << "Transformation matrix:" << std::endl << sac_ia.getFinalTransformation() << std::endl;
// 可视化结果
pcl::visualization::PCLVisualizer viewer("PointCloud Viewer");
viewer.setBackgroundColor(0.0, 0.0, 0.0);
viewer.addPointCloud<pcl::PointXYZ>(cloud_in, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloud_in, 255, 0, 0), "cloud_in");
viewer.addPointCloud<pcl::PointXYZ>(cloud_out, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloud_out, 0, 255, 0), "cloud_out");
viewer.addPointCloud<pcl::PointXYZ>(aligned, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(aligned, 0, 0, 255), "aligned");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud_in");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud_out");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "aligned");
viewer.addCoordinateSystem(1.0);
viewer.initCameraParameters();
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
return 0;
}
```
需要注意的是,这里使用了点云的ISS关键点和FPFH描述子来进行配准,因此需要先计算点云的法线,并提取ISS关键点和FPFH描述子。配准的结果可以通过变换矩阵来表示。
阅读全文