PCL 欧式聚类(Segmentation_EuclideanClusterExtraction)

PCL专栏目录及须知

注意:欧式聚类很常用。

1.欧式聚类原理:

(1)对点云建KD树。并选取点云中内某一初始点P。

(2)对P进行邻近搜索,获取其在用户设定的距离阈值内的所有点云点。加入集合Q。

(3)对集合Q内未进行临近搜索的所有点云点按照用户设定的距离阈值进行邻近搜索。

(4)不停迭代(2)(3),得到一簇符合距离阈值的点。

(5)重新设置未聚类的点云初始点P,再进行如上步骤,直到聚类得到所有点云。

如下图:

假设我们设置的初始点P在红色部分中,

1)我们首先通过距离阈值,不停摸完附近点云,直到摸无可摸,得到红色簇的点云簇。

2)更换初始点P的位置(红色簇已聚类,被剔除,因此只能在其他点云中得到初始点P)。假设更换后的初始点P在蓝色簇中,再进行1)的步骤,摸出蓝色簇。

3)如此反复,直到得到所有点云点。

2.使用场景

欧式聚类非常常用,在点云不连通的情况下,聚类效果较好,参数调整也较少。

3.关键函数

(1)设置邻近搜索的半径,也就是你摸点云时候的距离阈值

segmentation.setClusterTolerance(1.3); 

(2)一簇点包含的最少点数(少于这个点数不当作点云簇)

segmentation.setMinClusterSize(50);

(3)一簇点包含的最大点数(大于这个点数不当作点云簇)

segmentation.setMaxClusterSize(10000);

(4)得到的结果为分簇之后的点云索引

std::vector<pcl::PointIndices> clusterIndices;                           // 聚类后点云
    segmentation.extract(clusterIndices);

4.完整代码

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/common/colors.h>
#include <pcl/kdtree/kdtree.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/visualization/pcl_visualizer.h> 
#include <boost/thread/thread.hpp>

int main()
{
    /****************欧式聚类********************/
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);			// 源点云
    pcl::io::loadPCDFile("D:/code/csdn/data/kmeansTest.pcd", *cloud);

    // 欧式聚类
    pcl::EuclideanClusterExtraction<pcl::PointXYZ> segmentation;              // 欧式聚类对象
    segmentation.setClusterTolerance(1.3);                                   // 近邻搜索的搜索半径为2cm(也即两个不同聚类团点之间的最小欧氏距离)
    segmentation.setMinClusterSize(50);                                      // 一簇点包含的最少点数
    segmentation.setMaxClusterSize(10000);                                     // 一簇点包含的最大点数
    pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
    tree->setInputCloud(cloud);
    segmentation.setSearchMethod(tree);
    segmentation.setInputCloud(cloud);
    std::vector<pcl::PointIndices> clusterIndices;                           // 聚类后点云
    segmentation.extract(clusterIndices);

    // 提取聚类后点云
    std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> clustreClouds;
    for (size_t i = 0; i < clusterIndices.size(); i++)
    {
        pcl::PointCloud<pcl::PointXYZ>::Ptr cloudTemp(new pcl::PointCloud<pcl::PointXYZ>);
        pcl::copyPointCloud(*cloud, clusterIndices[i], *cloudTemp);
        clustreClouds.push_back(cloudTemp);
    }

    /****************展示********************/
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("EuclideanClusterExtraction"));
    for (size_t i = 0; i < clustreClouds.size(); i++)
    {
       pcl::RGB rgb = pcl::getRandomColor(0, 255);
       viewer->addPointCloud<pcl::PointXYZ>(clustreClouds.at(i), pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(clustreClouds.at(i), rgb.r, rgb.g, rgb.b), "clustreClouds" + i);
    }
    
    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);
        boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    }

    return (0);
}

5.结果展示

ESP8266是一款常用的WiFi模块,它支持通过MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议)连接到物联网服务器,实现设备之间的通信。以下是基本步骤: 1. **安装库**: - 首先,你需要在Arduino IDE中安装`PubSubClient`库,这是一个用于ESP8266与MQTT服务器通信的常用库。 2. **配置WiFi连接**: - 设置ESP8266连接到你的Wi-Fi网络,包括SSID和密码。 3. **设置MQTT客户端**: ```cpp #include <ESP8266WiFi.h> #include < PubSubClient.h > WiFiClient client; PubSubClient mqttClient(client, "your_broker_address", "username", "password"); ``` 这里"your_broker_address"替换为你的MQTT服务器地址,"username"和"password"则是登录账号和密码。 4. **连接到MQTT服务器**: ```cpp void connectToMqtt() { mqttClient.setServer("your_broker_address", 1883); while (!client.connected()) { if (client.connect("ESP8266Client")) { Serial.println("Connected to MQTT broker"); mqttClient.subscribe("$SYS/#"); // 订阅所有主题 } else { delay(5000); // 尝试重连,延时5秒 Serial.print("Attempting to reconnect..."); } } } ``` 5. **发布和订阅主题**: - 发布数据到主题: ```cpp void publish(String topic, String message) { mqttClient.publish(topic, message); Serial.println("Published message to " + topic); } ``` - 订阅并处理接收的数据: ```cpp void loop() { if (mqttClient.connected()) { mqttClient.loop(); // 处理接收到的消息 } // ...其他代码... } ``` 6. **断开连接**: ```cpp void stopMqtt() { mqttClient.disconnect(); Serial.println("Disconnected from MQTT broker"); } ``` 记得根据实际情况调整代码,并在`loop()`函数中添加相应的数据处理逻辑。当你有新的消息要发送或需要监听来自服务器的信息时,只需调用上述相应方法即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值