pcl拟图片中所有直线的代码c++
时间: 2024-02-12 10:04:32 浏览: 93
C++读取图片代码
5星 · 资源好评率100%
以下是使用PCL C++实现的代码,用于检测点云中的所有直线:
```c++
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/features/normal_3d.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/visualization/pcl_visualizer.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>);
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
pcl::SACSegmentationFromNormals<pcl::PointXYZ, pcl::Normal> seg;
pcl::ExtractIndices<pcl::PointXYZ> extract;
pcl::ExtractIndices<pcl::Normal> extract_normals;
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
pcl::visualization::PCLVisualizer viewer("Detected Lines");
// Read in the cloud data
pcl::PCDReader reader;
reader.read<pcl::PointXYZ> ("cloud.pcd", *cloud);
// Downsample the cloud using a Voxel Grid filter
pcl::VoxelGrid<pcl::PointXYZ> vg;
vg.setInputCloud (cloud);
vg.setLeafSize (0.01f, 0.01f, 0.01f);
vg.filter (*cloud_filtered);
// Estimate point normals
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
ne.setInputCloud (cloud_filtered);
ne.setSearchMethod (tree);
ne.setKSearch (50);
ne.compute (*cloud_normals);
// Create the segmentation object for the planar model and set all the parameters
seg.setOptimizeCoefficients (true);
seg.setModelType (pcl::SACMODEL_NORMAL_PLANE);
seg.setNormalDistanceWeight (0.1);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setMaxIterations (100);
seg.setDistanceThreshold (0.03);
seg.setInputCloud (cloud_filtered);
seg.setInputNormals (cloud_normals);
// Obtain the plane inliers and coefficients
seg.segment (*inliers, *coefficients);
// Extract the planar inliers from the input cloud
extract.setInputCloud (cloud_filtered);
extract.setIndices (inliers);
extract.setNegative (false);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane (new pcl::PointCloud<pcl::PointXYZ> ());
extract.filter (*cloud_plane);
// Remove the planar inliers, extract the rest
extract.setNegative (true);
extract.filter (*cloud_filtered);
extract_normals.setInputCloud (cloud_normals);
extract_normals.setIndices (inliers);
extract_normals.setNegative (false);
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals_plane (new pcl::PointCloud<pcl::Normal> ());
extract_normals.filter (*cloud_normals_plane);
// Create the segmentation object for cylinder segmentation and set all the parameters
seg.setOptimizeCoefficients (true);
seg.setModelType (pcl::SACMODEL_CYLINDER);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setNormalDistanceWeight (0.1);
seg.setMaxIterations (10000);
seg.setDistanceThreshold (0.05);
seg.setRadiusLimits (0.01, 0.2);
seg.setInputCloud (cloud_filtered);
seg.setInputNormals (cloud_normals_plane);
// Obtain the cylinder inliers and coefficients
std::vector<pcl::ModelCoefficients> cylinder_coefficients;
std::vector<pcl::PointIndices> cylinder_inliers;
seg.segment (cylinder_inliers, cylinder_coefficients);
// Draw the cylinder inliers in red
for (size_t i = 0; i < cylinder_inliers.size (); i++)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cylinder (new pcl::PointCloud<pcl::PointXYZ> ());
for (size_t j = 0; j < cylinder_inliers[i].indices.size (); j++)
cloud_cylinder->push_back ((*cloud_filtered)[cylinder_inliers[i].indices[j]]);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> red_color(cloud_cylinder, 255, 0, 0);
viewer.addPointCloud<pcl::PointXYZ> (cloud_cylinder, red_color, "cylinder" + std::to_string(i));
}
// Spin until the user closes the viewer
viewer.spin();
return 0;
}
```
这个代码首先将点云数据读入内存,然后使用体素网格滤波器进行下采样。接着估计点的法线,并使用法线平面模型进行分割。然后使用法线面模型分割后的剩余点云数据,使用圆柱模型进行分割。最后将所有检测到的圆柱绘制在可视化窗口中。
阅读全文