簡體   English   中英

如何在Windows.Forms.PictureBox中正確顯示Kinect視頻流?

[英]How can I show Kinect video stream in Windows.Forms.PictureBox appropriately?

我正在嘗試將Kinect的視頻流顯示到PictureBox中。 原因是,我想用一些圖像覆蓋它,並使用FillEllipse()方法添加實時標記。 但是,我最后得到一個紅色x(十字)的盒子。 有人能告訴我,我哪里出錯了? 我應該使用WritableBitmap嗎? 我想到了這一點,但是可寫位圖不提供諸如FillEllipse()之類的方法來放置標記。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.Kinect;
using System.Drawing.Imaging;
using System.Drawing;
using System.Runtime.InteropServices;

namespace fTrack_WF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        KinectSensor myKinect;

        private void Window_Loaded(object sender, EventArgs e)
        {
            if (KinectSensor.KinectSensors.Count == 0)
            {
                MessageBox.Show("No Kinects device detected", "Camera View");
                Application.Exit();
                return;
            }

            try
            {
                // get first Kinect device attached on computer
                myKinect = KinectSensor.KinectSensors[0];

                // enable depth stream
                myKinect.DepthStream.Enable();

                // enable color video stream
                myKinect.ColorStream.Enable();

                // start the sensor
                myKinect.Start();


                // connect up the video event handler
                myKinect.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(myKinect_ColorFrameReady);

            }
            catch
            {
                MessageBox.Show("Kinect initialise failed", "Camera viewer");
                Application.Exit();
            }


        }


        #region Video Image Processing

        byte[] colorData = null;
        Bitmap kinectVideoBitmap = null;
        IntPtr colorPtr;

        void myKinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame == null) return;

                if (colorData == null)
                    colorData = new byte[colorFrame.PixelDataLength];

                colorFrame.CopyPixelDataTo(colorData);

                Marshal.FreeHGlobal(colorPtr);
                colorPtr = Marshal.AllocHGlobal(colorData.Length);
                Marshal.Copy(colorData, 0, colorPtr, colorData.Length);

                kinectVideoBitmap = new Bitmap(
                    colorFrame.Width,
                    colorFrame.Height,
                    colorFrame.Width * colorFrame.BytesPerPixel;
                    PixelFormat.Format32bppRgb,
                    colorPtr);

                kinectVideoBox.Image = kinectVideoBitmap;

                kinectVideoBitmap.Dispose();

            }

        }

        #endregion
    }
}

非常感謝你!

此致,ikel

我找到了答案。 處置是必要的指示,以騰出資源在這里 問題是,我在繪制之后處理得太早,所以看起來好像沒有畫出來。 但是,無論如何,我在這里給出了更明確的回答。

Bitmap繼承自Image ,它實現了IDisposable ,所以當你完成使用實例時,你應該在它上面調用Dispose() 這將清除Image中的非托管資源。

但是, Image 還實現finalizer ,因此如果由於某種原因您無法調用Dispose() ,則在實例的最終確定期間將回收資源,這將在實例不再被引用后的某個時刻發生。

我只是刪除了kinectVideoBitmap.Dispose(); 我的Windows窗體在PictureBox控件中顯示Kinect的視頻流。

此致,ikel

我不清楚為什么你使用WinForms PictureBox而不是使用WPF。

您是否嘗試將Canvas放在視頻流的頂部,在SDK示例中進行了演示,並簡單地將其繪制到那個?

    <Grid HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="320" Height="240">
        <lt:KinectDepthViewer x:Name="DepthViewer" KinectSensorManager="{Binding KinectSensorManager}" />
        <Canvas>
            <lt:KinectSkeletonViewer
                                KinectSensorManager="{Binding KinectSensorManager}"
                                Width="{Binding ElementName=DepthViewer, Path=ActualWidth}"
                                Height="{Binding ElementName=DepthViewer, Path=ActualHeight}"
                                ShowBones="True" ShowJoints="True" ShowCenter="True" ImageType="Depth" />
        </Canvas>
    </Grid>


    <Canvas Name="DrawingCanvas">
    </Canvas>

第二個畫布處於較高的z-index,其上的任何對象都將覆蓋您的視頻流。

PS盡管我的代碼指向深度查看器,但在使用SDK中的示例時,視頻流的執行方式相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM