pcl::PointCloud<PointT>::Ptr pcl 共享指针如何与模板类型共用
时间: 2023-08-06 16:08:36 浏览: 55
在PCL中,PointCloud是一个模板类,其中模板参数PointT表示点云中点的类型。而PointCloud的Ptr类型是一个共享指针,用于管理PointCloud对象的内存。因为PointCloud是一个模板类,所以需要在使用PointCloud::Ptr时指定模板参数PointT。
例如,如果我们想要使用PointXYZ类型的PointCloud对象,可以这样定义一个PointCloud::Ptr共享指针:
```
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
```
这里的PointCloud::Ptr是一个共享指针类型,而<pcl::PointXYZ>是PointCloud模板类的一个实例化。这样就能够使用PointCloud::Ptr来管理PointXYZ类型的PointCloud对象的内存了。
相关问题
创建一个pcl::PCLPointCloud2类型的指针类似于pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
创建pcl::PCLPointCloud2` 类型的指针对象的方法与创建 `pcl::PointCloud<PointT>` 类型的指针对象的方法类似,只需要将 `pcl::PointCloud<PointT>` 替换为 `pcl::PCLPointCloud2` 即可。
以下是一个示例:
```cpp
pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2);
```
这里,`pcl::PCLPointCloud2::Ptr` 定义了一个 `pcl::PCLPointCloud2` 类型的智能指针,`cloud` 是这个智能指针的对象。通过 `new` 关键字可以在堆中创建一个 `pcl::PCLPointCloud2` 类型的对象,并将其地址存储到 `cloud` 中。由于使用了智能指针,当 `cloud` 超出它的作用域时,它所指向的对象将自动被销毁,无需手动释放内存。
pcl::IterativeClosestPoint<PointT, PointT> icp; icp.setInputSource(source_cloud); icp.setInputTarget(target_cloud); icp.setMaxCorrespondenceDistance(0.05); pcl::registration::CorrespondenceRejectorSampleConsensus<PointT>::Ptr ransac(new pcl::registration::CorrespondenceRejectorSampleConsensus<PointT>()); ransac->setInputSource(source_cloud); ransac->setInputTarget(target_cloud); ransac->setInlierThreshold(0.05); ransac->setMaximumIterations(1000); ransac->setInputCorrespondences(correspondences); pcl::CorrespondencesPtr correspondences_filtered(new pcl::Correspondences); ransac->getCorrespondences(*correspondences_filtered); PointCloudT::Ptr aligned_cloud(new PointCloudT); icp.setInputCorrespondences(correspondences_filtered); icp.align(*aligned_cloud);如何修改
这段代码的问题在于,它将pcl::CorrespondencesPtr类型的指针correspondences_filtered作为参数传递给了icp.setInputCorrespondences()函数。实际上,pcl::IterativeClosestPoint<PointT, PointT>类中也没有定义setInputCorrespondences()函数。正确的做法是将pcl::Correspondences类型的对象correspondences_filtered作为参数传递给pcl::IterativeClosestPoint<PointT, PointT>类的align()函数,从而进行点云配准。修改后的代码如下:
```
pcl::IterativeClosestPoint<PointT, PointT> icp;
icp.setInputSource(source_cloud);
icp.setInputTarget(target_cloud);
icp.setMaxCorrespondenceDistance(0.05);
pcl::registration::CorrespondenceRejectorSampleConsensus<PointT>::Ptr ransac(new pcl::registration::CorrespondenceRejectorSampleConsensus<PointT>());
ransac->setInputSource(source_cloud);
ransac->setInputTarget(target_cloud);
ransac->setInlierThreshold(0.05);
ransac->setMaximumIterations(1000);
ransac->setInputCorrespondences(correspondences);
pcl::CorrespondencesPtr correspondences_filtered(new pcl::Correspondences);
ransac->getCorrespondences(*correspondences_filtered);
PointCloudT::Ptr aligned_cloud(new PointCloudT);
icp.align(*aligned_cloud, correspondences_filtered);
```
注意,这里通过icp.align()函数将点对应关系传递给ICP算法,从而进行点云配准。