1. 程式人生 > >關於pcl索引的使用

關於pcl索引的使用

最近開始動手做實驗,之前寫了一個小實驗利用到了PCL庫中的索引;

現在在寫利用PCL中的RegionGrowing類來分割生成面片,無論是迭代生成還是進行提取都需要用到pcl庫中定義的索引,

雖然搞的不是太明白,還是想寫下來來記錄自己的思路。

 

先看一下PCL是如何定義PointIndices的結構:

 1 struct PointIndices
 2   {
 3     PointIndices () : header (), indices ()
 4     {}
 5 
 6     ::pcl::PCLHeader header;
 7 
 8     std::vector<int> indices;
 9 
10     public:
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;
 3     for (auto i : *normals)
 4     {
 5         if (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的索引不知道能不能用,我測試一下再來修改

 

 

利用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);

 

若有不對之處,請留言指出!

&n