1. 程式人生 > >PCL 常用小知識

PCL 常用小知識

ati dex min [1] light identity office offic ase

時間計算

pcl中計算程序運行時間有很多函數,其中利用控制臺的時間計算

首先必須包含頭文件 #include <pcl/console/time.h>

#include <pcl/console/time.h>

pcl::console::TicToc time; 
time.tic();
//程序段
cout<<time.toc()/1000<<"s"<<endl;

pcl::PointCloud::Ptr和pcl::PointCloud的兩個類相互轉換

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
 
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudPointer(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ> cloud;
cloud = *cloudPointer;
cloudPointer = cloud.makeShared();

查找點雲的x,y,z的極值

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/common.h>
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile<pcl::PointXYZ> ("your_pcd_file.pcd", *cloud); pcl::PointXYZ minPt, maxPt; pcl::getMinMax3D (*cloud, minPt, maxPt);

如果知道需要保存點的索引,如何從原點雲中拷貝點到新點雲?

#include <pcl/io/pcd_io.h>
#include <pcl/common/impl/io.hpp>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
 
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile<pcl::PointXYZ>("C:\office3-after21111.pcd", *cloud);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudOut(new pcl::PointCloud<pcl::PointXYZ>);
std::vector<int > indexs = { 1, 2, 5 };
pcl::copyPointCloud(*cloud, indexs, *cloudOut);

取已知索引之外的點雲

pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
inliers->indices = pointIdxRadiusSearchMap;
//已知索引的index
std::vector<int> pointIdxRadiusSearchMap;

pcl::ExtractIndices<pcl::PointXYZ> extract; 
extract.setInputCloud(_laser3d_map);
extract.setIndices(inliers);                 
extract.setNegative(true);  //false: 篩選Index對應的點,true:過濾獲取Index之外的點                
extract.filter(*map_3d_2);

如何從點雲裏刪除和添加點?

#include <pcl/io/pcd_io.h>
#include <pcl/common/impl/io.hpp>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
 
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile<pcl::PointXYZ>("C:\office3-after21111.pcd", *cloud);
pcl::PointCloud<pcl::PointXYZ>::iterator index = cloud->begin();
cloud->erase(index);//刪除第一個
index = cloud->begin() + 5;
cloud->erase(cloud->begin());//刪除第5個
pcl::PointXYZ point = { 1, 1, 1 };
//在索引號為5的位置1上插入一點,原來的點後移一位
cloud->insert(cloud->begin() + 5, point);
cloud->push_back(point);//從點雲最後面插入一點
std::cout << cloud->points[5].x;//輸出1

如果刪除的點太多建議用上面的方法拷貝到新點雲,再賦值給原點雲,如果要添加很多點,建議先resize,然後用循環向點雲裏的添加。

如何對點雲進行全局或局部變換

#include <pcl/io/pcd_io.h>
#include <pcl/common/impl/io.hpp>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/common/transforms.h>

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile("path/.pcd",*cloud);
//全局變化
 //構造變化矩陣
Eigen::Matrix4f transform_1 = Eigen::Matrix4f::Identity();
float theta = M_PI/4;   //旋轉的度數,這裏是45度
transform_1 (0,0) = cos (theta);  //這裏是繞的Z軸旋轉
transform_1 (0,1) = -sin(theta);
transform_1 (1,0) = sin (theta);
transform_1 (1,1) = cos (theta);
       
//transform_1 (0,2) = 0.3;   //這樣會產生縮放效果
//transform_1 (1,2) = 0.6;
// transform_1 (2,2) = 1;

transform_1 (0,3) = 25; //這裏沿X軸平移
transform_1 (1,3) = 30;
transform_1 (2,3) = 380;
pcl::PointCloud<pcl::PointXYZ>::Ptr transform_cloud1 (new pcl::PointCloud<pcl::PointXYZ>);
pcl::transformPointCloud(*cloud,*transform_cloud1,transform_1);  //不言而喻
//第一個參數為輸入,第二個參數為輸入點雲中部分點集索引,第三個為存儲對象,第四個是變換矩陣。

pcl::transformPointCloud(*cloud,pcl::PointIndices indices,*transform_cloud1,matrix);

鏈接兩個點雲字段(兩點雲大小必須相同)

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile("/home/yxg/pcl/pcd/mid.pcd",*cloud);
pcl::NormalEstimation<pcl::PointXYZ,pcl::Normal> ne;
ne.setInputCloud(cloud);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>());
ne.setSearchMethod(tree);
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>()); 
ne.setKSearch(8);

//ne.setRadisuSearch(0.3);
ne.compute(*cloud_normals);    
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_nomal (new pcl::PointCloud<pcl::PointNormal>);
pcl::concatenateFields(*cloud,*cloud_normals,*cloud_with_nomal);

刪除無效點

#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/filter.h>
#include <pcl/io/pcd_io.h>
    
using namespace std;
typedef pcl::PointXYZRGBA point;
typedef pcl::PointCloud<point> CloudType;
    
int main (int argc,char **argv)
{
    CloudType::Ptr cloud (new CloudType);
    CloudType::Ptr output (new CloudType);
    
     pcl::io::loadPCDFile(argv[1],*cloud);
     cout<<"size is:"<<cloud->size()<<endl;
            
     vector<int> indices;
     pcl::removeNaNFromPointCloud(*cloud,*output,indices);
     cout<<"output size:"<<output->size()<<endl;
            
     pcl::io::savePCDFile("out.pcd",*output);
     return 0;
}     

xyzrgb格式轉換為xyz格式的點雲

#include <pcl/io/pcd_io.h>
#include <ctime>
#include <Eigen/Core>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>

using namespace std;
typedef pcl::PointXYZ point;
typedef pcl::PointXYZRGBA pointcolor;

int main(int argc,char **argv)
{
        pcl::PointCloud<pointcolor>::Ptr input (new pcl::PointCloud<pointcolor>);
        pcl::io::loadPCDFile(argv[1],*input);
        

        pcl::PointCloud<point>::Ptr output (new pcl::PointCloud<point>);
        int M = input->points.size();
        cout<<"input size is:"<<M<<endl;

        for (int i = 0;i <M;i++)
        {
                point p;
                p.x = input->points[i].x;
                p.y = input->points[i].y;
                p.z = input->points[i].z; 
                output->points.push_back(p);
        }
        output->width = 1;
        output->height = M;
        
        cout<< "size is"<<output->size()<<endl;
        pcl::io::savePCDFile("output.pcd",*output);

}

flann kdtree 查詢k近鄰

//平均密度計算
pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;  //創建一個快速k近鄰查詢,查詢的時候若該點在點雲中,則第一個近鄰點是其本身

kdtree.setInputCloud(cloud);
int k =2;
float everagedistance =0;
for (int i =0; i < cloud->size()/2;i++)
{
   vector<int> nnh ;
   vector<float> squaredistance;
       
   //pcl::PointXYZ p;
   //p = cloud->points[i];
   kdtree.nearestKSearch(cloud->points[i],k,nnh,squaredistance);
   everagedistance += sqrt(squaredistance[1]);
   //cout<<everagedistance<<endl;
}

everagedistance = everagedistance/(cloud->size()/2);
cout<<"everage distance is : "<<everagedistance<<endl;
        
 
#include <pcl/kdtree/kdtree_flann.h>

pcl::KdTreeFLANN<pcl::PointXYZ> kdtree; //創建KDtree
kdtree.setInputCloud (in_cloud);

pcl::PointXYZ searchPoint; //創建目標點,(搜索該點的近鄰)
searchPoint.x = 1;
searchPoint.y = 2;
searchPoint.z = 3;

//查詢近鄰點的個數
 int k = 10; //近鄰點的個數
std::vector<int> pointIdxNKNSearch(k); //存儲近鄰點集的索引
std::vector<float>pointNKNSquareDistance(k); //近鄰點集的距離
 if (kdtree.nearestKSearch(searchPoint,k,pointIdxNKNSearch,pointNKNSquareDistance)>0)
{
       for (size_t i = 0; i < pointIdxNKNSearch.size (); ++i)
             std::cout << "    "  <<   in_cloud->points[ pointIdxNKNSearch[i] ].x 
                            << " " << in_cloud->points[ pointIdxNKNSearch[i] ].y 
                            << " " <<in_cloud->points[ pointIdxNKNSearch[i] ].z 
                           << " (squared distance: " <<pointNKNSquareDistance[i] << ")<<std::endl;
}

//半徑為r的近鄰點
float radius = 40.0f;  //其實是求的40*40距離範圍內的點
std::vector<int> pointIdxRadiusSearch;  //存儲的對應的平方距離
std::vector<float> a;
if ( kdtree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, a) > 0 )
{
      for (size_t i = 0; i < pointIdxRadiusSearch.size (); ++i)
              std::cout << "    "  <<   in_cloud->points[ pointIdxRadiusSearch[i] ].x 
                            << " " <<in_cloud->points[ pointIdxRadiusSearch[i] ].y 
                            << " " << in_cloud->points[ pointIdxRadiusSearch[i] ].z 
                            << " (squared distance: " <<a[i] << ")" << std::endl;
}

關於ply文件

後綴命名為.ply格式文件,常用的點雲數據文件。ply文件不僅可以存儲數據,而且可以存儲網格數據. 用emacs打開一個ply文件,觀察表頭,如果表頭element face的值為0,則表示該文件為點雲文件,如果element face的值為某一正整數N,則表示該文件為網格文件,且包含N個網格.所以利用pcl讀取 ply 文件,不能一味用pcl::PointCloud<PointT>::Ptr cloud (new pcl::PointCloud<PintT>)來讀取。在讀取ply文件時候,首先要分清該文件是點雲還是網格類文件。如果是點雲文件,則按照一般的點雲類去讀取即可,官網例子,就是這樣。如果ply文件是網格類,則需要

pcl::PolygonMesh mesh;
pcl::io::loadPLYFile(argv[1],mesh);
pcl::io::savePLYFile("result.ply", mesh);

讀取。(官網例子之所以能成功,是因為它對模型進行了細分處理,使得網格變成了點)

計算點的索引

例如sift算法中,pcl無法直接提供索引(主要原因是sift點是通過計算出來的,在某些不同參數下,sift點可能並非源數據中的點,而是某些點的近似),若要獲取索引,則可利用以下函數:

void getIndices (pointcloud::Ptr cloudin, pointcloud keypoints, pcl::PointIndices::Ptr indices)
{
    pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
    kdtree.setInputCloud(cloudin);
    std::vector<float>pointNKNSquareDistance; //近鄰點集的距離
    std::vector<int> pointIdxNKNSearch;

    for (size_t i =0; i < keypoints.size();i++)
    {
        kdtree.nearestKSearch(keypoints.points[i],1,pointIdxNKNSearch,pointNKNSquareDistance);
        // cout<<"the distance is:"<<pointNKNSquareDistance[0]<<endl;
        // cout<<"the indieces is:"<<pointIdxNKNSearch[0]<<endl;
                
        indices->indices.push_back(pointIdxNKNSearch[0]);
                
   }

}

其思想就是:將原始數據插入到flann的kdtree中,尋找keypoints的最近鄰,如果距離等於0,則說明是同一點,提取索引即可.

計算質心

Eigen::Vector4f centroid;  //質心
pcl::compute3DCentroid(*cloud_smoothed,centroid); //估計質心的坐標

從網格提取頂點(將網格轉化為點)

#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/obj_io.h>
#include <pcl/PolygonMesh.h>
#include <pcl/point_cloud.h>
#include <pcl/io/vtk_lib_io.h>//loadPolygonFileOBJ所屬頭文件;
#include <pcl/io/vtk_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
using namespace pcl;

int main(int argc,char **argv) { pcl::PolygonMesh mesh; //pcl::io::loadPolygonFileOBJ(argv[1], mesh); pcl::io::loadPLYFile(argv[1],mesh); pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::fromPCLPointCloud2(mesh.cloud, *cloud); pcl::io::savePCDFileASCII("result.pcd", *cloud); return 0; }

以上代碼可以從.obj或.ply面片格式轉化為點雲類型。



 

PCL 常用小知識