使用 Helixtoolkit c# 显示 Point3DCollection

Posted

技术标签:

【中文标题】使用 Helixtoolkit c# 显示 Point3DCollection【英文标题】:displaying Point3DCollection with Helixtoolkit c# 【发布时间】:2020-10-15 08:03:47 【问题描述】:

我使用 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,-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, 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, 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);
                            //Point3DCollection pointss = new Point3DCollection();
foreach (var vertex in vertices)
                            
var point3D = new Point3D(vertex.x, vertex.y, 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, 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);
        

【问题讨论】:

如何在您的视图模型中定义pointss 我只使用了 3 次 pointss,在我发送的示例中都可以看到这三个。我需要在其他地方使用pointss 吗? 【参考方案1】:

你只能绑定到公共属性,不能绑定到字段,所以你必须像这样定义它:

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));
   

【讨论】:

我是否需要为PropertyChangedEventHandler 使用另一个库,因为当我实现该代码时,我得到一个指令丢失的错误? @XanderVreeswijk 我猜你需要添加using System.ComponentModel;,这是INotifyPropertyChanged所在的命名空间。 我的代码基于 librealsense 中可用的代码:github.com/IntelRealSense/librealsense/tree/master/wrappers/… 我已将点放在公共 CaptureWindow() 中,而您给我的代码在此之前。当我启动代码时,我得到错误对象引用未在对象示例上设置。我将更改主要问题,以便您了解我的意思。 @XanderVreeswijk 该集合是不可观察的,因此当您从中添加或删除项目时用户界面不会注意到。尝试将点添加到局部变量,并在填充循环后将其分配给pointss 属性。

以上是关于使用 Helixtoolkit c# 显示 Point3DCollection的主要内容,如果未能解决你的问题,请参考以下文章

2021-09-20 WPF上位机 30-模型加载(HelixToolKIt使用)

调整 HelixToolKit 模型的外观

HelixToolkit.WPF Additional DependencyProperty 不工作

如何在 WPF 中创建可扩展的 Moebius-strip?

在 Windows 窗体 C# 中创建 POI 映射。如何将图标(图像)放在地图上(图片框中的现有图像)?

C#通过NPOI操作Excel