のんびり動機付け

社会人2年目,動機付けを高く保ちたいブログ

PCLインストールとPLY出力

何したい

3次元点群の見栄えを良くするためにアウトライヤーを取り除きます.
このアウトライヤー除去は,PCL(Point Cloud Library)のフィルタリングを使う.
使い方を記録します.

環境

Ubunut14.04

インストール

Prebuilt binaries for Linux - Point Cloud Library (PCL)を参考にコマンドを入力します.

sudo add-apt-repository ppa:v-launchpad-jochen-sprickerhof-de/pcl
sudo apt-get update
sudo apt-get install libpcl-all

アウトライヤー除去のサンプルプログラム

除去のアルゴリズムはK-means法を使って,近傍の点の距離の平均値が大きいもの(離れている)を除去する.
下のサンプルは,PCL公式のフィルタリングでアウトライヤー除去をするプログラムの出力部分を
PLY形式でも出力するように改良した.

// statistical_removal.cpp
#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/statistical_outlier_removal.h>

int
main (int argc, char** argv)
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);

    // Fill in the cloud data
    pcl::PCDReader reader;
    // Replace the path below with the path where you saved your file
    reader.read<pcl::PointXYZ> ("table_scene_lms400.pcd", *cloud);

    std::cerr << "Cloud before filtering: " << std::endl; 
    std::cerr << *cloud << std::endl;

    // Create the filtering object
    pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
    sor.setInputCloud (cloud);
    sor.setMeanK (50);
    sor.setStddevMulThresh (1.0);
    sor.filter (*cloud_filtered);

    std::cerr << "Cloud after filtering: " << std::endl;
    std::cerr << *cloud_filtered << std::endl;

    pcl::PCDWriter writer;
    pcl::PLYWriter ply_writer;
    writer.write<pcl::PointXYZ> ("table_scene_lms400_inliers.pcd", *cloud_filtered, false);
    ply_writer.write<pcl::PointXYZ> ("table_scene_lms400_inliers.ply", *cloud_filtered, false);

    sor.setNegative (true);
    sor.filter (*cloud_filtered);
    writer.write<pcl::PointXYZ> ("table_scene_lms400_outliers.pcd", *cloud_filtered, false);
    ply_writer.write<pcl::PointXYZ> ("table_scene_lms400_outliers.ply", *cloud_filtered, false);

    return (0);
}

プログラムのコンパイルは,CMakeLists.txtを書いて

# CMakeLists.txt
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

project(statistical_removal)

find_package(PCL 1.2 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_definitions("-Wall -std=c++11")

add_executable (statistical_removal statistical_removal.cpp)
target_link_libraries (statistical_removal ${PCL_LIBRARIES})

cmakeしてmakeをする.

mkdir build
cd build
cmake ..
make