LoginSignup
3
2

More than 5 years have passed since last update.

PCLでpoint cloud中の点をランダムに間引く

Last updated at Posted at 2015-02-05

厳密ではないが,お手軽に点を間引きたい時に便利.

アイデア

cloud->points の実態はstl::vectorなので,コンテナに対する std::remove_if を適用する.
operator() が呼び出された時にランダムでTrue/Falseを返すクラスを作り,remove_if に投げる.

コード

まびく関数

#include <algorithm>

template <class T>
class isDelete
{
  float _random_sampling_percentage;
public:
  isDelete(float random_sampling_percentage)
  {
    srand((long)time(NULL)); 
    _random_sampling_percentage = random_sampling_percentage / 100.0;
  };
  bool operator() (T& val)
  {
    return (rand()/(double)RAND_MAX >= _random_sampling_percentage);
  }
};

void pointsReduction(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,
                     float random_sampling_percentage)
{
  isDelete<pcl::PointXYZ> randDel( random_sampling_percentage );

  cloud->points.erase( std::remove_if(cloud->points.begin(),
                                      cloud->points.end(),
                                      randDel  ), 
                       cloud->points.end() );
}

Ptr型のcloudを与えればOK.

呼び出し
pointsReduction(cloud, 10); // 10%に間引く
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2