C# 读取pcd、ply点云文件数据

最近研究了下用pcl读取点云数据,又做了个C#的dll,方便读取,同样这个dll基于pcl 最新版本1.13.1版本开发。

上次做的需要先得到点云长度,再获取数据。这次这个定义了一个PointCloudXYZ类来存数据。将下面的dll拷贝到可执行目录下,引用Q_PclSharp.dll使用

Dll下载链接

链接:https://pan.baidu.com/s/1H7ig8YXJDTscprVDydjuJA

提取码:6xha

具体使用方法

  1. 引用Q_PclSharp.dll 并使用命名空间 using Q_PclSharp;

  2. PointCloudXYZ cloudXYZ = new PointCloudXYZ();

  3. IO.loadPlyFile(filePath, cloud.PointCloudXYZPointer);

装个VTK,把读到的数据显示出来

测试效果

测试代码

cs 复制代码
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;
using Q_PclSharp;

namespace TestCSdll
{
    public partial class Form1 : Form
    {
        PointCloudXYZ cloud = new PointCloudXYZ();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {                               
                cloud.Clear();
                IO.loadPlyFile(ofd.FileName, cloud.PointCloudXYZPointer);

                vtkPoints points = vtkPoints.New();
                for (int i = 0; i < cloud.Size; i++)
                {
                    points.InsertNextPoint(cloud.GetX(i), cloud.GetY(i), cloud.GetZ(i));

                }
                vtkUnsignedCharArray colors_rgb = GetRGB(cloud);
                vtkPolyData polydata = vtkPolyData.New();
                polydata.SetPoints(points);
                polydata.GetPointData().SetScalars(colors_rgb);

                vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New();
                glyphFilter.SetInputConnection(polydata.GetProducerPort());

                vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
                mapper.SetInputConnection(glyphFilter.GetOutputPort());

                vtkActor actor = vtkActor.New();
                actor.SetMapper(mapper);

                vtkRenderer render = renderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer();

                for (int i = 0; i < render.GetActors().GetNumberOfItems(); i++)
                {
                    var item = render.GetActors().GetItemAsObject(i);
                    render.RemoveActor((vtkActor)item);
                    item.Dispose();
                }

                render.AddActor(actor);
                render.ResetCamera();
                this.Refresh();
            }
        }

        vtkUnsignedCharArray GetRGB(PointCloudXYZ cloud)
        {
            vtkUnsignedCharArray colors_rgb = vtkUnsignedCharArray.New();
            double[] minmax = new double[6];
            cloud.GetMinMaxXYZ(minmax);
            double z = minmax[5] - minmax[4];
            double z_median = z / 2;
            colors_rgb.SetNumberOfComponents(3);
            double r = 0, g = 0, b = 0;
            for (int i = 0; i < cloud.Size; i++)
            {               
                if ((cloud.GetZ(i) - minmax[4]) > z_median)
                {                   
                    r = (255 * ((cloud.GetZ(i) - minmax[4] - z_median) / z_median)); ;
                    g = (255 * (1 - ((cloud.GetZ(i) - minmax[4] - z_median) / z_median)));
                    b = 0;
                    colors_rgb.InsertNextTuple3(r, g, b);
                }
                else
                {
                    r = 0;
                    g = (255 * ((cloud.GetZ(i) - minmax[4]) / z_median));
                    b = (255 * (1 - ((cloud.GetZ(i) - minmax[4]) / z_median)));
                    colors_rgb.InsertNextTuple3(r, g, b);
                }
            }
            return colors_rgb;
        }
    }
}
相关推荐
o0向阳而生0o2 小时前
28、.NET 中元数据是什么?
microsoft·c#·.net
niuTaylor3 小时前
Linux驱动开发快速上手指南:从理论到实战
linux·运维·开发语言·驱动开发·c#
军训猫猫头3 小时前
89.WPF 中实现便捷的数字输入框:DecimalUpDown 控件的使用 WPF例子 C#例子.
开发语言·c#·wpf
冰茶_5 小时前
.NET MAUI 发展历程:从 Xamarin 到现代跨平台应用开发框架
学习·microsoft·微软·c#·.net·xamarin
The Future is mine6 小时前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
Iotfsd13 小时前
.NET写的开源工业物联网网关(IoTGateway)
物联网·c#·.net·dotnet·边缘网关·雾计算·工业物联网智能网关
先生沉默先13 小时前
c#接口_抽象类_多态学习
开发语言·学习·c#
江沉晚呤时14 小时前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
iReachers14 小时前
使用命令行加密混淆C#程序
开发语言·c#
[太阳]8814 小时前
Kafka命令行的使用/Spark-Streaming核心编程(二)
c#·linq