利用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的索引不可用,因為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;    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM