ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++点云区域生长利用PCL库

2020-12-21 15:01:23  阅读:210  来源: 互联网

标签:gray PCL viewer C++ pcl 点云 lvl txt cloud


描述

利用PCL库进行点云区域生长

代码

代码中的部分参数,还是要根据你的点云数据的实际情况,进行更改的。

  • 举例子,代码中有这样两句话

    pass.setFilterFieldName ("z"); 
    pass.setFilterLimits (-1000, 1000); 
    

    按照相机的z轴方向过滤点,由于我的点单位是mm,所以是-1米到1米,如果你的点云单位是米,上面的参数很显然应该是-1和1

  • 完整的main.cpp

#include <iostream>

//点云需要的头文件
#include <pcl/point_types.h>
#include <pcl/io/ply_io.h>
#include <pcl/search/search.h> 
#include <pcl/search/kdtree.h> 
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/segmentation/region_growing.h> 
#include <pcl/visualization/cloud_viewer.h> 
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/passthrough.h>
#include <pcl/features/normal_3d.h>  


void drawPointCloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, std::string titleName)
{
    pcl::visualization::PCLVisualizer viewer (titleName);
    int v (0);

    viewer.createViewPort (0.0, 0.0, 1.0, 1.0, v);

    viewer.addCoordinateSystem(0.5);

    float bckgr_gray_level = 0.0;  // Black
    float txt_gray_lvl = 1.0 - bckgr_gray_level;

    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_in_color_h (cloud, (int) 255 * txt_gray_lvl, (int) 255 * txt_gray_lvl, (int) 255 * txt_gray_lvl);
    viewer.addPointCloud (cloud, cloud_in_color_h, "cloud_in_v1", v);

    viewer.addText (titleName, 10, 15, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "icp_info_1", v);

    viewer.setBackgroundColor (bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v);

    viewer.setCameraPosition (-3.68332, 2.94092, 5.71266, 0.289847, 0.921947, -0.256907, 0);
    viewer.setSize (1280, 1024);

    while (!viewer.wasStopped())
    {
        viewer.spinOnce();
    }
}

void regionGrowing(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_input)
{ 
    std::cout<<"[regionGrowing] input pointcloud size: "<<cloud_input->size() << std::endl; 
   
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_downsampled(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_processed(new pcl::PointCloud<pcl::PointXYZ>);

    //Use a voxelSampler to downsample
    pcl::VoxelGrid<pcl::PointXYZ> voxelSampler;
    voxelSampler.setInputCloud(cloud_input);
    voxelSampler.setLeafSize(0.1f, 0.1f, 0.1f);
    voxelSampler.filter(*cloud_downsampled);

    //Use a filter to reduce noise
    pcl::StatisticalOutlierRemoval<pcl::PointXYZ> statFilter;
    statFilter.setInputCloud(cloud_downsampled);
    statFilter.setMeanK(10);
    statFilter.setStddevMulThresh(0.2);
    statFilter.filter(*cloud_processed);
 
    // Create the normal estimation class, and pass the input dataset to it
    pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
    ne.setInputCloud(cloud_processed);
    // Create an empty kdtree representation, and pass it to the normal estimation object.
    // Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
    pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
    ne.setSearchMethod(tree);

    // Output datasets
    pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
    // setRadiusSearch and setKSearch are two methods of searching, both are useful, we just use setKSearch
    // ne.setRadiusSearch(0.01); // Use all neighbors in a sphere of radius 1cm
    ne.setKSearch(10); 
    // Compute the features
    ne.compute(*normals);

    pcl::IndicesPtr indices (new std::vector <int>); 
    pcl::PassThrough<pcl::PointXYZ> pass; 
    pass.setInputCloud (cloud_processed); 
    pass.setFilterFieldName ("z"); 
    pass.setFilterLimits (-1000, 1000); 
    pass.filter (*indices); 


    //聚类对象
    pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg; 
    reg.setMinClusterSize (5000); //最小聚类的点数 50
    reg.setMaxClusterSize (1000000);   //最大聚类的点数 1000000
    reg.setSearchMethod (tree);  //搜索方式
    reg.setNumberOfNeighbours (30); //设置搜索的邻域点的个数 30
    reg.setInputCloud (cloud_processed); //输入点云
    //reg.setIndices (indices); 
    reg.setInputNormals (normals); //输入的法线
    reg.setSmoothnessThreshold (50.0 / 180.0 * M_PI); //设置平滑度  3 
    reg.setCurvatureThreshold (1.0); //设置曲率的阈值
    std::vector <pcl::PointIndices> clusters; 
    reg.extract (clusters); 
    
    
    //输出点云簇的个数
    std::cout << "Number of clusters is equal to " << clusters.size () << std::endl; 
    std::cout << "First cluster has " << clusters[0].indices.size () << " points." << endl;
    //输出每个点云簇的点数 
    for (int i = 0 ; i<clusters.size() ; i++){
        std::cout << "ID = "<<i << " cluster has " << clusters[i].indices.size() << " points." << endl; 
    }

    
    //输出第一个点云簇的前10个点的序号
    int counter = 0;
    std::cout<<"ID = 0 cluster first 10 points id are : ";
    while (counter < clusters[0].indices.size ()) 
    { 
        std::cout <<clusters[0].indices[counter] << ", ";
        counter++; 
        if (counter == 10) 
        {
            break;
        }
    } 
    std::cout << std::endl; 


    // 用不同颜色划分各个点云簇
    pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud (); 
    pcl::visualization::CloudViewer viewer("Cluster viewer");
    viewer.showCloud(colored_cloud);
    while (!viewer.wasStopped())
    {
        boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    }
    

    // 把想要的某一点云簇画出来
    int need = 1;
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_objectonly(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::copyPointCloud(*cloud_processed, clusters[need].indices, *cloud_objectonly);
    
    pcl::visualization::PCLVisualizer viewer_part ("Final1 with Visualization");
    int v1 (0);
    int v2 (1);
    viewer_part.createViewPort (0.0, 0.0, 0.5, 1.0, v1);
    viewer_part.createViewPort (0.5, 0.0, 1.0, 1.0, v2);

    viewer_part.addCoordinateSystem(0.5);

    float bckgr_gray_level = 0.0;  // Black
    float txt_gray_lvl = 1.0 - bckgr_gray_level;

    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_in_color_h (cloud_processed, (int) 255 * txt_gray_lvl, (int) 255 * txt_gray_lvl, (int) 255 * txt_gray_lvl);
    viewer_part.addPointCloud (cloud_processed, cloud_in_color_h, "cloud_in_v1", v1);

    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_tr_color_h (cloud_objectonly, 20, 180, 20);
    viewer_part.addPointCloud (cloud_objectonly, cloud_tr_color_h, "cloud_tr_v1", v2);

    viewer_part.addText ("The Fucking Original Point Cloud", 10, 15, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "icp_info_1", v1);
    viewer_part.addText ("The Fucking Processed Point Cloud", 10, 15, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "icp_info_2", v2);

    viewer_part.setBackgroundColor (bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v1);
    viewer_part.setBackgroundColor (bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v2);

    viewer_part.setCameraPosition (-3.68332, 2.94092, 5.71266, 0.289847, 0.921947, -0.256907, 0);
    viewer_part.setSize (1280, 1024);

    while (!viewer_part.wasStopped())
    {
        viewer_part.spinOnce();
    }

    
    // 画出点云和法线
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer_normal(new pcl::visualization::PCLVisualizer("3D Viewer"));
    viewer_normal->setBackgroundColor(0, 0, 0);
    viewer_normal->addPointCloud<pcl::PointXYZRGB>(colored_cloud, "sample cloud");
    viewer_normal->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
    viewer_normal->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal>(colored_cloud, normals, 10, 0.2, "normals");
    viewer_normal->addCoordinateSystem(1.0);
    viewer_normal->initCameraParameters();
    viewer_normal->setCameraPosition (-3.68332, 2.94092, 5.71266, 0.289847, 0.921947, -0.256907, 0);
    while (!viewer_normal->wasStopped())
    {
        viewer_normal->spinOnce(100);
        boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    }
}

pcl::PointCloud<pcl::PointXYZ>::Ptr loadPointCloud(std::string path)
{

    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    if (pcl::io::loadPLYFile<pcl::PointXYZ>(path, *cloud) == -1) 
    {       
        PCL_ERROR("Couldnot read file.\n");
        return 0;
    }
    std::cout<<"pointcloud size: "<<cloud->width<<" * "<<cloud->height << std::endl; 
    return cloud;
}

int main(int argc, char** argv)
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
    cloud = loadPointCloud("../standard.ply");

    regionGrowing(cloud);

	return 1;
}

附赠的CMakeLists.txt

cmake_minimum_required(VERSION 2.8.7)
project(test)

find_package(PCL 1.5 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fexceptions -frtti -pthread -O3 -march=core2")

set(ROOT 		"${CMAKE_CURRENT_SOURCE_DIR}/")

include_directories(
    ${ROOT}
    ${ROOT}/include
)

file(GLOB SOURCES
    "*.cpp"
    )

link_directories(
    ${ROOT}/lib
)

add_executable(test ${SOURCES})
target_link_libraries(test ${PCL_LIBRARIES})

标签:gray,PCL,viewer,C++,pcl,点云,lvl,txt,cloud
来源: https://blog.csdn.net/weixin_42156097/article/details/111474829

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有