基于pcl库使用kruskal最小生成树处理点云ply文件并将各各点按照生成树顺序连接可视化的c++代码
以下是基于pcl库使用kruskal最小生成树处理点云ply文件并将各个点按照生成树顺序连接可视化的C++代码:
#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/common/common.h>
#include <pcl/common/transforms.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/features/normal_3d.h>
#include <pcl/registration/ia_ransac.h>
#include <pcl/common/eigen.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/voxel_grid.h>
typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
// Structure to represent a weighted edge in graph
struct Edge {
int src, dest;
float weight;
};
// Structure to represent a connected, undirected and weighted graph
struct Graph {
// V-> Number of vertices, E-> Number of edges
int V, E;
// graph is represented as an array of edges
struct Edge* edge;
};
// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = new Graph;
graph->V = V;
graph->E = E;
graph->edge = new Edge[E];
return graph;
}
// A structure to represent a subset for union-find
struct subset {
int parent;
int rank;
};
// A utility function to find set of an element i
// (uses path compression technique)
int find(struct subset subsets[], int i)
{
// find root and make root as parent of i
// (path compression)
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
// A function that does union of two sets of x and y
// (uses union by rank)
void Union(struct subset subsets[], int x, int y)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);
// Attach smaller rank tree under root of high rank tree
// (Union by Rank)
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
// If ranks are same, then make one as root and increment
// its rank by one
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
// Compare two edges according to their weights.
// Used in qsort() for sorting an array of edges
int myComp(const void* a, const void* b)
{
struct Edge* a1 = (struct Edge*)a;
struct Edge* b1 = (struct Edge*)b;
return a1->weight > b1->weight;
}
// The main function to construct MST using Kruskal's algorithm
void kruskalMST(struct Graph* graph, PointCloudT::Ptr cloud)
{
int V = graph->V;
struct Edge result[V]; // Tnis will store the resultant MST
int e = 0; // An index variable, used for result[]
int i = 0; // An index variable, used for sorted edges
// Step 1: Sort all the edges in non-decreasing order of their weight
// If we are not allowed to change the given graph, we can create a copy of
// array of edges
qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp);
// Allocate memory for creating V ssubsets
struct subset* subsets = new subset[(V * sizeof(struct subset))];
// Create V subsets with single elements
for (int v = 0; v < V; ++v) {
subsets[v].parent = v;
subsets[v].rank = 0;
}
// Number of edges to be taken is equal to V-1
while (e < V - 1 && i < graph->E) {
// Step 2: Pick the smallest edge. And increment
// the index for next iteration
struct Edge next_edge = graph->edge[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
// If including this edge does't cause cycle,
// include it in result and increment the index
// of result for next edge
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
// Else discard the next_edge
}
// print the contents of result[] to display the built MST
pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
viewer.setBackgroundColor(0, 0, 0);
for (i = 0; i < e; ++i) {
int srcIdx = result[i].src;
int destIdx = result[i].dest;
PointT srcPt = cloud->points[srcIdx];
PointT destPt = cloud->points[destIdx];
pcl::PointXYZRGB srcPtRGB;
srcPtRGB.x = srcPt.x;
srcPtRGB.y = srcPt.y;
srcPtRGB.z = srcPt.z;
srcPtRGB.r = 255;
srcPtRGB.g = 0;
srcPtRGB.b = 0;
pcl::PointXYZRGB destPtRGB;
destPtRGB.x = destPt.x;
destPtRGB.y = destPt.y;
destPtRGB.z = destPt.z;
destPtRGB.r = 255;
destPtRGB.g = 0;
destPtRGB.b = 0;
std::string edgeId = "edge" + std::to_string(i);
viewer.addLine(srcPtRGB, destPtRGB, edgeId);
}
while (!viewer.wasStopped()) {
viewer.spinOnce(100);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
int main(int argc, char** argv)
{
// Read input cloud from PLY file
PointCloudT::Ptr cloud(new PointCloudT);
pcl::PLYReader reader;
reader.read("input_cloud.ply", *cloud);
// Create a graph from the point cloud
int V = cloud->size();
int E = V * (V - 1) / 2;
struct Graph* graph = createGraph(V, E);
int edgeIndex = 0;
for (int i = 0; i < cloud->size(); i++) {
for (int j = i + 1; j < cloud->size(); j++) {
PointT srcPt = cloud->points[i];
PointT destPt = cloud->points[j];
float distance = pcl::euclideanDistance(srcPt, destPt);
graph->edge[edgeIndex].src = i;
graph->edge[edgeIndex].dest = j;
graph->edge[edgeIndex].weight = distance;
edgeIndex++;
}
}
// Find minimum spanning tree and visualize it
kruskalMST(graph, cloud);
return 0;
}
这段代码首先读取一个PLY文件作为输入点云,然后根据输入点云的坐标计算各个点之间的距离,并构建成一个加权无向图。接下来,使用Kruskal算法找到该图的最小生成树,并将最小生成树的边可视化。最后,使用PCL的可视化工具显示生成的点云和边。
请注意,此代码假设输入的PLY文件包含点的XYZ坐标和RGB颜色信息。如果您的PLY文件不包含颜色信息,您可能需要进行相应的修改
原文地址: https://www.cveoy.top/t/topic/hHZ5 著作权归作者所有。请勿转载和采集!