首先需要在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下载
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)