pcl点云的离群点去除

pcl离群点的去除

点云及其文件下载:https://github.com/zhouyelihua/PointCloudProcessing
pcl的离群点计算是利用单点计算其周边的方差,利用方差的大小来判断其取舍


cmake文件

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(filter)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (filter filter.cpp)
target_link_libraries (filter ${PCL_LIBRARIES})

c++程序代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
#include
#include
#include
typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud PointCloudT;
int
    main (int argc, char** argv)
{
    std::vector<double> X_vector;
    std::vector<double> Y_vector;
    std::vector<double> Z_vector;
    std::vector<double> R_vector;
    std::vector<double> G_vector;
    std::vector<double> B_vector;
    //X_vector.reserve(100000);
    //Y_vector.reserve(100000);
    //Z_vector.reserve(100000);
    //R_vector.reserve(100000);
    //G_vector.reserve(100000);
    //B_vector.reserve(100000);
    //X_vector.reserve(100000);
    double x_temp, y_temp, z_temp, r_temp, g_temp, b_temp,U,V;

    std::fstream in;
    char filename[30] = {0};
    sprintf(filename, "data%d.asc", 0);
    /*fstream in(filename);*/
    in.open(filename, fstream::in);
    //cout<<"process "<
    //in.open("Data0.txt", fstream::in);
    in.seekg(0);
    int NumPoint;
    //int nnnn=10000;
    int i=0;
    while (!in.eof())// while(nnnn--)
//  while(i<10)
    {
        //cout<

        in >> x_temp >> y_temp >> z_temp >> r_temp >> g_temp >> b_temp>>U>>V;
        //cout<
        //cout<
        X_vector.push_back(x_temp);
        Y_vector.push_back(y_temp);
        Z_vector.push_back(z_temp);
        R_vector.push_back(r_temp);
        G_vector.push_back(g_temp);
        B_vector.push_back(b_temp);

    }
    cout<<"read done"<cout<cout<<"begin!\n";
    // Load point cloud from disk
    pcl::PointCloud::Ptr cloud_in (new pcl::PointCloud);

    // Fill in the cloud data
    cloud_in->width  = NumPoint;
    cloud_in->height = 1;
    cloud_in->points.resize (cloud_in->width * cloud_in->height);
    cout<<"cloud_in->points.size ()"<points.size ()<//for (size_t i = 0; i < cloud->points.size (); ++i)
    for (size_t i = 0; i < cloud_in->points.size (); ++i)
    {
        //cout<
        cloud_in->points[i].x = X_vector.at(i) ;
        cloud_in->points[i].y = Y_vector.at(i) ;
        cloud_in->points[i].z = Z_vector.at(i) ;
        cloud_in->points[i].r = (int)(R_vector.at(i)*255 );
        cloud_in->points[i].g = (int)(G_vector.at(i)*255 );
        cloud_in->points[i].b = (int)(B_vector.at(i)*255 );
        //cout<
    }
    X_vector.clear();
    Y_vector.clear();
    Z_vector.clear();
    R_vector.clear();
    G_vector.clear();
    B_vector.clear();

    pcl::PointCloud::Ptr cloud_out_filtered (new pcl::PointCloud);

    pcl::StatisticalOutlierRemovalsor;

    sor.setInputCloud(cloud_in);
    sor.setMeanK(100);
    sor.setStddevMulThresh(1.0);
    sor.filter(*cloud_out_filtered);
    //pcl::filters::GaussianKernelRGB sor_in;
    //sor_in.setInputCloud (cloud_in);
    // sor_in.setSigma(0.1);
    //sor_in.setThreshold(1.0);

    // sor_in.setThresholdRelativeToSigma(0.1f);
    //sor_in.initCompute();
    //sor_in.(cloud_out_filtered);

    std::fstream out2;
    out2.open("data1out.txt", fstream::out);

    for (size_t i = 0; i < cloud_out_filtered->points.size (); ++i)
    {
        //cout<
        out2<points[i].x<<"\t"
            <points[i].y<<"\t"
            <points[i].z<<"\t"
            <<(int)cloud_out_filtered->points[i].r<<"\t"
            <<(int)cloud_out_filtered->points[i].g<<"\t"
            <<(int)cloud_out_filtered->points[i].b<<"\n";
        //cout<
    }
    out2.close();
    return (0);
}

点云文件的格式

pcl点云的离群点去除_第1张图片

效果涉及实验室机密不展示了

你可能感兴趣的:(PCL)