obj物体模型生成点云数据
#include <iostream>
#include <vector>
#include <ctime>
//---------------------------------------------
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_lib_io.h> //obj读取头文件
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/features/normal_3d.h>
//-法向显示错误:no override found for vtkActor--
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
//outlier
#include <pcl/filters/statistical_outlier_removal.h>
using namespace std;
int main() {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr bottom(new pcl::PointCloud<pcl::PointXYZ>);
//-------------------------------读取点云文件---------------------------------------------------
pcl::PolygonMesh mesh;
if (pcl::io::loadPolygonFile("/home/mi/3rd_package/ch13/pcl_obj_test/bunny_low_resolution.obj", mesh) == -1)
{
cout << "COULD NOT READ FILE mid.pcl \n";
system("pause");
return (-1);
}
pcl::fromPCLPointCloud2(mesh.cloud, *cloud);
cout << "points sieze is:" << cloud->size() << endl;
//outlier
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
sor.setInputCloud(cloud);
sor.setMeanK(200); //设置在进行统计时考虑查询点邻近点数
sor.setStddevMulThresh(1.0); //设置判断是否为离群点的阈值,如果一个点的距离超出平均距离一个标准差以上,则该点被标记为离群点,并将被移除。
sor.filter(*cloud_filtered);
//---------------------------------显示---------------------------------------------------------------------
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D viewer"));
int v1(0);
viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1);//xmin, ymin, xmax, ymax,取值范围0-1
viewer->setBackgroundColor(0, 0, 0, v1);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> green0(cloud, 0, 225, 0);
viewer->addPointCloud(cloud, green0, "cloud", v1);
int v2(0);
viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);
viewer->setBackgroundColor(0.3, 0.3, 0.3, v2);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> green1(cloud_filtered, 0, 225, 0);
viewer->addPointCloud(cloud_filtered, green1, "cloud_filtered", v2);
while (!viewer->wasStopped()) {
viewer->spinOnce(100);
}
system("pause");
return 0;
}
效果如下
生成了一只兔子的点云:
PCL安装过程参考:
https://blog.csdn.net/qq_36728314/article/details/89487719
过程中如果发现
/usr/include/pcl**/pcl/
下没有visualization文件夹
则所名没有安装vtk
-
(在此如果要实现可视化需要跟新vtk版本,可以使用sudo apt-get install libvtk + Tab 补全查找自己需要的版本。
-
我使用的是sudo apt-get install libvtk6.2 libvtk6.2-qt libvtk6-dev)