Created
December 31, 2018 12:52
-
-
Save ogoshen/58eb3b74b229d8a47d8c6f2bebc099be to your computer and use it in GitHub Desktop.
cs-tutorial-2-capture with depth aligned to color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Navigation; | |
using System.Windows.Shapes; | |
using System.Windows.Threading; | |
namespace Intel.RealSense | |
{ | |
/// <summary> | |
/// Interaction logic for Window.xaml | |
/// </summary> | |
public partial class CaptureWindow : Window | |
{ | |
private Pipeline pipeline; | |
private Colorizer colorizer; | |
private Align aligner; | |
private CancellationTokenSource tokenSource = new CancellationTokenSource(); | |
static Action<VideoFrame> UpdateImage(Image img) | |
{ | |
var wbmp = img.Source as WriteableBitmap; | |
return new Action<VideoFrame>(frame => | |
{ | |
using (frame) | |
{ | |
var rect = new Int32Rect(0, 0, frame.Width, frame.Height); | |
wbmp.WritePixels(rect, frame.Data, frame.Stride * frame.Height, frame.Stride); | |
} | |
}); | |
} | |
public CaptureWindow() | |
{ | |
InitializeComponent(); | |
try | |
{ | |
Action<VideoFrame> updateDepth; | |
Action<VideoFrame> updateColor; | |
// The colorizer processing block will be used to visualize the depth frames. | |
colorizer = new Colorizer(); | |
aligner = new Align(Stream.Color); | |
// Create and config the pipeline to strem color and depth frames. | |
pipeline = new Pipeline(); | |
var cfg = new Config(); | |
cfg.EnableStream(Stream.Depth, 640, 480); | |
cfg.EnableStream(Stream.Color, Format.Rgb8); | |
var pp = pipeline.Start(cfg); | |
SetupWindow(pp, out updateDepth, out updateColor); | |
Task.Factory.StartNew(() => | |
{ | |
while (!tokenSource.Token.IsCancellationRequested) | |
{ | |
// We wait for the next available FrameSet and using it as a releaser object that would track | |
// all newly allocated .NET frames, and ensure deterministic finalization | |
// at the end of scope. | |
using (var original = pipeline.WaitForFrames()) | |
using (var frames = aligner.Process(original)) | |
{ | |
var colorFrame = frames.ColorFrame.DisposeWith(frames); | |
var depthFrame = frames.DepthFrame.DisposeWith(frames); | |
// We colorize the depth frame for visualization purposes, . | |
var colorizedDepth = colorizer.Process(depthFrame).DisposeWith(frames); | |
// Render the frames. | |
Dispatcher.Invoke(DispatcherPriority.Render, updateDepth, colorizedDepth); | |
Dispatcher.Invoke(DispatcherPriority.Render, updateColor, colorFrame); | |
} | |
} | |
}, tokenSource.Token); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
Application.Current.Shutdown(); | |
} | |
} | |
private void control_Closing(object sender, System.ComponentModel.CancelEventArgs e) | |
{ | |
tokenSource.Cancel(); | |
} | |
private void SetupWindow(PipelineProfile pipelineProfile, out Action<VideoFrame> depth, out Action<VideoFrame> color) | |
{ | |
using (var p = pipelineProfile.GetStream(Stream.Depth) as VideoStreamProfile) | |
imgDepth.Source = new WriteableBitmap(p.Width, p.Height, 96d, 96d, PixelFormats.Rgb24, null); | |
depth = UpdateImage(imgDepth); | |
using (var p = pipelineProfile.GetStream(Stream.Color) as VideoStreamProfile) | |
imgColor.Source = new WriteableBitmap(p.Width, p.Height, 96d, 96d, PixelFormats.Rgb24, null); | |
color = UpdateImage(imgColor); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment