用Helixtoolkit c#显示Point3DCollection

问题描述

我已经使用librealsense中的代码制作了点云:

var points = pc.Process(depthFrame).As<Points>();
//float depth = depthFrame.Getdistance(x,y);
//bBox = (287,23,86,320);
// We colorize the depth frame for visualization purposes
var colorizedDepth = colorizer.Process<VideoFrame>(depthFrame).disposeWith(frames);
//var org = Cv2.ImRead(colorFrame);
// 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);

而且我已经将点云从Media3D转换为Point3DCollection:

Point3DCollection pointss = new Point3DCollection();
foreach (var vertex in vertices)
{
    var point3D = new Point3D(vertex.x,vertex.y,vertex.z);
    pointss.Add(point3D);
}

我想在XAML文件中使用以下行显示这些点:

<h:HelixViewport3D Grid.ColumnSpan="1" Grid.Column="1" Margin="2.4,1,-0.4" >
<h:DefaultLights/>
<h:PointsVisual3D Points="{Binding pointss}" Color="Red" Size ="2"/>
</h:HelixViewport3D>

但是我看不到我的观点。我的代码有问题吗? 我现在正在使用的代码如下所示。我已经添加了答案中给出的内容,但是我没有在对象的示例上设置错误对象引用。我正在使用的代码如下:

namespace Intel.RealSense
{
    /// <summary>
    /// Interaction logic for Window.xaml
    /// </summary>
    public partial class CaptureWindow : System.Windows.Window
    {
        private Pipeline pipeline;
        private Colorizer colorizer;
        private CancellationTokenSource tokenSource = new CancellationTokenSource();
        private Pipeline pipe = new Pipeline();
        private PointCloud pc = new PointCloud();
        private ThresholdFilter threshold;
        private Point3DCollection _pointss;
        public Point3DCollection pointss
        {
            get => _pointss;
            set
            {
                if (_pointss == value)
                    return;

                _pointss = value;
                OnPropertyChanged();
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName = null)
        {
            PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
        }
        //static CvTrackbar Track;
        //static OpenCvSharp.Point[][] contours;
        //static HierarchyIndex[] hierarchy;
        static Action<VideoFrame> UpdateImage(Image img)
        {
            var wbmp = img.source as WriteableBitmap;
            return new Action<VideoFrame>(frame =>
            {
                var rect = new Int32Rect(0,frame.Width,frame.Height);
                wbmp.WritePixels(rect,frame.Data,frame.Stride * frame.Height,frame.Stride);
            });
        }
        public CaptureWindow()
        {
            InitializeComponent();
            ModelImporter import = new ModelImporter();
            
            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);
                PipelineProfile selection = pp;
                var depth_stream = selection.GetStream<VideoStreamProfile>(Stream.Depth);
                Intrinsics i = depth_stream.GetIntrinsics();
                float[] fov = i.FOV;
                SetupWindow(pp,out updateDepth,out updateColor);

                Task.Factory.StartNew(() =>
                {
                    while (!tokenSource.Token.IsCancellationRequested)
                    {
                        threshold = new ThresholdFilter();
                        threshold.Options[Option.Mindistance].Value = 0.0F;
                        threshold.Options[Option.Maxdistance].Value = 0.1F;
                        using (var releaser = new FramesReleaser())
                        {
                            using (var frames = pipeline.WaitForFrames().disposeWith(releaser))
                            {
                                var pframes = frames
                                .ApplyFilter(threshold).disposeWith(releaser);
                            }
                        }
                        // 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>();
                            //float depth = depthFrame.Getdistance(x,y);
                            //bBox = (287,320);

                            // We colorize the depth frame for visualization purposes
                            var colorizedDepth = colorizer.Process<VideoFrame>(depthFrame).disposeWith(frames);
                            //var org = Cv2.ImRead(colorFrame);
                            // 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);
                            //Point3DCollection pointss = new Point3DCollection();
foreach (var vertex in vertices)
                            {
var point3D = new Point3D(vertex.x,vertex.z);
pointss.Add(point3D);
}

                            // 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() + ")";
                            }));
                            //HelixToolkit.Wpf.
                        }
                    }
                },tokenSource.Token);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
                System.Windows.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,PixelFormats.Rgb24,null);
depth = UpdateImage(imgDepth);

using (var p = pipelineProfile.GetStream(Stream.Color).As<VideoStreamProfile>())
                imgColor.source = new WriteableBitmap(p.Width,null);
color = UpdateImage(imgColor);
        }
}

解决方法

您只能绑定到公共属性,而不能绑定到字段,因此必须这样定义:

public Point3DCollection pointss { get; } = new Point3DCollection();

如果要在运行时重新分配集合,还应该实现INotifyPropertyChanged,否则分配新集合将不会触发绑定更新,并且更改也不会反映在UI中。

public class YourViewModel : INotifyPropertyChanged
{
   private Point3DCollection  _pointss;
   public Point3DCollection pointss
   {
      get => _pointss;
      set
      {
         if (_points == value)
            return;

         _points = value;
         OnPropertyChanged();
      }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   protected virtual void OnPropertyChanged(string propertyName = null)
   {
      PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
   }
}