球面点云的生成以及pcl用已知参数模型提取点云

写在前面

1、本文内容
球面点云的生成,添加噪声后,pc实现用已知参数模型提取点云

2、平台
pcl 1.10.0
3、转载请注明出处:
https://blog.csdn.net/qq_41102371/article/details/127141692

code

model_outlier_removal.cpp

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/model_outlier_removal.h>
#include <pcl/visualization/pcl_visualizer.h>
using namespace std::chrono_literals;


pcl::visualization::PCLVisualizer::Ptr
simpleVis(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
    
    
  // --------------------------------------------
  // -----Open 3D viewer and add point cloud-----
  // --------------------------------------------
  pcl::visualization::PCLVisualizer::Ptr viewer(
      new pcl::visualization::PCLVisualizer("3D Viewer"));
  viewer->setBackgroundColor(0, 0, 0);
  viewer->addPointCloud<pcl::PointXYZ>(cloud, "sample cloud");
  viewer->setPointCloudRenderingProperties(
      pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
  // viewer->addCoordinateSystem (1.0, "global");
  viewer->initCameraParameters();
  return (viewer);
}


int
main ()
{
    
    
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_sphere_filtered (new pcl::PointCloud<pcl::PointXYZ>);

  // 1. Generate cloud data
  int noise_size = 100;
  int sphere_data_size = 1000;
  cloud->width = noise_size + sphere_data_size;
  cloud->height = 1;
  cloud->points.resize (cloud->width * cloud->height);
  // 1.1 Add noise
  for (std::size_t i = 0; i < noise_size; ++i)
  {
    
    
    cloud->points[i].x = 4 * rand () / (RAND_MAX + 1.0f)-2;
    cloud->points[i].y = 4 * rand () / (RAND_MAX + 1.0f)-2;
    cloud->points[i].z = 4 * rand () / (RAND_MAX + 1.0f)-2;
  }
  // 1.2 Add sphere:
  double rand_x1 = 1;
  double rand_x2 = 1;
  for (std::size_t i = noise_size; i < noise_size + sphere_data_size; ++i)
  {
    
    
    // See: http://mathworld.wolfram.com/SpherePointPicking.html
    while (pow (rand_x1, 2) + pow (rand_x2, 2) >= 1)
    {
    
    
      rand_x1 = (rand () % 100) / (50.0f) - 1;
      rand_x2 = (rand () % 100) / (50.0f) - 1;
    }
    double pre_calc = sqrt (1 - pow (rand_x1, 2) - pow (rand_x2, 2));
    cloud->points[i].x = 2 * rand_x1 * pre_calc;
    cloud->points[i].y = 2 * rand_x2 * pre_calc;
    cloud->points[i].z = 1 - 2 * (pow (rand_x1, 2) + pow (rand_x2, 2));
    rand_x1 = 1;
    rand_x2 = 1;
  }

  //std::cerr << "Cloud before filtering: " << std::endl;
  //for (std::size_t i = 0; i < cloud->points.size (); ++i)
  //  std::cout << "    " << cloud->points[i].x << " " << cloud->points[i].y << " " << cloud->points[i].z << std::endl;

  // 2. filter sphere:
  // 2.1 generate model:
  // modelparameter for this sphere:
  // position.x: 0, position.y: 0, position.z:0, radius: 1
  pcl::ModelCoefficients sphere_coeff;
  sphere_coeff.values.resize (4);
  sphere_coeff.values[0] = 0;
  sphere_coeff.values[1] = 0;
  sphere_coeff.values[2] = 0;
  sphere_coeff.values[3] = 1;

  pcl::ModelOutlierRemoval<pcl::PointXYZ> sphere_filter;
  sphere_filter.setModelCoefficients (sphere_coeff);
  sphere_filter.setThreshold (0.05);
  sphere_filter.setModelType (pcl::SACMODEL_SPHERE);
  sphere_filter.setInputCloud (cloud);
  sphere_filter.filter (*cloud_sphere_filtered);

  //std::cerr << "Sphere after filtering: " << std::endl;
  //for (std::size_t i = 0; i < cloud_sphere_filtered->points.size (); ++i)
  //  std::cout << "    " << cloud_sphere_filtered->points[i].x << " " << cloud_sphere_filtered->points[i].y << " " << cloud_sphere_filtered->points[i].z
  //      << std::endl;

  pcl::visualization::PCLVisualizer::Ptr viewer;
  viewer = simpleVis(cloud);
  while (!viewer->wasStopped()) {
    
    
    viewer->spinOnce(100);
    std::this_thread::sleep_for(100ms);
  }

  viewer = simpleVis(cloud_sphere_filtered);
  while (!viewer->wasStopped()) {
    
    
    viewer->spinOnce(100);
    std::this_thread::sleep_for(100ms);
  }
  
  return (0);
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

project(model_outlier_removal)

find_package(PCL 1.7 REQUIRED)

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

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

compile&run

cmake -DCMAKE_BUILD_TYPE=Release -DPCL_DIR="YOUR_PATH/PCL 1.10.0/cmake" -S ./ -B ./build
cmake --build ./build --config Release --target ALL_BUILD
.\build\Release\model_outlier_removal.exe

结果

提取前
在这里插入图片描述

提取后
在这里插入图片描述

参考

https://mathworld.wolfram.com/SpherePointPicking.html
http://sobereva.com/311

猜你喜欢

转载自blog.csdn.net/qq_41102371/article/details/127141692