1. 头文件 function.h
#pragma once
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/common/impl/io.hpp>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/project_inliers.h>
#include <pcl/console/parse.h>
#include <pcl/io/io.h>
#include <pcl/io/ply_io.h>
#include <pcl/common/io.h>
#include <pcl/common/centroid.h>
#include <pcl/common/common.h>
#include <pcl/common/transforms.h>
#include <pcl/filters/passthrough.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/filters/conditional_removal.h>
#include <pcl/range_image/range_image.h>
#include <pcl/features/normal_3d.h>
#include <pcl/sample_consensus/ransac.h>
#include <pcl/sample_consensus/sac_model_plane.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/keypoints/uniform_sampling.h>
#include <pcl/compression/octree_pointcloud_compression.h>
#include <pcl/filters/model_outlier_removal.h>
#include <pcl/filters/convolution_3d.h>
#include <pcl/filters/bilateral.h>
#include <pcl/filters/fast_bilateral.h>
#include <pcl/filters/crop_hull.h>
#include <pcl/surface/concave_hull.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/segmentation/conditional_euclidean_clustering.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/segmentation/region_growing_rgb.h>
#include <pcl/segmentation/min_cut_segmentation.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/features/don.h>
#include <pcl/segmentation/impl/extract_clusters.hpp>
#include <pcl/segmentation/supervoxel_clustering.h>
#include <pcl/features/boundary.h>
#include <pcl/console/time.h>
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/imgproc/types_c.h"
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include "vtkAutoInit.h"
#include <vector>
#include <string>
#include <Windows.h>
#include <iostream>
#include <stdio.h>
class PCLfunction
{
public:
double getMaxValue(pcl::PointXYZ p1, pcl::PointXYZ p2);
double maxLen;
public:
void visualization(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, int mode);
void visualization1(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud);
void visualization2wd(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2);
void visualization2wd1(pcl::PointCloud<pcl::PointXYZI>::Ptr cloud1, pcl::PointCloud<pcl::PointXYZI>::Ptr cloud2);
void visualization2wd2(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1, pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud2);
void visualization2wd3(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud1, pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud2);
void visualizationCon(pcl::PointCloud<pcl::PointXYZ>::Ptr data[]);
void PassThrough(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, float min, float max, int mode);
void VoxelGrid(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, float a);
void UniformSampling(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, double r);
void StatisticalOutlierRemoval(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, int k, double sigma);
void RadiusOutlierRemoval(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, double r, int n);
void ConditionalRemoval(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, int mode, double a, double b);
void ProjectInliers(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud);
void ModelOutlierRemoval(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, float thresh);
void Gaussian(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud);
void BilateralFilter(pcl::PointCloud<pcl::PointXYZI>::Ptr incloud, pcl::PointCloud<pcl::PointXYZI>::Ptr outcloud, double sigma_s, double sigma_r);
void ExtractIndices(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, std::vector<int> index);
void CropHull(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud);
void RANSAC(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud);
void EuclideanClusterExtraction(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud);
void RegionGrowing(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::Normal>::Ptr cloud_normals, pcl::PointCloud<pcl::PointXYZRGB>::Ptr outcloud);
void RegionGrowingRGB(pcl::PointCloud<pcl::PointXYZRGB>::Ptr incloud, pcl::PointCloud<pcl::PointXYZRGB>::Ptr outcloud);
void MinCutSegmentation(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZRGB>::Ptr outcloud);
void DifferenceOfNormalsEstimation(pcl::PointCloud<pcl::PointXYZRGB>::Ptr incloud);
void SupervoxelClustering(pcl::PointCloud<pcl::PointXYZRGBA>::Ptr incloud);
void NormalEstimation(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::Normal>::Ptr cloud_normals);
void EstimateBoundary(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::Normal>::Ptr normals, pcl::PointCloud<pcl::Boundary>& boundaries);
void CreateCloudFromTxt(const std::string& file_path, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud);
void CircularColumn(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::Normal>::Ptr normals, pcl::PointCloud<pcl::PointXYZ > ::Ptr outcloud);
void Circle2D(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud);
void removeNan(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud);
float kdTreeSearch(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, int k);
};
2. 源文件 function.cpp
- 能够实现一些较为基础的可视化/滤波/分割等操作,代码也基本都注释,要用哪个模块的功能直接将函数复制出来即可
- 由于是之前刚接触时写的也许会存在一些不好的地方还望指出
- 直接运行switch语句选择操作时先将点云的路径地址更改为自己的,其次注意输入输出参数的点云类型要一致,也就是说弹出窗口让选择点云时应当仔细观察点云的类型是否相同,不是每个选项组合都能出结果
#include "function.h"
float colors[] = {
255, 0, 0,
0, 255, 0,
0, 0, 255,
255, 255, 0,
0, 255, 255,
255, 0, 255,
255, 255, 255,
255, 128, 0,
255, 153, 255,
51, 153, 255,
153, 102, 51,
128, 51, 153,
153, 153, 51,
163, 38, 51,
204, 153, 102,
204, 224, 255,
128, 179, 255,
206, 255, 0,
255, 204, 204,
204, 255, 153,
};
pcl::PointXYZ p_min, p_max;
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
pcl::search::KdTree<pcl::PointXYZI>::Ptr tree1(new pcl::search::KdTree<pcl::PointXYZI>);
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree2(new pcl::search::KdTree<pcl::PointXYZRGB>);
bool enforceIntensitySimilarity(const pcl::PointXYZINormal& point_a, const pcl::PointXYZINormal& point_b, float squared_distance)
{
if (fabs((float)point_a.intensity - (float)point_b.intensity) < 5.0f)
return (true);
else
return (false);
}
bool enforceCurvatureOrIntensitySimilarity(const pcl::PointXYZINormal& point_a, const pcl::PointXYZINormal& point_b, float squared_distance)
{
Eigen::Map<const Eigen::Vector3f> point_a_normal = point_a.getNormalVector3fMap(), point_b_normal = point_b.getNormalVector3fMap();
if (fabs((float)point_a.intensity - (float)point_b.intensity) < 5.0f)
return (true);
if (fabs(point_a_normal.dot(point_b_normal)) < 0.05)
return (true);
return (false);
}
bool customRegionGrowing(const pcl::PointXYZINormal& point_a, const pcl::PointXYZINormal& point_b, float squared_distance)
{
Eigen::Map<const Eigen::Vector3f> point_a_normal = point_a.getNormalVector3fMap(), point_b_normal = point_b.getNormalVector3fMap();
if (squared_distance < 10000)
{
if (fabs((float)point_a.intensity - (float)point_b.intensity) < 8.0f)
return (true);
if (fabs(point_a_normal.dot(point_b_normal)) < 0.06)
return (true);
}
else
{
if (fabs((float)point_a.intensity - (float)point_b.intensity) < 3.0f)
return (true);
}
return (false);
}
void PCLfunction::visualization(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,int mode)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("点云显示"));
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0, 255, 0);
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud, "x");
viewer->addPointCloud<pcl::PointXYZ>(cloud, fildColor, "example");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "example");
pcl::getMinMax3D(*cloud, p_min, p_max);
maxLen = getMaxValue(p_max, p_min);
switch (mode)
{
case 1:
viewer->setCameraPosition(0.5*(p_min.x + p_max.x), 0.5*(p_min.y + p_max.y), p_max.z + 2 * maxLen, 0.5*(p_min.x + p_max.x), 0.5*(p_min.y + p_max.y), p_max.z, 0, 1, 0);
break;
case 2:
viewer->setCameraPosition(0.5*(p_min.x + p_max.x), 0.5*(p_min.y + p_max.y), p_min.z - 2 * maxLen, 0.5*(p_min.x + p_max.x), 0.5*(p_min.y + p_max.y), p_min.z, 0, 1, 0);
break;
case 3:
viewer->setCameraPosition(0.5*(p_min.x + p_max.x), p_min.y - 2 * maxLen, 0.5*(p_min.z + p_max.z), 0.5*(p_min.x + p_max.x), p_min.y, 0.5*(p_min.z + p_max.z), 0, 0, 1);
break;
case 4:
viewer->setCameraPosition(0.5*(p_min.x + p_max.x), p_max.y + 2 * maxLen, 0.5*(p_min.z + p_max.z), 0.5*(p_min.x + p_max.x), p_min.y, 0.5*(p_min.z + p_max.z), 0, 0, 1);
break;
case 5:
viewer->setCameraPosition(p_min.x - 2 * maxLen, 0.5*(p_min.y + p_max.y), 0.5*(p_min.z + p_max.z), p_max.x, 0.5*(p_min.y + p_max.y), 0.5*(p_min.z + p_max.z), 0, 0, 1);
break;
case 6:
viewer->setCameraPosition(p_max.x + 2 * maxLen, 0.5*(p_min.y + p_max.y), 0.5*(p_min.z + p_max.z), p_max.x, 0.5*(p_min.y + p_max.y), 0.5*(p_min.z + p_max.z), 0, 0, 1);
break;
default:
viewer->resetCamera();
}
viewer->setCameraFieldOfView(0.5);
viewer->setCameraClipDistances(0.1, 572);
viewer->setCameraPosition(-1, 0, 0, 0, 0, 0, 0, 0, 1);
while (!viewer->wasStopped()) {
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
double PCLfunction::getMaxValue(pcl::PointXYZ p1, pcl::PointXYZ p2)
{
double max = 0;
if (p1.x - p2.x > p1.y - p2.y)
{
max = p1.x - p2.x;
}
else
{
max = p1.y - p2.y;
}
if (max < p1.z - p2.z)
{
max = p1.z - p2.z;
}
return max;
}
void PCLfunction::visualization1(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addCoordinateSystem(1.0);
viewer->resetCamera();
viewer->initCameraParameters();
while (!viewer->wasStopped()) {
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
void PCLfunction::visualization2wd(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> MView(new pcl::visualization::PCLVisualizer("处理效果"));
int v1(0);
MView->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
MView->setBackgroundColor(0, 0, 0, v1);
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud1, "x");
MView->addText("before", 10, 10, "v1_text", v1);
int v2(0);
MView->createViewPort(0.5, 0.0, 1, 1.0, v2);
MView->setBackgroundColor(0, 0, 0, v2);
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor2(cloud2, "x");
MView->addText("after", 10, 10, "v2_text", v2);
MView->addPointCloud<pcl::PointXYZ>(cloud1, fildColor, "sample cloud", v1);
MView->addPointCloud<pcl::PointXYZ>(cloud2, fildColor2, "cloud", v2);
MView->addCoordinateSystem(0.1);
MView->initCameraParameters();
MView->resetCamera();
MView->spin();
while (!MView->wasStopped())
{
MView->spinOnce(1000);
boost::this_thread::sleep(boost::posix_time::microseconds(1000));
}
}
void PCLfunction::visualization2wd1(pcl::PointCloud<pcl::PointXYZI>::Ptr cloud1, pcl::PointCloud<pcl::PointXYZI>::Ptr cloud2)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> MView(new pcl::visualization::PCLVisualizer("处理效果"));
int v1(0);
MView->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
MView->setBackgroundColor(0, 0, 0, v1);
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZI> fildColor(cloud1, "x");
MView->addText("before", 10, 10, "v1_text", v1);
int v2(0);
MView->createViewPort(0.5, 0.0, 1, 1.0, v2);
MView->setBackgroundColor(0.2, 0.2, 0.2, v2);
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZI> fildColor2(cloud2, "x");
MView->addText("after", 10, 10, "v2_text", v2);
MView->addPointCloud<pcl::PointXYZI>(cloud1, fildColor, "sample cloud", v1);
MView->addPointCloud<pcl::PointXYZI>(cloud2, fildColor2, "cloud", v2);
MView->addCoordinateSystem(0.1);
MView->initCameraParameters();
MView->resetCamera();
MView->spin();
while (!MView->wasStopped())
{
MView->spinOnce(1000);
boost::this_thread::sleep(boost::posix_time::microseconds(1000));
}
}
void PCLfunction::visualization2wd2(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1, pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud2)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> MView(new pcl::visualization::PCLVisualizer("处理效果"));
int v1(0);
MView->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
MView->setBackgroundColor(0, 0, 0, v1);
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud1, "x");
MView->addText("before", 10, 10, "v1_text", v1);
int v2(0);
MView->createViewPort(0.5, 0.0, 1, 1.0, v2);
MView->setBackgroundColor(0.2, 0.2, 0.2, v2);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud2);
MView->addText("after", 10, 10, "v2_text", v2);
MView->addPointCloud<pcl::PointXYZ>(cloud1, fildColor, "sample cloud", v1);
MView->addPointCloud<pcl::PointXYZRGB>(cloud2, rgb, "cloud", v2);
MView->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud", v1);
MView->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud", v2);
MView->addCoordinateSystem(0.1);
MView->initCameraParameters();
MView->resetCamera();
MView->spin();
while (!MView->wasStopped())
{
MView->spinOnce(1000);
boost::this_thread::sleep(boost::posix_time::microseconds(1000));
}
}
void PCLfunction::visualization2wd3(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud1, pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud2)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> MView(new pcl::visualization::PCLVisualizer("处理效果"));
int v1(0);
MView->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
MView->setBackgroundColor(0, 0, 0, v1);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud2);
MView->addText("before", 10, 10, "v1_text", v1);
int v2(0);
MView->createViewPort(0.5, 0.0, 1, 1.0, v2);
MView->setBackgroundColor(0.2, 0.2, 0.2, v2);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb1(cloud2);
MView->addText("after", 10, 10, "v2_text", v2);
MView->addPointCloud<pcl::PointXYZRGB>(cloud1, rgb, "sample cloud", v1);
MView->addPointCloud<pcl::PointXYZRGB>(cloud2, rgb1, "cloud", v2);
MView->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud", v1);
MView->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud", v2);
MView->addCoordinateSystem(0.1);
MView->initCameraParameters();
MView->resetCamera();
MView->spin();
while (!MView->wasStopped())
{
MView->spinOnce(1000);
boost::this_thread::sleep(boost::posix_time::microseconds(1000));
}
}
void PCLfunction::visualizationCon(pcl::PointCloud<pcl::PointXYZ>::Ptr data[])
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
viewer->addPointCloud<pcl::PointXYZ>(data[0], "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
viewer->addCoordinateSystem(1.0);
viewer->resetCamera();
viewer->initCameraParameters();
int i = 0;
while (!viewer->wasStopped()) {
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(data[i], "x");
viewer->updatePointCloud(data[i], fildColor, "sample cloud");
i++;
if (i == sizeof(data) / sizeof(data[0])) i = 0;
viewer->spinOnce(5000);
}
}
void PCLfunction::PassThrough(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, float min, float max, int mode)
{
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(incloud);
switch (mode)
{
case 1:
pass.setFilterFieldName("x");
break;
case 2:
pass.setFilterFieldName("y");
break;
case 3:
pass.setFilterFieldName("z");
break;
}
pass.setFilterLimits(min, max);
pass.filter(*outcloud);
};
void PCLfunction::VoxelGrid(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, float a)
{
pcl::PCLPointCloud2::Ptr cloud_blob(new pcl::PCLPointCloud2);
pcl::PCLPointCloud2::Ptr cloud_blob2(new pcl::PCLPointCloud2);
pcl::toPCLPointCloud2(*incloud, *cloud_blob);
pcl::VoxelGrid<pcl::PCLPointCloud2> vox;
vox.setInputCloud(cloud_blob);
vox.setLeafSize(a, a, a);
vox.filter(*cloud_blob2);
pcl::fromPCLPointCloud2(*cloud_blob2, *outcloud);
}
void PCLfunction::UniformSampling(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, double r)
{
pcl::UniformSampling<pcl::PointXYZ> filter;
filter.setInputCloud(incloud);
filter.setRadiusSearch(r);
filter.filter(*outcloud);
}
void PCLfunction::StatisticalOutlierRemoval(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, int k, double sigma)
{
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
sor.setInputCloud(incloud);
sor.setMeanK(k);
sor.setStddevMulThresh(sigma);
sor.filter(*outcloud);
sor.setNegative(true);
}
void PCLfunction::RadiusOutlierRemoval(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, double r, int n)
{
pcl::RadiusOutlierRemoval<pcl::PointXYZ> outrem;
outrem.setInputCloud(incloud);
outrem.setRadiusSearch(r);
outrem.setMinNeighborsInRadius(n);
outrem.filter(*outcloud);
}
void PCLfunction::ConditionalRemoval(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud,int mode, double a, double b)
{
pcl::ConditionAnd<pcl::PointXYZ>::Ptr range_cond(new pcl::ConditionAnd<pcl::PointXYZ>());
switch (mode)
{
case 1:
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZ>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZ>("x", pcl::ComparisonOps::GT, a)));
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZ>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZ>("x", pcl::ComparisonOps::LT, b)));
case 2:
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZ>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZ>("y", pcl::ComparisonOps::GT, a)));
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZ>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZ>("y", pcl::ComparisonOps::LT, b)));
case 3:
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZ>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZ>("z", pcl::ComparisonOps::GT, a)));
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZ>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZ>("z", pcl::ComparisonOps::LT, b)));
}
pcl::ConditionalRemoval<pcl::PointXYZ> condrem;
condrem.setCondition(range_cond);
condrem.setInputCloud(incloud);
condrem.setKeepOrganized(true);
condrem.filter(*outcloud);
}
void PCLfunction::ProjectInliers(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud)
{
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
coefficients->values.resize(4);
coefficients->values[0] = coefficients->values[1] = 0;
coefficients->values[2] = 1.0;
coefficients->values[3] = 0;
pcl::ProjectInliers<pcl::PointXYZ> proj;
proj.setModelType(pcl::SACMODEL_PLANE);
proj.setInputCloud(incloud);
proj.setModelCoefficients(coefficients);
proj.filter(*outcloud);
}
void PCLfunction::ModelOutlierRemoval(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, float thresh)
{
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(thresh);
sphere_filter.setModelType(pcl::SACMODEL_SPHERE);
sphere_filter.setInputCloud(incloud);
sphere_filter.filter(*outcloud);
}
void PCLfunction::Gaussian(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud)
{
pcl::filters::GaussianKernel<pcl::PointXYZ, pcl::PointXYZ> kernel;
kernel.setSigma(5);
kernel.setThresholdRelativeToSigma(5);
kernel.setThreshold(5);
pcl::filters::Convolution3D<pcl::PointXYZ, pcl::PointXYZ, pcl::filters::GaussianKernel<pcl::PointXYZ, pcl::PointXYZ> > convolution;
convolution.setKernel(kernel);
convolution.setInputCloud(incloud);
convolution.setNumberOfThreads(8);
convolution.setSearchMethod(tree);
convolution.setRadiusSearch(0.1);
convolution.convolve(*outcloud);
}
void PCLfunction::BilateralFilter(pcl::PointCloud<pcl::PointXYZI>::Ptr incloud, pcl::PointCloud<pcl::PointXYZI>::Ptr outcloud, double sigma_s, double sigma_r)
{
pcl::BilateralFilter<pcl::PointXYZI> nf;
nf.setInputCloud(incloud);
nf.setSearchMethod(tree1);
nf.setHalfSize(sigma_s);
nf.setStdDev(sigma_r);
nf.filter(*outcloud);
}
void PCLfunction::ExtractIndices(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud, std::vector<int> index)
{
boost::shared_ptr<std::vector<int>> index_ptr = boost::make_shared<std::vector<int>>(index);
pcl::ExtractIndices<pcl::PointXYZ> extract;
extract.setInputCloud(incloud);
extract.setIndices(index_ptr);
extract.setNegative(false);
extract.filter(*outcloud);
}
void PCLfunction::CropHull(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr boundingbox_ptr(new pcl::PointCloud<pcl::PointXYZ>);
boundingbox_ptr->push_back(pcl::PointXYZ(0.1, 0.1, 0));
boundingbox_ptr->push_back(pcl::PointXYZ(0.1, -0.1, 0));
boundingbox_ptr->push_back(pcl::PointXYZ(-0.1, 0.1, 0));
boundingbox_ptr->push_back(pcl::PointXYZ(-0.1, -0.1, 0));
boundingbox_ptr->push_back(pcl::PointXYZ(0.15, 0.1, 0));
pcl::ConvexHull<pcl::PointXYZ> hull;
hull.setInputCloud(boundingbox_ptr);
hull.setDimension(2);
std::vector<pcl::Vertices> polygons;
pcl::PointCloud<pcl::PointXYZ>::Ptr
surface_hull(new pcl::PointCloud<pcl::PointXYZ>);
hull.reconstruct(*surface_hull, polygons);
pcl::PointCloud<pcl::PointXYZ>::Ptr objects(new pcl::PointCloud<pcl::PointXYZ>);
pcl::CropHull<pcl::PointXYZ> bb_filter;
bb_filter.setDim(2);
bb_filter.setInputCloud(incloud);
bb_filter.setHullIndices(polygons);
bb_filter.setHullCloud(surface_hull);
bb_filter.filter(*outcloud);
}
void PCLfunction::RANSAC(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud)
{
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
pcl::SACSegmentation<pcl::PointXYZ> seg;
seg.setOptimizeCoefficients(true);
seg.setModelType(pcl::SACMODEL_PLANE);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setDistanceThreshold(0.01);
seg.setInputCloud(incloud);
seg.segment(*inliers, *coefficients);
pcl::copyPointCloud<pcl::PointXYZ>(*incloud, *inliers, *outcloud);
}
void PCLfunction::EuclideanClusterExtraction(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud)
{
pcl::PCDWriter writer;
std::vector<pcl::PointIndices> cluster_indices;
pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
ec.setClusterTolerance(0.5);
ec.setMinClusterSize(999);
ec.setMaxClusterSize(999999);
ec.setSearchMethod(tree);
ec.setInputCloud(incloud);
ec.extract(cluster_indices);
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D viewer"));
int v1(0);
viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
viewer->setBackgroundColor(0.1, 0.1, 0.1, v1);
viewer->addText("before", 10, 10, "v1 text", v1);
viewer->addPointCloud<pcl::PointXYZ>(incloud, "cloud_in", v1);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "cloud_in", v1);
int v2(0);
viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);
viewer->setBackgroundColor(0.3, 0.3, 0.3, v2);
viewer->addText("after", 10, 10, "v2 text", v2);
int j = 0;
for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin(); it != cluster_indices.end(); ++it)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster(new pcl::PointCloud<pcl::PointXYZ>);
for (std::vector<int>::const_iterator pit = it->indices.begin(); pit != it->indices.end(); pit++)
{
cloud_cluster->points.push_back(incloud->points[*pit]);
}
cloud_cluster->width = cloud_cluster->points.size();
cloud_cluster->height = 1;
cloud_cluster->is_dense = true;
std::stringstream ss;
ss << "cloud_cluster_" << j << ".pcd";
writer.write<pcl::PointXYZ>(ss.str(), *cloud_cluster, false);
cout << "-----" << ss.str() << "详情-----" << endl;
cout << *cloud_cluster << endl;
viewer->addPointCloud<pcl::PointXYZ>(cloud_cluster, ss.str(), v2);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, ss.str(), v2);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, colors[j * 3]/255 , colors[j * 3 + 1]/255, colors[j * 3 + 2]/255 , ss.str(), v2);
j++;
}
viewer->resetCamera();
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
#if 0
void PCLfunction::ConditionalEuclideanClustering(pcl::PointCloud<pcl::PointXYZI>::Ptr incloud, pcl::PointCloud<pcl::PointXYZI>::Ptr outcloud,int Method)
{
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_out(new pcl::PointCloud<pcl::PointXYZI>);
pcl::PointCloud<pcl::PointXYZINormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointXYZINormal>);
pcl::IndicesClustersPtr clusters(new pcl::IndicesClusters), small_clusters(new pcl::IndicesClusters), large_clusters(new pcl::IndicesClusters);
pcl::console::TicToc tt;
float Leaf = 80.0;
double Radius = 300.0;
std::cerr << "Downsampling...\n", tt.tic();
pcl::VoxelGrid<pcl::PointXYZI> vg;
vg.setInputCloud(incloud);
vg.setLeafSize(Leaf, Leaf, Leaf);
vg.setDownsampleAllData(true);
vg.filter(*cloud_out);
std::cerr << ">> Done: " << tt.toc() << " ms, " << cloud_out->points.size() << " points\n";
std::cerr << "Computing normals...\n", tt.tic();
pcl::copyPointCloud(*cloud_out, *cloud_with_normals);
pcl::NormalEstimation<pcl::PointXYZI, pcl::PointXYZINormal> ne;
ne.setInputCloud(cloud_out);
ne.setSearchMethod(tree1);
ne.setRadiusSearch(Radius);
ne.compute(*cloud_with_normals);
std::cerr << ">> Done: " << tt.toc() << " ms\n";
std::cerr << "Segmenting to clusters...\n", tt.tic();
pcl::ConditionalEuclideanClustering<pcl::PointXYZINormal> cec(true);
cec.setInputCloud(cloud_with_normals);
switch (Method)
{
case 1:
cec.setConditionFunction(&enforceIntensitySimilarity);
break;
case 2:
cec.setConditionFunction(&enforceCurvatureOrIntensitySimilarity);
break;
case 3:
cec.setConditionFunction(&customRegionGrowing);
break;
default:
cec.setConditionFunction(&customRegionGrowing);
break;
}
cec.setClusterTolerance(500.0);
cec.setMinClusterSize(cloud_with_normals->points.size() / 1000);
cec.setMaxClusterSize(cloud_with_normals->points.size() / 5);
cec.segment(*clusters);
cec.getRemovedClusters(small_clusters, large_clusters);
for (const auto& small_cluster : (*small_clusters))
for (const auto& j : small_cluster.indices)
(*cloud_out)[j].intensity = -2.0;
for (const auto& large_cluster : (*large_clusters))
for (const auto& j : large_cluster.indices)
(*cloud_out)[j].intensity = +10.0;
for (const auto& cluster : (*clusters))
{
int label = rand() % 8;
for (const auto& j : cluster.indices)
(*cloud_out)[j].intensity = label;
}
std::cerr << "Saving...\n", tt.tic();
pcl::io::savePCDFile("output.pcd", *cloud_out);
std::cerr << ">> Done: " << tt.toc() << " ms\n";
outcloud = cloud_out;
}
#endif
void PCLfunction::RegionGrowing(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::Normal>::Ptr cloud_normals, pcl::PointCloud<pcl::PointXYZRGB>::Ptr outcloud)
{
pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;
reg.setMinClusterSize(50);
reg.setMaxClusterSize(100000);
reg.setSearchMethod(tree);
reg.setNumberOfNeighbours(500);
reg.setInputCloud(incloud);
reg.setInputNormals(cloud_normals);
reg.setSmoothnessThreshold(30);
reg.setCurvatureThreshold(0.05);
std::vector <pcl::PointIndices> clusters;
reg.extract(clusters);
outcloud = reg.getColoredCloud();
pcl::PCDWriter writer;
writer.write("resultFloor.pcd", *outcloud, false);
}
void PCLfunction::RegionGrowingRGB(pcl::PointCloud<pcl::PointXYZRGB>::Ptr incloud, pcl::PointCloud<pcl::PointXYZRGB>::Ptr outcloud)
{
pcl::RegionGrowingRGB<pcl::PointXYZRGB> reg;
reg.setInputCloud(incloud);
reg.setSearchMethod(tree2);
reg.setDistanceThreshold(10);
reg.setPointColorThreshold(6);
reg.setRegionColorThreshold(5);
reg.setMinClusterSize(600);
std::vector <pcl::PointIndices> clusters;
reg.extract(clusters);
outcloud = reg.getColoredCloud();
pcl::PCDWriter writer;
writer.write("1.pcd", *outcloud, false);
}
void PCLfunction::MinCutSegmentation(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZRGB>::Ptr outcloud)
{
pcl::MinCutSegmentation<pcl::PointXYZ> clustering;
clustering.setInputCloud(incloud);
clustering.setSearchMethod(tree);
pcl::PointCloud<pcl::PointXYZ>::Ptr foregroundPoints(new pcl::PointCloud<pcl::PointXYZ>());
pcl::PointXYZ point;
point.x = 0.5;
point.y = 0.5;
point.z = 0.5;
foregroundPoints->points.push_back(point);
clustering.setForegroundPoints(foregroundPoints);
clustering.setSigma(0.5);
clustering.setRadius(0.5);
clustering.setNumberOfNeighbours(200);
clustering.setSourceWeight(0.6);
std::vector <pcl::PointIndices> clusters;
clustering.extract(clusters);
outcloud = clustering.getColoredCloud();
pcl::PCDWriter writer;
writer.write("2.pcd", *outcloud, false);
}
void PCLfunction::DifferenceOfNormalsEstimation(pcl::PointCloud<pcl::PointXYZRGB>::Ptr incloud)
{
pcl::PointCloud<pcl::PointNormal>::Ptr doncloud(new pcl::PointCloud<pcl::PointNormal>);
pcl::PointCloud<pcl::PointNormal>::Ptr normals_small_scale(new pcl::PointCloud<pcl::PointNormal>);
pcl::PointCloud<pcl::PointNormal>::Ptr normals_large_scale(new pcl::PointCloud<pcl::PointNormal>);
pcl::copyPointCloud<pcl::PointXYZRGB, pcl::PointNormal>(*incloud, *doncloud);
pcl::DifferenceOfNormalsEstimation<pcl::PointXYZRGB, pcl::PointNormal, pcl::PointNormal> don;
don.setInputCloud(incloud);
don.setNormalScaleLarge(normals_large_scale);
don.setNormalScaleSmall(normals_small_scale);
don.initCompute();
don.computeFeature(*doncloud);
}
void PCLfunction::SupervoxelClustering(pcl::PointCloud<pcl::PointXYZRGBA>::Ptr incloud)
{
bool disable_transform = true;
float voxel_resolution = 0.008f;
float seed_resolution = 0.1f;
float color_importance = 0.2f;
float spatial_importance = 0.4f;
float normal_importance = 1.0f;
pcl::SupervoxelClustering<pcl::PointXYZRGBA> super(voxel_resolution, seed_resolution);
if (disable_transform)
super.setUseSingleCameraTransform(false);
super.setInputCloud(incloud);
super.setColorImportance(color_importance);
super.setSpatialImportance(spatial_importance);
super.setNormalImportance(normal_importance);
std::map <uint32_t, pcl::Supervoxel<pcl::PointXYZRGBA>::Ptr> supervoxel_clusters;
super.extract(supervoxel_clusters);
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr voxel_centroid_cloud = super.getVoxelCentroidCloud();
pcl::PointCloud<pcl::PointXYZL>::Ptr labeled_voxel_cloud = super.getLabeledVoxelCloud();
pcl::PCDWriter writer;
writer.write("3.pcd", *voxel_centroid_cloud, false);
writer.write("4.pcd", *labeled_voxel_cloud, false);
}
void PCLfunction::NormalEstimation(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::Normal>::Ptr cloud_normals)
{
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
ne.setInputCloud(incloud);
ne.setSearchMethod(tree);
ne.setKSearch(50);
ne.compute(*cloud_normals);
}
void PCLfunction::EstimateBoundary(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::Normal>::Ptr normals, pcl::PointCloud<pcl::Boundary>& boundaries)
{
pcl::BoundaryEstimation<pcl::PointXYZ, pcl::Normal, pcl::Boundary> boundary_est;
boundary_est.setInputCloud(cloud);
boundary_est.setInputNormals(normals);
boundary_est.setKSearch(10);
boundary_est.setAngleThreshold(M_PI / 2);
pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree(new pcl::search::KdTree<pcl::PointXYZ>);
boundary_est.setSearchMethod(kdtree);
boundary_est.compute(boundaries);
}
void PCLfunction::CreateCloudFromTxt(const std::string& file_path, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
std::ifstream fin(file_path.c_str());
std::string line;
pcl::PointXYZ point;
while (getline(fin, line))
{
std::stringstream ss(line);
ss >> point.x;
ss >> point.y;
ss >> point.z;
cloud->push_back(point);
}
fin.close();
}
void PCLfunction::CircularColumn(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::Normal>::Ptr normals, pcl::PointCloud<pcl::PointXYZ > ::Ptr outcloud)
{
pcl::SACSegmentationFromNormals<pcl::PointXYZ, pcl::Normal> seg;
seg.setInputCloud(cloud);
seg.setOptimizeCoefficients(true);
seg.setModelType(pcl::SACMODEL_CYLINDER);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setNormalDistanceWeight(0.1);
seg.setMaxIterations(1000);
seg.setDistanceThreshold(0.05);
seg.setRadiusLimits(3.0, 4.0);
seg.setInputNormals(normals);
pcl::PointIndices::Ptr inliers_cylinder(new pcl::PointIndices);
pcl::ModelCoefficients::Ptr coefficients_cylinder(new pcl::ModelCoefficients);
seg.segment(*inliers_cylinder, *coefficients_cylinder);
pcl::ExtractIndices<pcl::PointXYZ> extract;
extract.setInputCloud(cloud);
extract.setIndices(inliers_cylinder);
extract.setNegative(false);
extract.filter(*outcloud);
}
void PCLfunction::Circle2D(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud)
{
pcl::SampleConsensusModelCircle2D<pcl::PointXYZ>::Ptr model_circle2D(new pcl::SampleConsensusModelCircle2D<pcl::PointXYZ>(cloud));
pcl::RandomSampleConsensus<pcl::PointXYZ> ransac(model_circle2D);
ransac.setDistanceThreshold(0.01);
ransac.setMaxIterations(10000);
ransac.computeModel();
vector<int> inliers;
ransac.getInliers(inliers);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_circle(new pcl::PointCloud<pcl::PointXYZ>);
pcl::copyPointCloud<pcl::PointXYZ>(*cloud, inliers, *outcloud);
Eigen::VectorXf coefficient;
ransac.getModelCoefficients(coefficient);
}
void PCLfunction::removeNan(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud)
{
std::vector<int> indices;
pcl::removeNaNFromPointCloud(*incloud, *outcloud, indices);
}
float PCLfunction::kdTreeSearch(pcl::PointCloud<pcl::PointXYZ>::Ptr incloud, int k)
{
pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
kdtree.setInputCloud(incloud);
float everagedistance = 0;
for (int i = 0; i < incloud->size() / 2; i++)
{
vector<int> nnh;
vector<float> squaredistance;
kdtree.nearestKSearch(incloud->points[i], k, nnh, squaredistance);
everagedistance += sqrt(squaredistance[1]);
}
everagedistance = everagedistance / (incloud->size() / 2);
return everagedistance;
}
int main(int argc, char** argv)
{
PCLfunction* my;
pcl::PointCloud<pcl::Boundary> boundaries;
pcl::PointCloud<pcl::PointXYZ>::Ptr incloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr outcloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZI>::Ptr incloud1(new pcl::PointCloud<pcl::PointXYZI>);
pcl::PointCloud<pcl::PointXYZI>::Ptr outcloud1(new pcl::PointCloud<pcl::PointXYZI>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr incloud2(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr outcloud2(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr incloud3(new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>);
int x = 0;
float w = 0;
std::cout << "选择点云" << endl;
cin >> x;
switch (x)
{
case 1:
pcl::io::loadPCDFile<pcl::PointXYZ>("table_scene_lms400.pcd", *incloud);
break;
case 2:
pcl::io::loadPCDFile<pcl::PointXYZRGB>("2.pcd", *incloud2);
break;
case 3:
pcl::io::loadPCDFile<pcl::PointXYZRGBA>("pig.pcd", *incloud3);
break;
case 4:
pcl::io::loadPCDFile<pcl::PointXYZI>("Statues_4.pcd", *incloud1);
break;
case 5:
pcl::io::loadPCDFile<pcl::PointXYZ>("test.pcd", *incloud);
break;
case 6:
pcl::io::loadPCDFile<pcl::PointXYZ>("circle2D.pcd", *incloud);
break;
case 7:
pcl::io::loadPCDFile<pcl::PointXYZ>("E:\\DataSet\\PointCloud\\boundary.pcd", *incloud);
break;
default:
pcl::io::loadPCDFile<pcl::PointXYZ>("rabbit.pcd", *incloud);
}
pcl::getMinMax3D(*incloud, p_min, p_max);
w = my->kdTreeSearch(incloud, 50);
std::cout <<"点云点数"<< incloud->size() << endl;
std::cout << "最小点" << p_min.x <<"," << p_min.y<<","<< p_min.z<<endl;
std::cout << "最大点" << p_max.x << "," << p_max.y << "," << p_max.z << endl;
std::cout << "点云密度" << w << endl;
cout << endl;
int y = 0;
std::cout << "选择点云处理方式" << endl;
cin >> y;
switch (y)
{
case 1:
my->NormalEstimation(incloud, cloud_normals);
break;
case 2:
my->NormalEstimation(incloud, cloud_normals);
my->RegionGrowing(incloud, cloud_normals, outcloud2);
pcl::io::loadPCDFile<pcl::PointXYZRGB>("resultFloor.pcd", *outcloud2);
break;
case 3:
my->RegionGrowingRGB(incloud2, outcloud2);
break;
case 4:
my->MinCutSegmentation(incloud, outcloud2);
break;
case 5:
my->NormalEstimation(incloud, cloud_normals);
my->EstimateBoundary(incloud, cloud_normals, boundaries);
for (size_t i = 0; i < incloud->points.size(); ++i) {
if (boundaries[i].boundary_point > 0) {
outcloud->push_back(incloud->points[i]);
}
}
break;
case 6:
my->ModelOutlierRemoval(incloud, outcloud, 2.0);
break;
case 7:
my->PassThrough(incloud, outcloud, 0.05, 3, 1);
break;
case 8:
my->VoxelGrid(incloud, outcloud, 0.01);
break;
case 9:
my->UniformSampling(incloud, outcloud, 0.01);
break;
case 10:
my->StatisticalOutlierRemoval(incloud, outcloud, 50, 1.0);
break;
case 11:
my->RANSAC(incloud, outcloud);
break;
case 12:
my->EuclideanClusterExtraction(incloud);
break;
case 13:
my->SupervoxelClustering(incloud3);
break;
case 14:
my->CropHull(incloud,outcloud);
break;
case 15:
my->NormalEstimation(incloud, cloud_normals);
my->CircularColumn(incloud, cloud_normals, outcloud);
break;
case 16:
break;
default:
my->PassThrough(incloud, outcloud, 0.05, 3, 1);
}
int z = 0;
std::cout << "选择可视化方式" << endl;
cin >> z;
switch (z)
{
case 1:
my->visualization2wd1(incloud1, outcloud1);
break;
case 2:
my->visualization1(outcloud2);
break;
case 3:
my->visualization2wd(incloud, outcloud);
break;
case 4:
my->visualization2wd2(incloud, outcloud2);
break;
case 5:
my->visualization2wd3(incloud2, outcloud2);
default:
my->visualization2wd2(incloud, outcloud2);
}
}
3. 一个示例
- 例如选择7,11,3可以得到对XYZ类型的点云进行RANSAC平面拟合的处理效果,并且以双窗口的形式对处理结果进行展示。
- 并且可以显示点云点数,最大最小点以及点云密度等信息
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8bcb3ab077cc540fb961ceca5d86dfe6.png#pic_center)