C#下使用RealSense D435i获取图像,深度,导出.ply点云

首先需要在NuGet管理中安装RealSense库相关包。

主要安装下面两个包:

 代码中引入:

using Intel.RealSense;
//配置相机
var cfg = new Config();

using (var ctx = new Context())
{

    var devices = ctx.QueryDevices();
    var dev = devices[0];

    Console.WriteLine("\nUsing device 0, an {0}", dev.Info[CameraInfo.Name]);
    Console.WriteLine("    Serial number: {0}", dev.Info[CameraInfo.SerialNumber]);
    Console.WriteLine("    Firmware version: {0}", dev.Info[CameraInfo.FirmwareVersion]);

    var sensors = dev.QuerySensors();
    var depthSensor = sensors[0];
    var colorSensor = sensors[1];
    
    var depthProfile = depthSensor.StreamProfiles.Where(p => p.Stream == Intel.RealSense.Stream.Depth).OrderBy(p => p.Framerate).Select(p => p.As<VideoStreamProfile>()).First();

    var colorProfile = colorSensor.StreamProfiles.Where(p => p.Stream == Intel.RealSense.Stream.Color).OrderBy(p => p.Framerate).Select(p => p.As<VideoStreamProfile>()).First();

    //配置相机宽高,深度,帧数
    cfg.EnableStream(Intel.RealSense.Stream.Depth, 640, 480, Format.Z16, 30);
    cfg.EnableStream(Intel.RealSense.Stream.Color, 640, 480, Format.Bgr8, 30);
}

var pipe = new Pipeline();

//启动相机并应用配置
pipe.Start(cfg);

Colorizer color_map = new Colorizer();

while (true)
{
   using (var frames = pipe.WaitForFrames())
   {
       //对齐深度图像
       Align align = new Align(Intel.RealSense.Stream.Color).DisposeWith(frames);
       Frame aligned = align.Process(frames).DisposeWith(frames);
       FrameSet alignedframeset = aligned.As<FrameSet>().DisposeWith(frames);
       var colorFrame = alignedframeset.ColorFrame.DisposeWith(alignedframeset);
       var depthFrame = alignedframeset.DepthFrame.DisposeWith(alignedframeset);

       var colorizedDepth = color_map.Process<VideoFrame>(depthFrame).DisposeWith(alignedframeset);

       //获取视频帧并转bitmap
       System.Drawing.Bitmap DepthImg = new System.Drawing.Bitmap(colorFrame.Width, colorFrame.Height, colorFrame.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, colorizedDepth.Data);

       System.Drawing.Bitmap ColorImg = new System.Drawing.Bitmap(colorFrame.Width, colorFrame.Height, colorFrame.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, frames.ColorFrame.Data);

       //将图像显示到picturebox控件中,需提前放置好控件
       PictureBox.Image = ColorImg;
       PictureBox2.Image = DepthImg;
   }
}

如果需要导出.ply点云数据,插入以下代码即可:

//点云数据
PointCloud pointcloud = new PointCloud();
//设置Texture
pointcloud.MapTexture(colorFrame);
//生成点云
Points points = pointcloud.Process(depthFrame).As<Points>();
//导出.ply
points.ExportToPLY("standard.ply", colorFrame);

具体demo下载:C#RealSense获取图像流Demo-C#文档类资源-CSDN下载

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值