std::vector<std::vector<int>> LidarObjectSeg::Run(const pcl::PointCloud<pcl::PointXYZI>::Ptr &inCloud) const { if (inCloud->empty()) return {}; std::vector<std::array<double, 3>> gridPoints; GridParam gridParam = this->EstimateGridParam(inCloud, gridPoints); std::multimap<int, int> hashTable = this->UpdateHashTable(gridPoints, gridParam); std::vector<int> clusterIndices(inCloud->size(), -1); int curClusterIdx = 0; START_HOST_TIMING(ExtractClusters) for (std::size_t i = 0; i < inCloud->size(); ++i) { if (clusterIndices[i] >= 0) continue; const auto &curGridPoint = gridPoints[i]; std::vector<int> neighborIndices = this->GetNeighbors(curGridPoint, gridParam, hashTable); for (int neighborIdx : neighborIndices) { if (neighborIdx == i) continue; int curPointVoxelIdx = clusterIndices[i]; int neighborVoxelIdx = clusterIndices[neighborIdx]; if (curPointVoxelIdx >= 0 && neighborVoxelIdx >= 0) { if (curPointVoxelIdx != neighborVoxelIdx) this->MergeClusters(clusterIndices, curPointVoxelIdx, neighborVoxelIdx); } else { if (curPointVoxelIdx < 0) clusterIndices[i] = neighborVoxelIdx; else clusterIndices[neighborIdx] = curPointVoxelIdx; } } if (clusterIndices[i] < 0 && neighborIndices.size() >= numMinPoints_) { for (int neighborIdx : neighborIndices) { clusterIndices[neighborIdx] = curClusterIdx; } curClusterIdx++; } } STOP_HOST_TIMING(ExtractClusters) START_HOST_TIMING(GetAllClusters) std::vector<std::vector<int>> allClusters = this->GetAllClusters(clusterIndices); STOP_HOST_TIMING(GetAllClusters) PRINT_ALL_TIMING() return allClusters; }
时间: 2024-02-15 10:28:50 浏览: 161
这是一个函数,可以接收一个指向 pcl::PointCloud<pcl::PointXYZI> 类型的指针作为参数,函数返回一个 std::vector<std::vector<int>> 类型的对象。在函数内部,首先判断输入点云是否为空,如果为空则直接返回一个空的 std::vector<std::vector<int>> 类型的对象。然后函数会估算点云的网格参数,并根据网格参数更新哈希表。之后,函数会遍历点云中的每个点,如果该点已经被聚类过,则跳过该点;否则,函数会获取该点附近的所有点,然后将这些点分配到同一个聚类中。最后,函数会返回所有聚类的点的索引。
相关问题
严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C2664 “bool pcl::visualization::PCLVisualizer::addPointCloud(const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZ>> &,const std::string &,int)”: 无法将参数 1 从“boost::shared_ptr<pcl::PointCloud<pcl::PointXYZI>>”转换为“const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZ>> &” pointToimg D:\VS2019\VsProject\pointToimg\pointToimg\main.cpp 11
这个错误是因为你传入的点云类型是pcl::PointXYZI,而PCLVisualizer期望的是pcl::PointXYZ类型的点云。你需要将点云类型转换为pcl::PointXYZ类型的点云,才能调用PCLVisualizer的addPointCloud函数。
以下是一个示例代码,将pcl::PointXYZI类型的点云转换为pcl::PointXYZ类型的点云:
```c++
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
pcl::io::loadPCDFile("input.pcd", *cloud);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz(new pcl::PointCloud<pcl::PointXYZ>);
pcl::copyPointCloud(*cloud, *cloud_xyz);
```
然后,你可以将转换后的点云传递给PCLVisualizer的addPointCloud函数:
```c++
viewer->addPointCloud(cloud_xyz, "cloud");
```
完整代码:
```c++
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <opencv2/opencv.hpp>
int main(int argc, char** argv) {
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
pcl::io::loadPCDFile("input.pcd", *cloud);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz(new pcl::PointCloud<pcl::PointXYZ>);
pcl::copyPointCloud(*cloud, *cloud_xyz);
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Cloud Viewer"));
viewer->addPointCloud(cloud_xyz, "cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");
viewer->setBackgroundColor(1, 1, 1);
pcl::visualization::LookupTable lut;
lut.insert(std::make_pair(0.0, cv::Vec3b(0, 0, 0)));
lut.insert(std::make_pair(1.0, cv::Vec3b(255, 255, 255)));
cv::Mat intensity_image(cloud->height, cloud->width, CV_8UC3);
for (int y = 0; y < intensity_image.rows; y++) {
for (int x = 0; x < intensity_image.cols; x++) {
float intensity = cloud->at(x, y).intensity;
cv::Vec3b color = lut.getColor(intensity);
intensity_image.at<cv::Vec3b>(y, x) = color;
}
}
cv::imwrite("output.png", intensity_image);
return 0;
}
```
pcl::PointCloud<pcl::PointXYZI>::Ptr pointcloud_in(new pcl::PointCloud<pcl::PointXYZI>());去掉小于2m以下的点,给出C++代码
Sure! Here is the C++ code to remove points below 2m from a pcl::PointCloud<pcl::PointXYZI>::Ptr named "pointcloud_in":
```cpp
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
pcl::PointCloud<pcl::PointXYZI>::Ptr removePointsBelow2m(pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_in)
{
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_out(new pcl::PointCloud<pcl::PointXYZI>);
for (const auto& point : cloud_in->points)
{
if (point.z >= 2.0) // Check if the point's z-coordinate is greater than or equal to 2m
{
cloud_out->points.push_back(point);
}
}
cloud_out->width = cloud_out->points.size();
cloud_out->height = 1;
cloud_out->is_dense = true;
return cloud_out;
}
int main()
{
pcl::PointCloud<pcl::PointXYZI>::Ptr pointcloud_in(new pcl::PointCloud<pcl::PointXYZI>);
// Populate the point cloud with data
pcl::PointCloud<pcl::PointXYZI>::Ptr pointcloud_filtered = removePointsBelow2m(pointcloud_in);
// Use the filtered point cloud
return 0;
}
```
In this code, the `removePointsBelow2m` function takes a `pcl::PointCloud<pcl::PointXYZI>::Ptr` as input and returns a new filtered point cloud where points below 2m are removed. The function iterates over each point in the input cloud and checks if its z-coordinate is greater than or equal to 2.0. If it is, the point is added to the output cloud.
Please note that you need to populate the `pointcloud_in` with actual data before calling the `removePointsBelow2m` function.
阅读全文