VTK入门教程06.04:显示点云模型

目的

基于VTK+PCL显示点云模型

新建空白工程,添加QWidget控件并提升为QVTKOpenGLNativeWidget。

pro

1
2
3
4
5
6
7
8
9
10
11
12
13
INCLUDEPATH += /usr/include/vtk-9.1 /usr/include/pcl-1.12 /usr/include/eigen3

LIBS += -lvtkGUISupportQt-9.1 -lvtksys-9.1 -lvtkInteractionStyle-9.1 \
-lvtkRenderingOpenGL2-9.1 -lvtkRenderingCore-9.1 -lvtkCommonExecutionModel-9.1 \
-lvtkCommonCore-9.1 -lvtkIOGeometry-9.1 -lvtkIOImage-9.1 -lvtkRenderingImage-9.1 \
-lvtkViewsCore-9.1 -lvtkImagingCore-9.1 -lvtkInteractionImage-9.1 -lvtkFiltersSources-9.1 \
-lvtkCommonColor-9.1 -lvtkCommonDataModel-9.1 -lvtkCommonMath-9.1

LIBS += -L/usr/lib/x86_64-linux-gnu/ -lpcl_common -lpcl_features -lpcl_filters \
-lpcl_io -lpcl_io_ply -lpcl_kdtree -lpcl_keypoints -lpcl_ml -lpcl_octree \
-lpcl_outofcore -lpcl_people -lpcl_recognition -lpcl_registration \
-lpcl_sample_consensus -lpcl_search -lpcl_segmentation -lpcl_stereo \
-lpcl_surface -lpcl_tracking -lpcl_visualization

头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <vtkAutoInit.h>
#include <vtkBMPReader.h>
#include <vtkFloatArray.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkImageActor.h>
#include <vtkImageChangeInformation.h>
#include <vtkImageData.h>
#include <vtkImageImport.h>
#include <vtkImageViewer2.h>
#include <vtkInteractorStyleImage.h>
#include <vtkJPEGReader.h>
#include <vtkLookupTable.h>
#include <vtkMetaImageReader.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSTLReader.h>
#include <vtkSmartPointer.h>
#include <vtkXMLImageDataWriter.h>
#include <vtkEventQtSlotConnect.h>
#include <vtkImageBlend.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageIterator.h>
#include <vtkImageLuminance.h>
#include <vtkImageStencil.h>
#include <vtkImageStencilData.h>
#include <vtkImageViewer.h>
#include <vtkPNGReader.h>
#include <vtkSphereSource.h>

#include <iostream>
#include <string>
#include <pcl/point_types.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/PolygonMesh.h>
#include <boost/thread/thread.hpp>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/PolygonMesh.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/io/vtk_io.h>
using namespace pcl;
using namespace pcl::io;
using namespace std;

VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)

读取

1
2
3
4
//初始化点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
//pcl::io::loadPLYFile<pcl::PointXYZ>("Laurana50k.ply", *cloud); //使用点云文件 ply
pcl::io::loadPCDFile("rabbit.pcd", *cloud); //使用点云文件 pcd

渲染

1
2
3
auto renderer2 = vtkSmartPointer<vtkRenderer>::New();
auto renderWindow2 = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
renderWindow2->AddRenderer(renderer2);

窗口

1
2
3
4
//声明视窗
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer(renderer2,renderWindow2,"3D Viewer",false));
//设置视窗背景色
viewer->setBackgroundColor(0, 0, 0);

显示

1
2
3
4
5
6
7
8
9
10
11
//预处理点云颜色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> magenta(cloud, 255, 255, 255);
//把点云加载到视窗
viewer->addPointCloud(cloud, magenta, "cloud");
//设置点云大小
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud");
viewer->resetCamera();
//连接qvtk
ui->widget->setRenderWindow(viewer->getRenderWindow());
viewer->setupInteractor(ui->widget->interactor(),ui->widget->renderWindow());
ui->widget->update();

效果

编译运行程序

兔子点云

这里的兔子点云模型是点云学习的hello world


VTK入门教程06.04:显示点云模型
https://blog.jackeylea.com/vtk/display-point-cloud-with-vtk/
作者
JackeyLea
发布于
2024年8月3日
许可协议