7 std::shared_ptr<std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr>>
EuclideanDetector::cluster_data(
pclPtrType p_pclPtr, std::vector<Object>& p_objectsVecRef,
float clusterTolerance,
int minClusterSize,
int maxClusterSize)
const {
9 auto pointCloudPtrVectorSPtr = std::make_shared<std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr>>();
12 if(p_pclPtr->size() == 0)
return pointCloudPtrVectorSPtr;
14 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(
new pcl::search::KdTree<pcl::PointXYZ>);
15 tree->setInputCloud(p_pclPtr);
17 std::vector<pcl::PointIndices> clusterIndices;
18 pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
19 ec.setClusterTolerance(clusterTolerance);
20 ec.setMinClusterSize(minClusterSize);
21 ec.setMaxClusterSize(maxClusterSize);
22 ec.setSearchMethod(tree);
23 ec.setInputCloud(p_pclPtr);
24 ec.extract(clusterIndices);
27 float sizeX, sizeY, sizeZ;
28 for(std::vector<pcl::PointIndices>::const_iterator it = clusterIndices.begin(); it != clusterIndices.end(); ++it) {
29 pcl::PointCloud<pcl::PointXYZ>::Ptr cloudClusterPtr(
new pcl::PointCloud<pcl::PointXYZ>);
32 for(std::vector<int>::const_iterator pit = it->indices.begin(); pit != it->indices.end(); ++pit) {
33 cloudClusterPtr->points.push_back(p_pclPtr->points[*pit]);
37 pcl::PointXYZ minDimensions;
38 pcl::PointXYZ maxDimensions;
39 pcl::getMinMax3D(*cloudClusterPtr, minDimensions, maxDimensions);
41 sizeX = std::fabs(maxDimensions.x - minDimensions.x);
42 sizeY = std::fabs(maxDimensions.y - minDimensions.y);
43 sizeZ = std::fabs(maxDimensions.z - minDimensions.z);
45 if(cloudClusterPtr->size() < m_postProcessingClusterSizeMin) {
46 log_debug(
"[RealSense] Object too small (min cluster points)");
50 if(cloudClusterPtr->size() > m_postProcessingClusterSizeMax) {
51 log_debug(
"[RealSense] Object too large (max cluster points)");
55 if(sizeX > m_maxSizeX) {
56 log_debug(
"[RealSense] Object too large (x)");
59 if (sizeY > m_maxSizeY) {
60 log_debug(
"[RealSense] Object too large (y)");
63 if(sizeZ > m_maxSizeZ) {
64 log_debug(
"[RealSense] Object too large (z)");
68 if(sizeX < m_minSizeX) {
69 log_debug(
"[RealSense] Object too small (x)");
72 if (sizeY < m_minSizeY) {
73 log_debug(
"[RealSense] Object too small (y)");
76 if(sizeZ < m_minSizeZ) {
77 log_debug(
"[RealSense] Object too small (z)");
81 pcl::PointXYZ centerPoint;
83 int centroidReturnValue = pcl::computeCentroid(*cloudClusterPtr, centerPoint);
90 foundObject.
size = cloudClusterPtr->size();
91 p_objectsVecRef.push_back(foundObject);
93 pointCloudPtrVectorSPtr->push_back(cloudClusterPtr);
96 log_debug(
"Objects Detected: ", p_objectsVecRef.size());
98 return pointCloudPtrVectorSPtr;
102 pcl::VoxelGrid<pcl::PointXYZ> sor;
103 sor.setInputCloud(p_inputCloudPtr);
104 sor.setLeafSize(p_leafSize, p_leafSize, p_leafSize);
105 sor.filter(*p_outputCloudPtr);