• win10环境下PCL安装和配置回顾(一)


    2020年博主有写过几篇关于PCL的博客,这边想先回顾下,再增加点新内容。之前写的博客专栏如下链接。

    https://blog.csdn.net/jiugeshao/category_11993239.html?spm=1001.2014.3001.5482https://blog.csdn.net/jiugeshao/category_11993239.html?spm=1001.2014.3001.5482

    一.PCL官网资料

    之前多篇博客中都提过,要善于利用官方去学习一个新的东西,好的东西,其官网大多必然讲些详细。现在官网的一些资源都会放到github上,比如之前博主讲到的OpenVINO

    OpenVINO使用介绍(一)_竹叶青lvye的博客-CSDN博客_openvino

    OpenVINO示例介绍_竹叶青lvye的博客-CSDN博客_openvino 示例

    PLC的官网链接如下:

    Point Cloud Library | The Point Cloud Library (PCL) is a standalone, large scale, open project for 2D/3D image and point cloud processing.

     Docs入口是API的介绍文档,PCL里模块分的很清楚,若想直接看对应模块的API,也可以直接从点击大方对应图标进入。

    Tutorials是详细的手册介绍,里面还会有一些示例,方便整体知道如何去使用,了解一些算子原理

    github网址如下

     GitHub - PointCloudLibrary/pcl: Point Cloud Library (PCL)

     可看到最新的版本是1.12.1

    tags可以下载到常用的平台下源码编译出来的安装文件,如果没有满足自己平台的,那么可以自己去编译下。(源码编译和交叉编译方法可以参考如下两篇博客),这里不再赘叙。

    Ubuntu下源码编译OpenCV静态库及使用(勾选WITH_QT)_竹叶青lvye的博客-CSDN博客_opencv静态库使用

    Ubuntu下交叉编译OpenCV(WITH_QT)_竹叶青lvye的博客-CSDN博客_opencv编译with_qt

    二. PCL安装和配置

    这一步主要可参考之前的博客:

    vs2017配置PCL1.8.1 QT5.12.1(一)- 配置步骤_竹叶青lvye的博客-CSDN博客_vs2017配置pcl

     这里博主选择了新的版本,vs2019 + PCL1.12.1

     这篇博客需要补充的地方是,中间在输入目标链接库名字时,可以用python脚本批量去获得lib库的名字。

     脚本如下:

    1. # author:"jiugeshao"
    2. # data:2022/9/3 2:16
    3. import os
    4. def ListFilesToTxt(Currentdir, outfile, nameExtentisons, isIncludeSubDir, isDebug):
    5. """
    6. :param Currentdir: 需要统计的目录
    7. :param outfile: 需要输出到的txt文件
    8. :param nameExtentisons: 后缀列表
    9. :param isIncludeSubDir: 是否需要统计子目录
    10. :return:
    11. """
    12. exts = nameExtentisons.split(" ")
    13. files = os.listdir(Currentdir)
    14. for name in files:
    15. fullname = os.path.join(Currentdir, name)
    16. if (os.path.isdir(fullname) & isIncludeSubDir):
    17. ListFilesToTxt(fullname, outfile, nameExtentisons, isIncludeSubDir, isDebug)
    18. else:
    19. for ext in exts:
    20. if (name.endswith(ext)):
    21. if(isDebug == True):
    22. if("gd" in name or "d.lib" in name):
    23. outfile.write(name + "\n")
    24. break
    25. else:
    26. if("gd" not in name or "d.lib" in name):
    27. outfile.write(name + "\n")
    28. break
    29. def Read(Currentdir, outfile, isDebug):
    30. """
    31. :param Currentdir:需要统计的目录
    32. :param outfile:需要输出到的txt文本
    33. :return:
    34. """
    35. nameExtensons = ".lib" #若要支持多种后缀统计,可以空格隔开,再加其它后缀名
    36. file = open(outfile,"w")
    37. if not file:
    38. print("cannot open the fule %s" % outfile)
    39. ListFilesToTxt(Currentdir, file, nameExtensons, 1, isDebug)
    40. file.close()
    41. dir = "C:\\Program Files\\PCL 1.12.1\\3rdParty"
    42. debug_txt = "D:\\PCL\\debug_3rdparty.txt"
    43. release_txt = "D:\\PCL\\release_3rdparty.txt"
    44. Read(dir, debug_txt, isDebug=True)
    45. Read(dir, release_txt, isDebug=False)

    如上是去获取3rdParty目录及其子目录下满足条件设置的lib(debug版本和release版本也都做了区分)。生成的debug_3rdparty.txt和release_3rdparty.txt文本如下:

                  

     大家可在此脚本基础上做其它演变。

    测试工程执行效果如下(工程也上传百度网盘):

    链接:https://pan.baidu.com/s/1RFLs3zvOIVR3ao_wR0lVfA 
    提取码:u4l9 
    c++代码如下:

    1. // test_vtk63.cpp : 定义控制台应用程序的入口点。
    2. //
    3. #include
    4. #include
    5. #include "vtkAutoInit.h"
    6. //VTK_MODULE_INIT(vtkRenderingOpenGL2);
    7. //VTK_MODULE_INIT(vtkInteractionStyle);
    8. //#define vtkRenderingCore_AUTOINIT 2(vtkRenderingOpenGL2, vtkInteractionStyle)
    9. #include
    10. #include
    11. #include
    12. #include //pcd 读写类相关的头文件。
    13. #include
    14. #include
    15. #include
    16. #include
    17. using namespace pcl;
    18. using namespace pcl::io;
    19. using namespace std;
    20. int testpointcloudToPcd()
    21. {
    22. vtkObject::GlobalWarningDisplayOff();
    23. pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
    24. char strfilepath[256] = "D:\\PCL\\rabbit.pcd";
    25. //第一种读入方法较多场合如此
    26. if (-1 == pcl::io::loadPCDFile(strfilepath, *cloud)) {
    27. cout << "error input!" << endl;
    28. return -1;
    29. }
    30. boost::shared_ptr viewer(new pcl::visualization::PCLVisualizer("PointsCloud Recognition"));
    31. viewer->addPointCloud(cloud, "model_cloud");
    32. viewer->addCoordinateSystem(1.0);
    33. viewer->initCameraParameters();
    34. while (!viewer->wasStopped())
    35. {
    36. viewer->spinOnce(100);
    37. boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    38. }
    39. }
    40. int _tmain(int argc, _TCHAR* argv[])
    41. {
    42. testpointcloudToPcd();
    43. return 0;
    44. }

    运行效果如下: 

    此版本对应的VTK版本是9.1

    三.在VTK控件中显示PCL点云

    1.直接用vtk去显示自定义的一个锥体

    1. // test_vtk63.cpp : 定义控制台应用程序的入口点。
    2. //
    3. #include
    4. #include
    5. #include "vtkConeSource.h"
    6. #include "vtkConeSource.h"
    7. #include "vtkCommand.h"
    8. #include "vtkCamera.h"
    9. #include "vtkRenderWindowInteractor.h"
    10. #include "vtkTransform.h"
    11. #include "vtkInteractorStyleTrackballCamera.h"
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. #include
    22. #include
    23. #include
    24. #include
    25. #include
    26. #include
    27. #include
    28. #include
    29. #include
    30. #include
    31. #include
    32. #include
    33. #include
    34. #include
    35. #include
    36. #include
    37. #include
    38. #include
    39. #include
    40. VTK_MODULE_INIT(vtkRenderingOpenGL);
    41. VTK_MODULE_INIT(vtkRenderingFreeType);
    42. VTK_MODULE_INIT(vtkInteractionStyle);
    43. void testvtk2()
    44. {
    45. //创建数据源:创建一个圆锥,并设置其参数:高度、底面半径和分辨率
    46. vtkConeSource* cone = vtkConeSource::New();
    47. cone->SetHeight(3.0);
    48. cone->SetRadius(1.0);
    49. cone->SetResolution(100);
    50. //映射器:创建一个多边形映射器,用于把多边形数据映射为可以被计算机渲染的图元
    51. vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
    52. coneMapper->SetInputConnection(cone->GetOutputPort());
    53. //创建一个Actor,并关联一个映射器,从而确定Actor的形状
    54. vtkActor* coneActor = vtkActor::New();
    55. coneActor->SetMapper(coneMapper);
    56. //方法1:设置演员颜色
    57. coneActor->GetProperty()->SetColor(1.0, 0.0, 0.0);//设置对象颜色为红色
    58. //方法2:设置演员颜色
    59. //vtkProperty* coneProperty = vtkProperty::New();//设置一个Property对象
    60. //coneProperty->SetColor(1.0, 0.0, 0.0);
    61. //coneActor->SetProperty(coneProperty);
    62. //渲染器:创建一个渲染器,添加要渲染的演员
    63. vtkRenderer* ren1 = vtkRenderer::New();
    64. ren1->AddActor(coneActor);
    65. ren1->SetBackground(0.1, 0.2, 0.4);
    66. //渲染窗口:添加一个渲染窗口,供渲染器使用
    67. vtkRenderWindow* renWin = vtkRenderWindow::New();
    68. renWin->AddRenderer(ren1);
    69. renWin->SetSize(300, 300);
    70. //实例化一个交互对象
    71. vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
    72. interactor->SetRenderWindow(renWin);
    73. renWin->Render();//绘制舞台上的东西
    74. interactor->Initialize();//交互对象初始化
    75. interactor->Start();//开始交互事件
    76. //释放内存
    77. cone->Delete();
    78. coneActor->Delete();
    79. coneMapper->Delete();
    80. ren1->Delete();
    81. renWin->Delete();
    82. interactor->Delete();
    83. }
    84. int _tmain(int argc, _TCHAR* argv[])
    85. {
    86. testvtk2();
    87. return 0;
    88. }

    运行出现如下报错:

    严重性    代码    说明    项目    文件    行    禁止显示状态
    错误    LNK2019    无法解析的外部符号 "void __cdecl vtkRenderingOpenGL_AutoInit_Construct(void)" (?vtkRenderingOpenGL_AutoInit_Construct@@YAXXZ),函数 "public: __cdecl `anonymous namespace'::vtkRenderingOpenGL_ModuleInit::vtkRenderingOpenGL_ModuleInit(void)" (??0vtkRenderingOpenGL_ModuleInit@?A0xfeadf992@@QEAA@XZ) 中引用了该符号    PCL_config_console    D:\PCL\code\PCL_config_console\PCL_config_console\PCL_config_console.obj    1    
     

    将代码中的VTK_MODULE_INIT(vtkRenderingOpenGL)修改为VTK_MODULE_INIT(vtkRenderingOpenGL2),即可!

     2.pcl读入点云,在vtk控件中显示

    代码如下:

    1. // test_vtk63.cpp : 定义控制台应用程序的入口点。
    2. //
    3. // test_mfc_showDlg.h: 头文件
    4. //
    5. #include
    6. #include "vtkAutoInit.h"
    7. VTK_MODULE_INIT(vtkRenderingOpenGL2);
    8. VTK_MODULE_INIT(vtkInteractionStyle);
    9. #include //多线程
    10. #include
    11. #include
    12. #include //pcd 读写类相关的头文件。
    13. #include
    14. #include
    15. #include //PCL中支持的点类型头文件。
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. #include
    22. #include "vtkCommand.h"
    23. #include "vtkRenderer.h"
    24. #include "vtkCellPicker.h"
    25. #include "vtkProperty.h"
    26. #include "vtkPlaneSource.h"
    27. #include "vtkMetaImageReader.h"
    28. #include "vtkImagePlaneWidget.h"
    29. #include "vtkPlaneSource.h"
    30. #include "vtkPlane.h"
    31. #include "vtkSmartPointer.h"
    32. #include "vtkResliceCursorActor.h"
    33. #include "vtkResliceCursorPolyDataAlgorithm.h"
    34. #include "vtkResliceCursor.h"
    35. #include "vtkResliceCursorWidget.h"
    36. #include "vtkResliceCursorLineRepresentation.h"
    37. #include "vtkResliceCursorThickLineRepresentation.h"
    38. #include
    39. #include "vtkImageReader2Factory.h"
    40. #include "vtkImageReader2.h"
    41. using namespace pcl;
    42. using namespace pcl::io;
    43. using namespace std;
    44. using pcl::visualization::PointCloudColorHandlerGenericField;
    45. using pcl::visualization::PointCloudColorHandlerCustom;
    46. void testvtk2()
    47. {
    48. pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
    49. char strfilepath[256] = "D:\\PCL\\rabbit.pcd";
    50. std::cout << "start to read" << std::endl;
    51. //第一种读入方法j较多场合如此
    52. if (-1 == pcl::io::loadPCDFile(strfilepath, *cloud)) {
    53. cout << "error input!" << endl;
    54. }
    55. cout << cloud->points.size() << endl;
    56. vtkSmartPointer points = vtkSmartPointer::New();//key code
    57. vtkIdType size = cloud->points.size();
    58. int n = 0;
    59. for (vtkIdType rowId = 0; rowId < size; rowId++)
    60. {
    61. auto dp = cloud->points.at(rowId);
    62. if (dp.z == 0) {
    63. continue;
    64. }
    65. points->InsertNextPoint(dp.x, dp.y, dp.z);//key code
    66. n++;
    67. }
    68. vtkSmartPointer polydata = vtkSmartPointer::New();
    69. polydata->SetPoints(points);
    70. vtkSmartPointer glyphFilter = vtkSmartPointer::New();
    71. glyphFilter->SetInputData(polydata);
    72. glyphFilter->Update();
    73. vtkPolyDataMapper* dataMapper = vtkPolyDataMapper::New();
    74. dataMapper->SetInputConnection(glyphFilter->GetOutputPort());
    75. vtkActor* coneActor = vtkActor::New();
    76. coneActor->SetMapper(dataMapper);
    77. coneActor->GetProperty()->SetColor(1.0, 0.0, 0.0);//设置对象颜色为红色
    78. //方法2:设置演员颜色
    79. //vtkProperty* coneProperty = vtkProperty::New();//设置一个Property对象
    80. //coneProperty->SetColor(1.0, 0.0, 0.0);
    81. //coneActor->SetProperty(coneProperty);
    82. //渲染器:创建一个渲染器,添加要渲染的演员
    83. vtkSmartPointer m_Renderer;
    84. vtkSmartPointer m_RenderWindow;
    85. m_Renderer = vtkSmartPointer::New();
    86. m_Renderer->AddActor(coneActor);
    87. m_Renderer->SetBackground(0.1, 0.2, 0.4);
    88. m_RenderWindow = vtkSmartPointer::New();
    89. m_RenderWindow->SetSize(500, 500);
    90. m_RenderWindow->AddRenderer(m_Renderer);
    91. vtkRenderWindowInteractor * interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
    92. interactor->SetRenderWindow(m_RenderWindow);
    93. m_RenderWindow->Render();//绘制舞台上的东西
    94. interactor->Initialize();//交互对象初始化
    95. interactor->Start();//开始交互事件
    96. polydata->Delete();
    97. glyphFilter->Delete();
    98. dataMapper->Delete();
    99. coneActor->Delete();
    100. m_Renderer->Delete();
    101. m_RenderWindow->Delete();
    102. interactor->Delete();
    103. //if (m_RenderWindow->GetInteractor() == NULL)
    104. //{
    105. // vtkSmartPointer RenderWindowInteractor =
    106. // vtkSmartPointer::New();
    107. // RenderWindowInteractor->SetRenderWindow(m_RenderWindow);
    108. // RenderWindowInteractor->Initialize();
    109. //}
    110. }
    111. int _tmain(int argc, _TCHAR* argv[])
    112. {
    113. testvtk2();
    114. return 0;
    115. }

    vtk中显示的效果如下:

    3.VTK保存数据为vtk文件

    在上面代码的基础上,增加头文件#include

    在增加如下代码(polydata->SetPoints(points)语句下面),即可以保存数据到vtk文件中

    4.VTK API读取本地vtk文件,并在自己控件里显示

    代码如下:

    1. // test_vtk63.cpp : 定义控制台应用程序的入口点。
    2. //
    3. // test_mfc_showDlg.h: 头文件
    4. //
    5. #include
    6. #include "vtkAutoInit.h"
    7. VTK_MODULE_INIT(vtkRenderingOpenGL2);
    8. VTK_MODULE_INIT(vtkInteractionStyle);
    9. #include //多线程
    10. #include
    11. #include
    12. #include //pcd 读写类相关的头文件。
    13. #include
    14. #include
    15. #include //PCL中支持的点类型头文件。
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. #include
    22. #include
    23. #include "vtkCommand.h"
    24. #include "vtkRenderer.h"
    25. #include "vtkCellPicker.h"
    26. #include "vtkProperty.h"
    27. #include "vtkPlaneSource.h"
    28. #include "vtkMetaImageReader.h"
    29. #include "vtkImagePlaneWidget.h"
    30. #include "vtkPlaneSource.h"
    31. #include "vtkPlane.h"
    32. #include "vtkSimplePointsReader.h"
    33. #include "vtkSmartPointer.h"
    34. #include "vtkResliceCursorActor.h"
    35. #include "vtkResliceCursorPolyDataAlgorithm.h"
    36. #include "vtkResliceCursor.h"
    37. #include "vtkResliceCursorWidget.h"
    38. #include "vtkResliceCursorLineRepresentation.h"
    39. #include "vtkResliceCursorThickLineRepresentation.h"
    40. #include
    41. #include "vtkImageReader2Factory.h"
    42. #include "vtkImageReader2.h"
    43. #include
    44. using namespace pcl;
    45. using namespace pcl::io;
    46. using namespace std;
    47. using pcl::visualization::PointCloudColorHandlerGenericField;
    48. using pcl::visualization::PointCloudColorHandlerCustom;
    49. void testvtk2()
    50. {
    51. vtkNew reader;
    52. reader->SetFileName("D:\\PCL\\rabbit.vtk");
    53. reader->Update();
    54. vtkSmartPointer polydata = vtkSmartPointer::New();
    55. polydata->SetPoints(reader->GetOutput()->GetPoints());
    56. vtkSmartPointer glyphFilter = vtkSmartPointer::New();
    57. glyphFilter->SetInputData(polydata);
    58. glyphFilter->Update();
    59. vtkPolyDataMapper* dataMapper = vtkPolyDataMapper::New();
    60. dataMapper->SetInputConnection(glyphFilter->GetOutputPort());
    61. vtkActor* coneActor = vtkActor::New();
    62. coneActor->SetMapper(dataMapper);
    63. coneActor->GetProperty()->SetColor(1.0, 0.0, 0.0);//设置对象颜色为红色
    64. //方法2:设置演员颜色
    65. //vtkProperty* coneProperty = vtkProperty::New();//设置一个Property对象
    66. //coneProperty->SetColor(1.0, 0.0, 0.0);
    67. //coneActor->SetProperty(coneProperty);
    68. //渲染器:创建一个渲染器,添加要渲染的演员
    69. vtkSmartPointer m_Renderer;
    70. vtkSmartPointer m_RenderWindow;
    71. m_Renderer = vtkSmartPointer::New();
    72. m_Renderer->AddActor(coneActor);
    73. m_Renderer->SetBackground(0.1, 0.2, 0.4);
    74. m_RenderWindow = vtkSmartPointer::New();
    75. m_RenderWindow->SetSize(500, 500);
    76. m_RenderWindow->AddRenderer(m_Renderer);
    77. vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
    78. interactor->SetRenderWindow(m_RenderWindow);
    79. m_RenderWindow->Render();//绘制舞台上的东西
    80. interactor->Initialize();//交互对象初始化
    81. interactor->Start();//开始交互事件
    82. polydata->Delete();
    83. glyphFilter->Delete();
    84. dataMapper->Delete();
    85. coneActor->Delete();
    86. m_Renderer->Delete();
    87. m_RenderWindow->Delete();
    88. interactor->Delete();
    89. //if (m_RenderWindow->GetInteractor() == NULL)
    90. //{
    91. // vtkSmartPointer RenderWindowInteractor =
    92. // vtkSmartPointer::New();
    93. // RenderWindowInteractor->SetRenderWindow(m_RenderWindow);
    94. // RenderWindowInteractor->Initialize();
    95. //}
    96. }
    97. int _tmain(int argc, _TCHAR* argv[])
    98. {
    99. testvtk2();
    100. return 0;
    101. }

    执行效果如下: 

     5.VTK保存数据到txt文本中

    在上面代码的基础上增加如下代码(polydata->SetPoints(reader->GetOutput()->GetPoints()):

    6.VTK读txt数据并转化为vtk数据

    代码如下,读入一个txt文本,然后进行数据的转换

    1. // test_vtk63.cpp : 定义控制台应用程序的入口点。
    2. //
    3. // test_mfc_showDlg.h: 头文件
    4. //
    5. #include
    6. #include "vtkAutoInit.h"
    7. VTK_MODULE_INIT(vtkRenderingOpenGL2);
    8. VTK_MODULE_INIT(vtkInteractionStyle);
    9. #include //多线程
    10. #include
    11. #include
    12. #include //pcd 读写类相关的头文件。
    13. #include
    14. #include
    15. #include //PCL中支持的点类型头文件。
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. #include
    22. #include "vtkCommand.h"
    23. #include "vtkRenderer.h"
    24. #include "vtkCellPicker.h"
    25. #include "vtkProperty.h"
    26. #include "vtkPlaneSource.h"
    27. #include "vtkMetaImageReader.h"
    28. #include "vtkImagePlaneWidget.h"
    29. #include "vtkPlaneSource.h"
    30. #include "vtkPlane.h"
    31. #include "vtkSmartPointer.h"
    32. #include "vtkResliceCursorActor.h"
    33. #include "vtkResliceCursorPolyDataAlgorithm.h"
    34. #include "vtkResliceCursor.h"
    35. #include "vtkResliceCursorWidget.h"
    36. #include "vtkResliceCursorLineRepresentation.h"
    37. #include "vtkResliceCursorThickLineRepresentation.h"
    38. #include
    39. #include "vtkImageReader2Factory.h"
    40. #include "vtkImageReader2.h"
    41. using namespace pcl;
    42. using namespace pcl::io;
    43. using namespace std;
    44. using pcl::visualization::PointCloudColorHandlerGenericField;
    45. using pcl::visualization::PointCloudColorHandlerCustom;
    46. void testvtk2()
    47. {
    48. //istringstream istr;
    49. //string line, str;
    50. //while (getline(cin, line))//从终端接收一行字符串,并放入字符串line中
    51. //{
    52. // istr.str(line);//把line中的字符串存入字符串流中
    53. // while (istr >> str)//每次读取一个单词(以空格为界),存入str中
    54. // {
    55. // cout << str << endl;
    56. // }
    57. //}
    58. //std::string filename = "Points.txt";
    59. // std::ifstream filestream(filename.c_str()); //文件流
    60. // std::string line;
    61. // vtkSmartPointer points = vtkSmartPointer::New();
    62. // while(std::getline(filestream, line)) //整行读取文件
    63. //{
    64. // double x, y, z;
    65. // std::stringstream linestream;
    66. // linestream << line;
    67. // linestream >> x >> y >> z;
    68. // points->InsertNextPoint(x, y, z); //新读取的数据赋予点的几何结构
    69. // }
    70. // filestream.close(); //关闭文件流操作
    71. vtkSmartPointer points = vtkSmartPointer::New();//key code
    72. ifstream inFile;
    73. inFile.open("D:\\PCL\\rabbit.txt");
    74. double x, y, z;
    75. while (inFile >> x >> y >> z)
    76. {
    77. points->InsertNextPoint(x, y, z);
    78. }
    79. vtkSmartPointer polydata = vtkSmartPointer::New();
    80. polydata->SetPoints(points);
    81. vtkSmartPointer glyphFilter = vtkSmartPointer::New();
    82. glyphFilter->SetInputData(polydata);
    83. glyphFilter->Update();
    84. vtkPolyDataMapper* dataMapper = vtkPolyDataMapper::New();
    85. dataMapper->SetInputConnection(glyphFilter->GetOutputPort());
    86. vtkActor* coneActor = vtkActor::New();
    87. coneActor->SetMapper(dataMapper);
    88. coneActor->GetProperty()->SetColor(1.0, 0.0, 0.0);//设置对象颜色为红色
    89. //方法2:设置演员颜色
    90. //vtkProperty* coneProperty = vtkProperty::New();//设置一个Property对象
    91. //coneProperty->SetColor(1.0, 0.0, 0.0);
    92. //coneActor->SetProperty(coneProperty);
    93. //渲染器:创建一个渲染器,添加要渲染的演员
    94. vtkSmartPointer m_Renderer;
    95. vtkSmartPointer m_RenderWindow;
    96. m_Renderer = vtkSmartPointer::New();
    97. m_Renderer->AddActor(coneActor);
    98. m_Renderer->SetBackground(0.1, 0.2, 0.4);
    99. m_RenderWindow = vtkSmartPointer::New();
    100. m_RenderWindow->SetSize(500, 500);
    101. m_RenderWindow->AddRenderer(m_Renderer);
    102. vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
    103. interactor->SetRenderWindow(m_RenderWindow);
    104. m_RenderWindow->Render();//绘制舞台上的东西
    105. interactor->Initialize();//交互对象初始化
    106. interactor->Start();//开始交互事件
    107. polydata->Delete();
    108. glyphFilter->Delete();
    109. dataMapper->Delete();
    110. coneActor->Delete();
    111. m_Renderer->Delete();
    112. m_RenderWindow->Delete();
    113. interactor->Delete();
    114. //if (m_RenderWindow->GetInteractor() == NULL)
    115. //{
    116. // vtkSmartPointer RenderWindowInteractor =
    117. // vtkSmartPointer::New();
    118. // RenderWindowInteractor->SetRenderWindow(m_RenderWindow);
    119. // RenderWindowInteractor->Initialize();
    120. //}
    121. }
    122. int _tmain(int argc, _TCHAR* argv[])
    123. {
    124. testvtk2();
    125. return 0;
    126. }

    显示效果如下:

    7.PCL读入txt数据显示

    代码如下:

    1. // test_vtk63.cpp : 定义控制台应用程序的入口点。
    2. //
    3. #include
    4. #include
    5. #include "vtkAutoInit.h"
    6. //VTK_MODULE_INIT(vtkRenderingOpenGL2);
    7. //VTK_MODULE_INIT(vtkInteractionStyle);
    8. //#define vtkRenderingCore_AUTOINIT 2(vtkRenderingOpenGL2, vtkInteractionStyle)
    9. #include
    10. #include
    11. #include
    12. #include //pcd 读写类相关的头文件。
    13. #include
    14. #include
    15. #include
    16. #include
    17. using namespace pcl;
    18. using namespace pcl::io;
    19. using namespace std;
    20. int testpointcloudToPcd()
    21. {
    22. pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
    23. ifstream in0;
    24. in0.open("D:\\PCL\\rabbit.txt");
    25. if (!in0.is_open())
    26. {
    27. cout << "error open!" << endl;
    28. return -1;
    29. }
    30. //a,b为符号
    31. int count = 0;
    32. double x, y, z;
    33. while (in0 >> x >> y >> z)
    34. {
    35. pcl::PointXYZ cltmp(x, y, z);
    36. cloud->points.push_back(cltmp);
    37. count++;
    38. }
    39. std::cout << "start to save pointcloud...";
    40. cloud->width = count; //设置点云宽度
    41. cloud->height = 1; //设置点云高度
    42. cloud->is_dense = false;//判断points中的数据是否是有限的(有限为true)或者说是判断点云中的点是否包含 Inf/NaN这种值(包含为false)。
    43. //为false或者true并不影响结果,是否为密度点云
    44. //pcl::io::savePCDFileASCII("D:\\mycode\\0-PCL_VTK\\pryamid_yidong10.pcd", *cloud);
    45. /*pcl::io::savePCDFileBinary("D:\\mycode\\0-PCL_VTK\\pryamid1.pcd", *cloud);
    46. pcl::io::savePCDFile("D:\\mycode\\0-PCL_VTK\\pryamid2.pcd", *cloud);*/
    47. std::cout << "save completed!start to show" << std::endl;
    48. boost::shared_ptr viewer(new pcl::visualization::PCLVisualizer("PointsCloud Recognition"));
    49. viewer->addPointCloud(cloud, "model_cloud");
    50. viewer->addCoordinateSystem(1.0);
    51. viewer->initCameraParameters();
    52. while (!viewer->wasStopped())
    53. {
    54. viewer->spinOnce(100);
    55. boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    56. }
    57. }
    58. int _tmain(int argc, _TCHAR* argv[])
    59. {
    60. testpointcloudToPcd();
    61. return 0;
    62. }

    8.常见点云文件格式介绍

    这边列举了一些博客用于参考

    (1)PCD

    点云库PCL入门1:存储点云数据的pcd文件简介 - 知乎

    PCL:点云数据(*.pcd)文件格式详解_孙 悟 空的博客-CSDN博客_pcd格式

    PCL中PCD(Point Cloud Data)文件格式解析_SOC罗三炮的博客-CSDN博客_pcd文件

    (2) PLY

    PLY文件格式 - 慕尘 - 博客园

    (3) Step

    一篇文章了解STEP文件格式 - 知乎

    https://www.jianshu.com/p/ccfc77d0ee45

    9.PCL读写点云文件

    这边列举了一些博客用于参考

    PCL读写pcd文件 - 知乎

    PCL:读写pcd点云的两种方式_孙 悟 空的博客-CSDN博客_pcl读取pcd

    ply las pcd等一些常见的基本点云文件格式的读写方法_qq_46084757的博客-CSDN博客_ply点云

    PCL入门系列三——PCL进行数据读写 - 知乎

    点云学习----PCL读写点云_刚上路DE小白的博客-CSDN博客_pcl读取点云

    10.VTK数据处理

    VTK图像处理(二)--vtkPolyData数据处理 - 知乎

    11. 八叉树

    如下的一些帖子,博主觉得可参考

    百度安全验证

    PCL学习笔记(六)-- k-d tree_看到我请叫我学C++的博客-CSDN博客

    PCL学习笔记(七)-- 八叉树OcTree_八叉树写法_看到我请叫我学C++的博客-CSDN博客

    https://www.cnblogs.com/z-web-2017/p/14111071.html

    【图形学】基于点云生成树木模型_树木点云_wk_119的博客-CSDN博客

    一文搞懂k近邻(k-NN)算法(一) - 知乎

  • 相关阅读:
    Anaconda与Jupyter Notebook入门级详细使用教程
    Hyperledger Fabric 2.2 学习笔记:测试网络test-network
    基于深度学习的小学语文“输出驱动”教学研究课题方案
    用IDEA操作数据库--MySQL
    【MySQL】MVCC机制(undo log,read view)
    mysql高阶语句
    SpringBoot电商项目之实现购物车功能
    让开发回归简单模式-组件封装
    Linux crontab 命令定时任务设置
    毅速丨金属3D打印能替代传统制造吗?
  • 原文地址:https://blog.csdn.net/jiugeshao/article/details/126688053