利用PCL索引进行点云的提取

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

利⽤PCL索引进⾏点云的提取
最近开始动⼿做实验,之前写了⼀个⼩实验利⽤到了PCL库中的索引;
现在在写利⽤PCL中的RegionGrowing类来分割⽣成⾯⽚,⽆论是迭代⽣成还是进⾏提取都需要⽤到pcl库中定义的索引,
虽然搞的不是太明⽩,还是想写下来来记录⾃⼰的思路。

先看⼀下PCL是如何定义PointIndices的结构:
1struct PointIndices
2 {
3 PointIndices () : header (), indices ()
4 {}
5
6 ::pcl::PCLHeader header;
7
8 std::vector<int> indices;
9
10public:
11 typedef boost::shared_ptr< ::pcl::PointIndices> Ptr;
12 typedef boost::shared_ptr< ::pcl::PointIndices const> ConstPtr;
13 }; // struct PointIndices
1 typedef boost::shared_ptr< ::pcl::PointIndices> PointIndicesPtr;
2 typedef boost::shared_ptr< ::pcl::PointIndices const> PointIndicesConstPtr;
可以看出在数据结构 PointIndices 中定义了点云的header和indices;这⾥我们不关⼼header的结构,⽽indices的结构就是简单的int类型的vector;
所以我们经常见到⼀些代码直接定义索引的时候直接使⽤了⼀下的定义:
1 std::vector<int > points_indices;//int类型的vector类
或者:
1 pcl::IndicesPtr indices(new std::vector <int>);//指向int类型的vector类的空智能指针
若要将智能指针指向定义的 points_indices,需要:
pcl::IndicesPtr index_ptr = boost::make_shared<std::vector<int>>(points_indices);
或者:
pcl::IndicesPtr index_ptr(new std::vector<int>(points_indices));
因为在pcl_base.h中有如下定义:
1 typedef boost::shared_ptr <std::vector<int> > IndicesPtr;
2 typedef boost::shared_ptr <const std::vector<int> > IndicesConstPtr;
PS:
pcl中⼤量⽤到了智能指针 share_ptr,shared_ptr允许多个指针指向同⼀个对象
 智能指针的使⽤⽅式与普通指针类似:
1.解引⽤⼀个智能指针返回它指向的对象;
2.如果在⼀个条件判断中使⽤智能指针,效果就是检测它是否为空.
使⽤智能指针的初始化:
1//⼀般的初始化⽅式
2 shared_ptr<string> pint(new string("normal usage!"));
3 cout<<*pint<<endl;
4
5//推荐的安全的初始化⽅式
6 shared_ptr<string> pint1 = make_shared<string>("safe uage!");
7 cout<<*pint1<<endl;
8
先把之前利⽤到的写⼀些:
1 int j = 0;
2 std::vector<int > indexs;
3for (auto i : *normals)
4 {
5if (i.normal_z < 0.05 && i.normal_z > -0.05)
6 {
7 normals1->points.push_back(i);
8 indexs.push_back(j);
9 }
10 j++;
11 }
12//打印滤波后将法向量存储在normal1的信息,以及相应的索引
13 std::cout << *normals1 << std::endl;
14 std::cout << indexs.size() << std::endl;
15
16//索引
17 boost::shared_ptr<std::vector<int>> index_ptr = boost::make_shared<std::vector<int>>(indexs);
18// Create the filtering object
19 pcl::ExtractIndices<pcl::PointXYZ> extract;
20// Extract the inliers
21 extract.setInputCloud(cloud_0);
22 extract.setIndices(index_ptr);
23 extract.setNegative(false);//如果设为true,可以提取指定index之外的点云
24 extract.filter(*cloud_1);
25//法向量滤波后得到的点云信息
26 std::cout << *cloud_1 << std::endl;
上⾯第17⾏代码也可以写为:
pcl::IndicesPtr index_ptr = boost::make_shared<std::vector<int>>(indexs);
那么现在有pcl_base.h下的IndicesPtr,为指向int类型的vector的智能指针的索引;
PointIndices.h下的定义的数据结构 PointIndices ;那么将点云进⾏索引的指针可⽤以下:
1 pcl::PointIndices index_1;
2 pcl::IndicesPtr index_ptr = boost::make_shared<std::vector<int>>(index_1.indices);
综上所述,索引的使⽤可以如下所⽰:
1 std::vector<int > indexs;
2 pcl::PointIndices index_1;
3 pcl::IndicesPtr indices_plane(new std::vector <int>(indexs));
4 pcl::IndicesPtr index_ptr(new std::vector<int>(index_1.indices));
5 pcl::IndicesPtr index_ptr = boost::make_shared<std::vector<int>>(index_1.indices);
6 pcl::IndicesPtr index_ptr = boost::make_shared<std::vector<int>>(indexs);
7 //pcl::IndicesPtr index_ptr = boost::make_shared<std::vector<int>>(index_1);//这个index_1的索引不可⽤,因为index_1为PointIndices类,只能⽤上述第5⾏那样调⽤
利⽤ExtractIndices进⾏索引点云的提取:
1 pcl::ExtractIndices<pcl::PointXYZ> extract;
2 extract.setInputCloud(cloud_0);
3 extract.setIndices(index_ptr);
4 extract.setNegative(false);//如果设为true,可以提取指定index之外的点云
5 extract.filter(*cloud_1);
总结⼀下,这篇⽂章主要是解决在PCL使⽤过程中,⽤于⾃定义条件的点云提取,将点云的索引进⾏相应的存储在 vector<int> 数组中,利⽤
pcl::IndicesPtr index_ptr = boost::make_shared<std::vector<int>>(points_indices);
或者:
pcl::IndicesPtr index_ptr(new std::vector<int>(points_indices));
进⾏智能指针的转化,以利⽤ExtractIndices类中的 setIndices()函数进⾏点云的提取。

举例如下:
1//根据想要的点添加到⾃定义的indices_0的数组中,
2//std::vector<int> indices_0;
3 pcl::PointIndices indices_0;
4
5 indices_0.indices.push_back(0);
6 indices_0.indices.push_back(10);
7 indices_0.indices.push_back(100);
8//将⾃定义的indices_0数组进⾏智能指针的转化
9//pcl::IndicesPtr index_ptr_0 = boost::make_shared<std::vector<int>>(indices_0.indices);
10 pcl::IndicesPtr index_ptr_0(new std::vector<int>(indices_0.indices));
11
12//利⽤ExtractIndices根据索引进⾏点云的提取
13 pcl::ExtractIndices<pcl::PointXYZ> extract;
14 extract.setInputCloud(cloud_0);
15 extract.setIndices(index_ptr_0);
16 extract.setNegative(false);//如果设为true,可以提取指定index之外的点云
17 extract.filter(*cloud_1);
18
19//cloud_1索引0,1,2分别对应与cloud_0索引的0,10,100
20 std::cout << *cloud_1 << std::endl;
21 std::cout << cloud_1->at(0) << std::endl;
22 std::cout << cloud_1->at(1) << std::endl;
23 std::cout << cloud_1->at(2) << std::endl;
24
25 std::cout << *cloud_0 << std::endl;
26 std::cout << cloud_0->at(0) << std::endl;
27 std::cout << cloud_0->at(10) << std::endl;
28 std::cout << cloud_0->at(100) << std::endl;。

相关文档
最新文档