ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

PCL学习一

PCL学习一 PCL 官网Point Cloud Library | The Point Cloud Library (PCL) is a standalone, large scale, open project for 2D/3D image and point cloud processing.turtorialsIntroduction — Point Cloud Library 0.0 documentation1不想学习QT2,所以使用另外一种方式C 封装 PCL 成 DLL读取 pcd、滤波、配准、分割输出点坐标数组 float [] /double []C# WinForm/WPF 项目引用ActivizVTK 的 C# 绑定在 C# 窗体拖入RenderWindowControl控件把 PCL 输出的点数组喂给 VTK 渲染支持鼠标旋转、平移、缩放、改点大小、按高程着色、添加坐标系优点成熟稳定大量工控 / 三维检测项目在用完全不碰 Qt缺点需要做 C DLL 封装处理内存安全点云跨语言传递注意内存泄漏重点坑说明Activiz 版本限制Activiz 原生只支持.NET Framework不支持 .NET Core/.NET 5。 如果想用 .NET6不能用 Activiz备选VTKSharpVTK 官方 C# 绑定上手难度更高。内存安全C 里不要直接 new 大块内存交给 C#上面示例用C# 预先分配 float 数组C 只填充数据这是最稳妥方案避免跨堆内存崩溃。性能 百万级点云可以跑千万级点云需要做降采样再传给 C# 渲染否则帧率下降。颜色扩展XYZRGB 如果你需要彩色点云可以在RenderPointCloud里增加vtkUnsignedCharArray保存 RGB绑定到点数据。安装步骤Activiz 5.8.0 完整安装步骤VS WinForm .NET Framework⚠️硬性前提项目必须是 .NET Framework推荐 4.8不能是 .NET 6/7/8/9Activiz 5.8 不支持 .NET Core/.NET5。 平台x64不要 AnyCPU /x86。1. 新建项目VS → 创建新项目 →Windows 窗体应用 (.NET Framework)框架选择.NET Framework 4.8项目名称随便写创建。2. NuGet 安装 Activiz.NET.x64 5.8.0两种方式任选其一方式 ANuGet 管理器界面推荐右键项目 →管理 NuGet 程序包浏览搜索Activiz.NET.x64版本选5.8.0点【安装】接受协议。3. 把 RenderWindowControl 加到工具箱关键安装完 NuGet 后控件不会自动出现在工具箱需要手动添加在 VS 工具箱空白处 →右键 → 选择项切换到【.NET Framework 组件】标签点【浏览】定位到 nuget 包目录下的 dllpackages\Activiz.NET.x64.5.8.0\lib\net20\Kitware.VTK.dll选中这个 dll确定。 ✅ 工具箱会出现RenderWindowControl拖到 Form 窗体上。如果找不到 packages 文件夹右键项目 → 在文件资源管理器中打开文件夹往上一级就是 packages。4. 项目平台配置非常重要否则运行报错顶部菜单栏生成 → 配置管理器活动解决方案平台 → 新建 → 选择x64取消勾选【首选 32 位】项目属性 → 生成Activiz 5.8 只有 64 位库x86 直接崩溃。代码using Kitware.VTK;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace PointCloud{public partial class Form1 : Form{vtkRenderer ren;vtkRenderWindow renWin;vtkRenderWindowInteractor iren;public Form1(){InitializeComponent();// InitVtkRender(); 在构造函数中初始化会报错}//RenderWindowControl 控件** 在 InitializeComponent() 之后还没完成底层 VTK 窗口初始化**。//构造函数里直接调用InitVtkRender()此时控件底层的 RenderWindow 对象还没创建所以renderWindowControl1.RenderWindow是 null。//✅ 解决办法**不要在 Form 构造函数调用 InitVtkRender**改到窗体的Form_Load事件里面执行初始化。private void Form1_Load(object sender, EventArgs e){InitVtkRender();}void InitVtkRender(){ren vtkRenderer.New();ren.SetBackground(0.1, 0.1, 0.1);renWin renderWindowControl1.RenderWindow;renWin.AddRenderer(ren);// 修复从vtkRenderWindow获取交互器iren renWin.GetInteractor();iren.Initialize();}void RenderPointCloud(float[] xyzData){int pointCount xyzData.Length / 3;if (pointCount 0) return;vtkPoints points vtkPoints.New();points.SetNumberOfPoints(pointCount);for (int i 0; i pointCount; i){double x xyzData[i * 3 0];double y xyzData[i * 3 1];double z xyzData[i * 3 2];points.SetPoint(i, x, y, z);}vtkPolyData polydata vtkPolyData.New();polydata.SetPoints(points);vtkVertexGlyphFilter glyphFilter vtkVertexGlyphFilter.New();glyphFilter.SetInput(polydata);vtkPolyDataMapper mapper vtkPolyDataMapper.New();mapper.SetInputConnection(glyphFilter.GetOutputPort());vtkActor actor vtkActor.New();actor.SetMapper(mapper);actor.GetProperty().SetPointSize(2);actor.GetProperty().SetColor(1, 1, 1);ren.AddActor(actor);ren.ResetCamera();renWin.Render();}private void btnLoadCloud_Click(object sender, EventArgs e){Random rd new Random();int numPoints 1000;float[] cloudData new float[numPoints * 3];for (int i 0; i numPoints; i){cloudData[i * 3 0] (float)rd.NextDouble() * 10 - 5;cloudData[i * 3 1] (float)rd.NextDouble() * 10 - 5;cloudData[i * 3 2] (float)rd.NextDouble() * 10 - 5;}RenderPointCloud(cloudData);}protected override void OnFormClosed(FormClosedEventArgs e){iren?.Dispose();renWin?.Dispose();ren?.Dispose();base.OnFormClosed(e);}}}运行效果
返回列表