C++ PCL点云数据可视化:找出Y轴最大值节点并标记
这段代码使用C++ PCL库对点云数据进行可视化处理,它能够找出在Y方向上具有最大值的节点,并将其标记为绿色。此外,代码还会将出现一次的节点标记为红色。
以下是代码的详细解释:
-
统计节点出现次数:
- 创建一个
std::unordered_map类型的countMap,用来存储每个节点出现的次数。 - 遍历
result中的每条边,将边的起始节点和目标节点在countMap中进行计数。
- 创建一个
-
标记出现三次的节点:
- 创建一个
pcl::PointCloud<pcl::PointXYZ>类型的指针threejiedian,用于存储出现三次的节点。 - 遍历
countMap,找到出现次数大于等于3的节点,并将它们的坐标点添加到threejiedian中。 - 使用
viewer.addSphere()为出现三次的节点添加一个绿色的球体表示,并将其添加到viewer中进行可视化展示。
- 创建一个
-
标记出现一次的节点:
- 创建一个
pcl::PointCloud<pcl::PointXYZ>类型的指针singlejiedian,用于存储出现一次的节点。 - 遍历
countMap,找到出现次数等于1的节点,并将它们的坐标点添加到singlejiedian中。 - 使用
viewer.addSphere()为出现一次的节点添加一个红色的球体表示,并将其添加到viewer中进行可视化展示。
- 创建一个
代码解释:
// Find the node with the maximum value in the y direction and mark it as green
std::unordered_map<int, int> countMap;
for (const auto& edge : result)
{
countMap[edge.src]++;
countMap[edge.tgt]++;
}
//出现三次的节点用绿色表示,表示为节点
pcl::PointCloud<pcl::PointXYZ>::Ptr threejiedian(new pcl::PointCloud<pcl::PointXYZ>);
int begin = 0;
for (const auto& pair : countMap)
{
if (pair.second >= 3)
{
//std::cout << "该点坐标: " << pair.first << " 出现 " << pair.second << " 次" << std::endl;
//std::cout << cloud->points[pair.first] << std::endl;
pcl::PointXYZ point3 = cloud->points[pair.first];
std::stringstream sa;
//std::cout << "节点索引号: " << pair.first << std::endl;
sa << begin;
begin++;
viewer.addSphere(pcl::PointXYZ(point3), 0.0023, 0, 1, 0, sa.str());
threejiedian->push_back(point3);
}
}
//出现一次的节点用红色表示,表示为叶尖
pcl::PointCloud<pcl::PointXYZ>::Ptr singlejiedian(new pcl::PointCloud<pcl::PointXYZ>);
int begin2 = 0;
for (const auto& pair : countMap)
{
if (pair.second == 1)
{
pcl::PointXYZ point3 = cloud->points[pair.first];
std::stringstream sb;
sb << begin2;
begin2++;
viewer.addSphere(pcl::PointXYZ(point3), 0.0023, 1, 0, 0, sb.str());
singlejiedian->push_back(point3);
}
}
总结:这段代码通过统计每个节点在边中出现的次数,找出在Y方向上具有最大值的节点,并将其标记为绿色,同时将出现一次的节点标记为红色。
原文地址: https://www.cveoy.top/t/topic/pYV7 著作权归作者所有。请勿转载和采集!