大三下每日打卡008
详细的点云识别的代码。
#include <pcl/ModelCoefficients.h> #include <pcl/point_types.h> #include <pcl/io/pcd_io.h> #include <pcl/filters/extract_indices.h> #include <pcl/filters/voxel_grid.h> #include <pcl/features/normal_3d.h> #include <pcl/kdtree/kdtree.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/visualization/pcl_visualizer.h> #include <pcl/common/centroid.h> #include <pcl/common/distances.h> bool isPushSpace = false; // 键盘事件 void keyboard_event_occurred(const pcl::visualization::KeyboardEvent& event, void* nothing) { if (event.getKeySym() == "space" && event.keyDown()) { isPushSpace = true; } } std::string classify_cluster(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster) { // 计算聚类的质心 pcl::PointXYZ centroid; pcl::computeCentroid(*cloud_cluster, centroid); // 计算聚类的大小(简单的bounding box法) float min_x = std::numeric_limits<float>::max(), max_x = -std::numeric_limits<float>::max(); float min_y = std::numeric_limits<float>::max(), max_y = -std::numeric_limits<float>::max(); float min_z = std::numeric_limits<float>::max(), max_z = -std::numeric_limits<float>::max(); for (const auto& point : cloud_cluster->points) { min_x = std::min(min_x, point.x); max_x = std::max(max_x, point.x); min_y = std::min(min_y, point.y); max_y = std::max(max_y, point.y); min_z = std::min(min_z, point.z); max_z = std::max(max_z, point.z); } // 计算聚类的尺寸(宽度、深度、高度) float width = max_x - min_x; float depth = max_y - min_y; float height = max_z - min_z; // 根据尺寸和质心位置进行分类(示例判断逻辑) if (width > 0.2 && depth > 0.2 && height > 0.2) return "Large Object"; // 大物体 else if (width > 0.1 && depth > 0.1 && height > 0.1) return "Medium Object"; // 中等物体 else return "Small Object"; // 小物体 } int main(int argc, char** argv) { // 从PCD文件中读取点云数据 pcl::PCDReader reader; pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>), cloud_f(new pcl::PointCloud<pcl::PointXYZ>()); //reader.read("C:\\Users\\huxiang\\Downloads\\table_scene_lms400.pcd", *cloud); //reader.read("C:\\Users\\huxiang\\Downloads\\points cloud document\\human in office.pcd", *cloud); //reader.read("C:\\Users\\huxiang\\Downloads\\points cloud document\\table_scene_mug_stereo_textured.pcd", *cloud); //reader.read("C:\\Users\\huxiang\\Downloads\\points cloud document\\TetrahedronMultiple.pcd", *cloud); reader.read("C:\\Users\\huxiang\\Downloads\\points cloud document\\CHAMBRE.pcd", *cloud); std::cout << "PointCloud before filtering has: " << cloud->points.size() << " data points." << std::endl; pcl::visualization::PCLVisualizer viewer("Cluster Extraction"); viewer.registerKeyboardCallback(&keyboard_event_occurred, (void*)NULL); int v1(1); int v2(2); viewer.createViewPort(0, 0, 0.5, 1, v1); viewer.createViewPort(0.5, 0, 1, 1, v2); // 使用体素网格滤波器进行下采样 pcl::VoxelGrid<pcl::PointXYZ> vg; pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>()); vg.setInputCloud(cloud); vg.setLeafSize(0.1f, 0.1f, 0.1f); vg.filter(*cloud_filtered); std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size() << " data points." << std::endl; viewer.addPointCloud(cloud, "cloud1", v1); viewer.addPointCloud(cloud_filtered, "cloud2", v2); viewer.spinOnce(10000); // 创建平面分割对象 pcl::SACSegmentation<pcl::PointXYZ> seg; pcl::PointIndices::Ptr inliers(new pcl::PointIndices); pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients); pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane(new pcl::PointCloud<pcl::PointXYZ>()); pcl::PCDWriter writer; seg.setOptimizeCoefficients(true); seg.setModelType(pcl::SACMODEL_PLANE); seg.setMethodType(pcl::SAC_RANSAC); seg.setMaxIterations(100); seg.setDistanceThreshold(0.02); // 重复分割直到剩余点云小于原始点云的30% int i = 0, nr_points = (int)cloud_filtered->points.size(); while (cloud_filtered->points.size() > 0.3 * nr_points) { seg.setInputCloud(cloud_filtered); seg.segment(*inliers, *coefficients); if (inliers->indices.size() == 0) { std::cout << "Could not estimate a planar model for the given dataset." << std::endl; break; } pcl::ExtractIndices<pcl::PointXYZ> extract; extract.setInputCloud(cloud_filtered); extract.setIndices(inliers); extract.setNegative(false); extract.filter(*cloud_plane); std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size() << " data points." << std::endl; extract.setNegative(true); extract.filter(*cloud_f); viewer.updatePointCloud(cloud_filtered, "cloud1"); viewer.updatePointCloud(cloud_f, "cloud2"); viewer.spinOnce(3000); cloud_filtered = cloud_f; } viewer.removePointCloud("cloud2", v2); // 创建KdTree对象作为搜索方法 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>()); tree->setInputCloud(cloud_filtered); std::vector<pcl::PointIndices> cluster_indices; pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec; ec.setClusterTolerance(0.02); // 2cm ec.setMinClusterSize(100); ec.setMaxClusterSize(25000); ec.setSearchMethod(tree); ec.setInputCloud(cloud_filtered); ec.extract(cluster_indices); 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(cloud_filtered->points[*pit]); cloud_cluster->width = cloud_cluster->points.size(); cloud_cluster->height = 1; cloud_cluster->is_dense = true; std::cout << "Cluster " << j << " has " << cloud_cluster->points.size() << " data points." << std::endl; // 使用分类函数判断聚类的类型 std::string cluster_type = classify_cluster(cloud_cluster); std::cout << "Cluster " << j << " is classified as: " << cluster_type << std::endl; std::stringstream ss; ss << "cloud_cluster_" << j << ".pcd"; writer.write<pcl::PointXYZ>(ss.str(), *cloud_cluster, false); // 显示不同颜色的聚类 pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cluster_color(cloud_cluster, rand() * 100 + j * 80, rand() * 50 + j * 90, rand() * 200 + j * 100); viewer.addPointCloud(cloud_cluster, cluster_color, ss.str(), v2); viewer.spinOnce(5000); j++; } while (!viewer.wasStopped()) { viewer.spinOnce(); } return 0; }
PATH=D:\tool\PCL 1.12.0\bin;D:\tool\PCL 1.12.0\3rdParty\VTK\bin;D:\tool\PCL 1.12.0\3rdParty\FLANN\bin;D:\tool\PCL 1.12.0\3rdParty\OpenNI2\Tools;D:\tool\PCL 1.12.0\3rdParty\Qhull\bin D:\tool\PCL 1.13.0\lib D:\tool\PCL 1.13.0\3rdParty\Boost\lib D:\tool\PCL 1.13.0\3rdParty\FLANN\lib D:\tool\PCL 1.13.0\3rdParty\OpenNI2\Lib D:\tool\PCL 1.13.0\3rdParty\Qhull\lib D:\tool\PCL 1.13.0\3rdParty\VTK\lib BOOST_USE_WINDOWS_H NOMINMAX _CRT_SECURE_NO_DEPRECATE D:\tool\PCL 1.13.0\include\pcl-1.13 D:\tool\PCL 1.13.0\3rdParty\Boost\include\boost-1_80 D:\tool\PCL 1.13.0\3rdParty\Eigen\eigen3 D:\tool\PCL 1.13.0\3rdParty\FLANN\include D:\tool\PCL 1.13.0\3rdParty\OpenNI2\Include D:\tool\PCL 1.13.0\3rdParty\Qhull\include D:\tool\PCL 1.13.0\3rdParty\VTK\include\vtk-9.2 #include <pcl/ModelCoefficients.h> #include <pcl/point_types.h> #include <pcl/io/pcd_io.h> #include <pcl/filters/extract_indices.h> #include <pcl/filters/voxel_grid.h> #include <pcl/features/normal_3d.h> #include <pcl/kdtree/kdtree.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/visualization/pcl_visualizer.h> bool isPushSpace = false; // 键盘事件 void keyboard_event_occurred(const pcl::visualization::KeyboardEvent& event, void* nothing) { if (event.getKeySym() == "space" && event.keyDown()) { isPushSpace = true; } } int main(int argc, char** argv) { // 从PCD文件中读取点云数据 pcl::PCDReader reader; pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>), cloud_f(new pcl::PointCloud<pcl::PointXYZ>); reader.read("C:\\Users\\huxiang\\Downloads\\table_scene_lms400.pcd", *cloud); std::cout << "PointCloud before filtering has: " << cloud->points.size() << " data points." << std::endl; pcl::visualization::PCLVisualizer viewer("Cluster Extraction"); // 注册键盘事件 viewer.registerKeyboardCallback(&keyboard_event_occurred, (void*)NULL); int v1(1); int v2(2); viewer.createViewPort(0, 0, 0.5, 1, v1); viewer.createViewPort(0.5, 0, 1, 1, v2); // 创建滤波对象: 使用下采样,叶子的大小为 1cm pcl::VoxelGrid<pcl::PointXYZ> vg; pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>); vg.setInputCloud(cloud); vg.setLeafSize(0.01f, 0.01f, 0.01f); vg.filter(*cloud_filtered); std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size() << " data points." << std::endl; viewer.addPointCloud(cloud, "cloud1", v1); viewer.addPointCloud(cloud_filtered, "cloud2", v2); // 渲染10秒再继续 viewer.spinOnce(10000); // 创建平面分割对象 pcl::SACSegmentation<pcl::PointXYZ> seg; pcl::PointIndices::Ptr inliers(new pcl::PointIndices); pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients); pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane(new pcl::PointCloud<pcl::PointXYZ>()); pcl::PCDWriter writer; seg.setOptimizeCoefficients(true); seg.setModelType(pcl::SACMODEL_PLANE); seg.setMethodType(pcl::SAC_RANSAC); seg.setMaxIterations(100); seg.setDistanceThreshold(0.02); // 把点云中所有的平面全部过滤掉,重复过滤,直到点云数量小于原来的0.3倍 int i = 0, nr_points = (int)cloud_filtered->points.size(); while (cloud_filtered->points.size() > 0.3 * nr_points) { // Segment the largest planar component from the remaining cloud seg.setInputCloud(cloud_filtered); seg.segment(*inliers, *coefficients); if (inliers->indices.size() == 0) { std::cout << "Could not estimate a planar model for the given dataset." << std::endl; break; } // Extract the planar inliers from the input cloud pcl::ExtractIndices<pcl::PointXYZ> extract; extract.setInputCloud(cloud_filtered); extract.setIndices(inliers); extract.setNegative(false); // Write the planar inliers to disk extract.filter(*cloud_plane); std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size() << " data points." << std::endl; // Remove the planar inliers, extract the rest extract.setNegative(true); extract.filter(*cloud_f); // 更新显示点云 viewer.updatePointCloud(cloud_filtered, "cloud1"); viewer.updatePointCloud(cloud_f, "cloud2"); // 渲染3秒再继续 viewer.spinOnce(3000); cloud_filtered = cloud_f; } viewer.removePointCloud("cloud2", v2); // 创建KdTree对象作为搜索方法 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>); tree->setInputCloud(cloud_filtered); std::vector<pcl::PointIndices> cluster_indices; pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec; ec.setClusterTolerance(0.02); // 2cm ec.setMinClusterSize(100); ec.setMaxClusterSize(25000); ec.setSearchMethod(tree); ec.setInputCloud(cloud_filtered); // 聚类抽取结果保存在一个数组中,数组中每个元素代表抽取的一个组件点云的下标 ec.extract(cluster_indices); // 遍历抽取结果,将其显示并保存 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(cloud_filtered->points[*pit]); // 设置点云属性 cloud_cluster->width = cloud_cluster->points.size(); cloud_cluster->height = 1; cloud_cluster->is_dense = true; std::cout << "当前聚类 " << j << " 包含的点云数量: " << cloud_cluster->points.size() << " data points." << std::endl; std::stringstream ss; ss << "cloud_cluster_" << j << ".pcd"; writer.write<pcl::PointXYZ>(ss.str(), *cloud_cluster, false); // 显示, 随机设置不同颜色,以区分不同的聚类 pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cluster_color(cloud_cluster, rand() * 100 + j * 80, rand() * 50 + j * 90, rand() * 200 + j * 100); viewer.addPointCloud(cloud_cluster, cluster_color, ss.str(), v2); j++; } while (!viewer.wasStopped()) { viewer.spinOnce(); } return (0); } 欧式聚类算法(Euclidean Clustering)是一种将点云数据分割成不同聚类(簇)的算法。它是点云分割中的一种常用方法,旨在将距离比较近的点分为同一簇,从而识别出不同的物体或结构。这个算法主要适用于包含较小噪音和具有一定距离差异的点云数据。基本思想: (1)选择一个种子点(Seed Point)作为当前簇的起始点。 (2) 遍历所有未分类的点,计算它们与种子点的距离。如果距离小于设定的阈值,将其归为同一簇。 (3) 对于同一簇中的点,重复步骤2,将与当前簇内任一点距离小于阈值的点加入簇。 (4) 切换到下一个未分类的点,作为新的种子点,继续重复步骤2和3。 (5) 当所有点都被分类为某个簇,聚类过程结束。
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号