#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/project_inliers.h>
#include <pcl/visualization/pcl_visualizer.h>
using namespace pcl;
using namespace std;
typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloud;
int main(int argc, char** argv)
{
//*******点云往平面投影的方法**********
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_projected(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile("C:/Users/18148/Desktop/qq3.pcd", *cloud) == -1){
PCL_ERROR("Could not read pcd file!\n");
return -1;
}
std::cerr << "Cloud before projection: " << std::endl;
/*for (size_t i = 0; i < cloud->points.size(); ++i)
std::cerr << " " << cloud->points[i].x << " "
<< cloud->points[i].y << " "
<< cloud->points[i].z << std::endl;
*/
// 定义模型系数对象,并填充对应的数据Create a set of planar coefficients with X=Y=0,Z=1
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
coefficients->values.resize(4);
coefficients->values[0] = 0.5;
coefficients->values[1] = 1;
coefficients->values[2] = 1;
coefficients->values[3] = 0;
// Create the filtering object
pcl::ProjectInliers<pcl::PointXYZ> proj;//创建投影滤波对象
proj.setModelType(pcl::SACMODEL_PLANE);//设置对象对应的投影模型
proj.setInputCloud(cloud);//设置输入点云
proj.setModelCoefficients(coefficients);//设置模型对应的系数
proj.filter(*cloud_projected);//执行投影滤波存储结果cloud_projected
std::cerr << "Cloud after projection: " << std::endl;
//for (size_t i = 0; i < cloud_projected->points.size(); ++i)
// std::cerr << " " << cloud_projected->points[i].x << " "
// << cloud_projected->points[i].y << " "
// << cloud_projected->points[i].z << std::endl;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
//pcl::visualization::PointCloudColorHandlerRGBField<PointT> rgb(cloud_pointsPtr);
pcl::visualization::PointCloudColorHandlerCustom<PointT> blue(cloud, 0, 0, 255);
viewer->addPointCloud<PointT>(cloud, blue, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
pcl::visualization::PointCloudColorHandlerCustom<PointT> red(cloud_projected, 255, 0, 0);
viewer->addPointCloud<PointT>(cloud_projected, red, "sample cloud2");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
system("pause");
return 0;
}
转载自:https://www.cnblogs.com/lovebay/p/9337364.html