pcd文件有ascii 和二进制格式,ascii可以直接记事本打开,C#可以一行行读。但二进制格式的打开是乱码,如果尝试程序中读取,对比下看了数据也对不上。
这里可以使用pcl里的函数来读取pcd,无论二进制或ascii都可以正确读取,但pcl是C++写的,不太方便使用。我写了个C#的dll文件Q_PclCs.dll,此dll基于pcl 1.13.1版本开发,可以通过引用此dll文件来读取pcd文件,而不需要额外配置其他东西。
文件下载地址
链接:https://pan.baidu.com/s/1dgXmd9VTf3G4ux__-fhtGg
提取码:vx8u
使用说明 将这些dll文件都放到你程序的可执行文件目录下,引用Q_PclCs.dll
读取方式
命名空间 Q_PclCs
using Q_PclCs;
PcdReader pcdReader = new PcdReader();
先读点云长度length
int length=pcdReader.GetLength(ofd.FileName);
准备数组存储
double[] x = new double[length];
double[] y= new double[length];
double[] z=new double[length];
pcdReader.PcdReaderFast(ref x, ref y, ref z);
这样就读出来了
测试程序,可以用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 Kitware.VTK;
using Q_PclCs;
namespace testCsGetPcd
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog() == DialogResult.OK)
{
PcdReader pcdReader = new PcdReader();
int length=pcdReader.GetLength(ofd.FileName);
double[] x = new double[length];
double[] y= new double[length];
double[] z=new double[length];
pcdReader.PcdReaderFast(ref x, ref y, ref z);
vtkPoints points = new vtkPoints();
for (int i = 1; i < length; i++)
{
points.InsertPoint(i, x[i], y[i], z[i]);
}
vtkPolyData polydata = vtkPolyData.New();
polydata.SetPoints(points);
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();
render.AddActor(actor);
//render.AddActor(scalarBar);
//render.SetViewport(0.0, 0.0, 1, 1);
render.ResetCamera();
this.Refresh();
}
}
}
}