Problem Point cloud for C# using Intel Realsense

0

I need to make a point cloud using the intel realsense D455 camera. I use the OpenTK from the site https://www.codeproject.com/Articles/839389/Fast-Point-Cloud-Viewer-with-Csharp-and-OpenGL. But if I use this code:

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.Diagnostics;
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;
using OpenTKLib;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace Intel.RealSense
{
    /// <summary>
    /// Interaction logic for Window.xaml
    /// </summary>
    public partial class CaptureWindow : Window
    {
        private Pipeline  pipeline;
        private Colorizer colorizer;
        private CancellationTokenSource tokenSource = new CancellationTokenSource();
        private Pipeline pipe = new Pipeline();
        private PointCloud pc = new PointCloud();
        private OpenTK.GLControl glControl;
        PointCloudRenderable pcr = new PointCloudRenderable();

        static Action<VideoFrame> UpdateImage(Image img)
        {
            var wbmp = img.Source as WriteableBitmap;
            return new Action<VideoFrame>(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();

                // 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);
                OGLControl oGLControl = new OpenTKLib.OGLControl();
                glControl = oGLControl;
                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 frames = pipeline.WaitForFrames())
                        {
                            var colorFrame = frames.ColorFrame.DisposeWith(frames);
                            var depthFrame = frames.DepthFrame.DisposeWith(frames);
                            var points = pc.Process(depthFrame).As<Points>();

                            // We colorize the depth frame for visualization purposes
                            var colorizedDepth = colorizer.Process<VideoFrame>(depthFrame).DisposeWith(frames);

                            // CopyVertices is extensible, any of these will do:
                            var vertices = new float[points.Count * 3];
                            // var vertices = new Intel.RealSense.Math.Vertex[points.Count];
                            // var vertices = new UnityEngine.Vector3[points.Count];
                            // var vertices = new System.Numerics.Vector3[points.Count]; // SIMD
                            // var vertices = new GlmSharp.vec3[points.Count];
                            //  var vertices = new byte[points.Count * 3 * sizeof(float)];
                            points.CopyVertices(vertices);

                            // Render the frames.
                            Dispatcher.Invoke(DispatcherPriority.Render, updateDepth, colorizedDepth);
                            Dispatcher.Invoke(DispatcherPriority.Render, updateColor, colorFrame);

                            Dispatcher.Invoke(new Action(() =>
                            {
                                String depth_dev_sn = depthFrame.Sensor.Info[CameraInfo.SerialNumber];
                                txtTimeStamp.Text = depth_dev_sn + " : " + String.Format("{0,-20:0.00}", depthFrame.Timestamp) + "(" + depthFrame.TimestampDomain.ToString() + ")";
                            }));
                            //Console.WriteLine("The camera is pointing at an object " + depthFrame.GetDistance(depthFrame.Width / 2, depthFrame.Height / 2) + " meters away\t");
                            //pc.MapTo
                        }
                    }
                }, 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);
        }

       
    }
}

I get the problem saying:

System.NullReferenceException
  HResult=0x80004003
  Message=De objectverwijzing is niet op een exemplaar van een object ingesteld.
  Source=OpenTKLib
  StackTrace:
   at OpenTKLib.OGLControl.OnResize(EventArgs e) in D:\vision_partners\Point Cloud\0.9.0.9 OpenGL Sources\OpenTKLib\_Controls\OGLControl.cs:line 132

This is the line where the problem lies:

this.GLrender.Camera.PerspectiveUpdate(this.Width, this.Height);

Does anyone know the solution to this problem?

c#
opengl
point-clouds
opentk

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0