#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int *rand_rgb(){
int *rgb = new int[3];
rgb[0] = rand() % 255;
rgb[1] = rand() % 255;
rgb[2] = rand() % 255;
return rgb;
}
typedef pcl::PointXYZ PointT;
typedef pcl::PointXYZRGBA Pointcolor;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCLPointCloud2* clouddd = new pcl::PCLPointCloud2;
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile("./cloud_cluster_0.pcd", *cloud) == -1)
{
PCL_ERROR("Cloudn't read file!");
system("pause");
return -1;
}
std::cout << "Reader Normal" << std::endl;
std::cout << "PointCloud before filtering has: " << cloud->points.size () << " data points." << std::endl;
pcl::PassThrough<PointT> pass;
pass.setInputCloud (cloud);
pass.setFilterFieldName ("z");
pass.setFilterLimits (0, 100);
pass.filter (*cloud);
pcl::VoxelGrid<pcl::PointXYZ> vg;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
vg.setInputCloud (cloud);
vg.setLeafSize (0.01f, 0.01f, 0.01f);
vg.filter (*cloud_filtered);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filteredyc (new pcl::PointCloud<pcl::PointXYZ>);
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
sor.setInputCloud (cloud_filtered);
sor.setMeanK (25);
sor.setStddevMulThresh (1.0);
sor.filter (*cloud_filteredyc);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
tree->setInputCloud(cloud_filteredyc);
std::vector<pcl::PointIndices> cluster_indices;
pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
ec.setClusterTolerance (1.5);
ec.setMinClusterSize (10);
ec.setMaxClusterSize (1000);
ec.setSearchMethod (tree);
ec.setInputCloud (cloud_filteredyc);
ec.extract (cluster_indices);
pcl::PointCloud<Pointcolor>::Ptr cloudclour;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0,0,0);
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
std::string index_num;
int index_num_int = 0;
for(auto x:cluster_indices)
{
std::stringstream ssTemp;
ssTemp<<index_num_int++;
index_num=ssTemp.str();
typename pcl::PointCloud<PointT>::Ptr cloud_temp(new pcl::PointCloud<PointT>);
for(auto p:x.indices)
cloud_temp->points.push_back(cloud_filteredyc->points[p]);
int *rgb1 = rand_rgb();
pcl::MomentOfInertiaEstimation <pcl::PointXYZ> feature_extractor;
feature_extractor.setInputCloud(cloud_temp);
feature_extractor.compute();
std::vector <float> moment_of_inertia;
std::vector <float> eccentricity;
pcl::PointXYZ min_point_AABB;
pcl::PointXYZ max_point_AABB;
float major_value, middle_value, minor_value;
Eigen::Vector3f major_vector, middle_vector, minor_vector;
Eigen::Vector3f mass_center;
feature_extractor.getMomentOfInertia(moment_of_inertia);
feature_extractor.getEccentricity(eccentricity);
feature_extractor.getAABB(min_point_AABB, max_point_AABB);
feature_extractor.getEigenValues(major_value, middle_value, minor_value);
feature_extractor.getEigenVectors(major_vector, middle_vector, minor_vector);
feature_extractor.getMassCenter(mass_center);
viewer->addCube(min_point_AABB.x, max_point_AABB.x, min_point_AABB.y, max_point_AABB.y, min_point_AABB.z, max_point_AABB.z, rgb1[0],rgb1[1], rgb1[2], "AABB"+index_num);
viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY,0.1, "AABB"+index_num);
viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_LINE_WIDTH,4, "AABB"+index_num);
viewer->addPointCloud<pcl::PointXYZ>(cloud_temp,pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloud_temp, rgb1[0],rgb1[1], rgb1[2]), "sample cloud"+index_num);
delete[]rgb1;
}
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return (0);
}
效果图