1

KinectとC#は初めてです。Kinectから深度画像を取得し、それをビットマップに変換してOpenCV操作を実行してから、表示しようとしています。問題は、深度画像の3分の1しか取得できず、残りは完全に黒であるということです(写真を参照)。これは生の深度画像ではなく、ペイント後に受け取る画像です。

ここに画像の説明を入力してください

これがコードです-

imageとimage1は、私が表示する2つの画像キャンバスです。

void DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
    {

        DepthImageFrame Image;
        Bitmap bm;
        using (Image = e.OpenDepthImageFrame())
        {


           if (Image != null)
            {
            this.shortpixeldata = new short[Image.PixelDataLength];
            this.depthFrame32 = new byte[Image.Width * Image.Height * Bgr32BytesPerPixel];


            bmp = new Bitmap(Image.Width, Image.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            Image.CopyPixelDataTo(this.shortpixeldata);

            byte[] convertedDepthBits = this.ConvertDepthFrame(this.shortpixeldata, ((KinectSensor)sender).DepthStream);


            BitmapData bmapdata = bmp.LockBits(
                                new System.Drawing.Rectangle(0, 0, Image.Width, Image.Height),
                                ImageLockMode.WriteOnly,
                                bmp.PixelFormat);


            IntPtr ptr = bmapdata.Scan0;
            Marshal.Copy(convertedDepthBits, 0, ptr, Image.PixelDataLength);
            bmp.UnlockBits(bmapdata);

            MemoryStream ms1 = new MemoryStream(); 
            bmp.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
            System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
            bImg.BeginInit();
            bImg.StreamSource = new MemoryStream(ms1.ToArray());
            bImg.EndInit();

            image.Source = bImg;

                if (bmp != null)
            {

                Image<Bgr, Byte> currentFrame = new Image<Bgr, Byte>(bmp); 

                Image<Gray, Byte> grayImage = currentFrame.Convert<Gray, Byte>().PyrDown().PyrUp();
                Image<Gray, Byte> Dest = new Image<Gray, Byte>(grayImage.Size);
                CvInvoke.cvCanny(grayImage, Dest, 10, 60, 3);


                image1.Source = ToBitmapSource(Dest);

                CalculateFps();
            }



            }



            else
            {
                System.Diagnostics.Debug.WriteLine("depth bitmap empty :/");
            }
        }
    }


        private byte[] ConvertDepthFrame(short[] depthFrame, DepthImageStream depthStream)
        {
            System.Diagnostics.Debug.WriteLine("depthframe len :{0}", depthFrame.Length);

        for (int i16 = 0, i32 = 0; i16 < depthFrame.Length && i32 < this.depthFrame32.Length; i16++, i32 += 4)
        {

        int realDepth = depthFrame[i16] >> DepthImageFrame.PlayerIndexBitmaskWidth;


        byte Distance = 0;


        int MinimumDistance = 800;
        int MaximumDistance = 4096;


        if (realDepth > MinimumDistance)
        {


        //White = Close
        //Black = Far
        Distance = (byte)(255-((realDepth-MinimumDistance)*255/(MaximumDistance-MinimumDistance)));


        this.depthFrame32[i32 + RedIndex] = (byte)(Distance);
        this.depthFrame32[i32 + GreenIndex] = (byte)(Distance);
        this.depthFrame32[i32 + BlueIndex] = (byte)(Distance);
        }

        else
        {
        this.depthFrame32[i32 + RedIndex] = 0;
        this.depthFrame32[i32 + GreenIndex] = 150;
        this.depthFrame32[i32 + BlueIndex] = 0;
        }
        }

        return this.depthFrame32;
        }

私は無駄に別のPixelFormatsを試しました。私は問題を理解することができません。誰かが私が間違っていることを知っていますか?ありがとう

4

2 に答える 2

0

WritableBitmap深度画像を表示可能な形式にコピーするには、を使用することをお勧めします。の場合PixelFormat、その情報は深度画像自体で利用できるため、WritableBitmapキャプチャされているのと同じ形式を使用する必要があります。

Microsoftが提供する例を見たことがありますか?それらはKinectforWindowsサンプルのCodePlexページにあります。深度データをにコピーして出力する方法を示すサンプルがいくつかありますWritableBitmap。たとえばDepthFrameReady、「DepthBasics-WPF」サンプルアプリケーションのコールバック関数は次のとおりです。

/// <summary>
/// Event handler for Kinect sensor's DepthFrameReady event
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
{
    using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
    {
        if (depthFrame != null)
        {
            // Copy the pixel data from the image to a temporary array
            depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);

            // Get the min and max reliable depth for the current frame
            int minDepth = depthFrame.MinDepth;
            int maxDepth = depthFrame.MaxDepth;

            // Convert the depth to RGB
            int colorPixelIndex = 0;
            for (int i = 0; i < this.depthPixels.Length; ++i)
            {
                // Get the depth for this pixel
                short depth = depthPixels[i].Depth;

                // To convert to a byte, we're discarding the most-significant
                // rather than least-significant bits.
                // We're preserving detail, although the intensity will "wrap."
                // Values outside the reliable depth range are mapped to 0 (black).

                // Note: Using conditionals in this loop could degrade performance.
                // Consider using a lookup table instead when writing production code.
                // See the KinectDepthViewer class used by the KinectExplorer sample
                // for a lookup table example.
                byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);

                // Write out blue byte
                this.colorPixels[colorPixelIndex++] = intensity;

                // Write out green byte
                this.colorPixels[colorPixelIndex++] = intensity;

                // Write out red byte                        
                this.colorPixels[colorPixelIndex++] = intensity;

                // We're outputting BGR, the last byte in the 32 bits is unused so skip it
                // If we were outputting BGRA, we would write alpha here.
                ++colorPixelIndex;
            }

            // Write the pixel data into our bitmap
            this.colorBitmap.WritePixels(
                new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                this.colorPixels,
                this.colorBitmap.PixelWidth * sizeof(int),
                0);
        }
    }
}

この特定のクラスの完全なコードは、次の場所にあります: http: //kinectforwindows.codeplex.com/SourceControl/changeset/view/861462899ae7#v1.x/ToolkitSamples1.6.0/C#/DepthBasics-WPF/MainWindow.xaml.cs

「KinectExplorer」の例は、3つのストリームすべてを一度に調べるため、確認するのに適したもう1つの例です。CodePlexリポジトリに含まれていないライブラリが必要ですが、Kinect forWindowsToolkitにあります。

于 2013-03-15T16:17:04.697 に答える
0

さて、私は自分でそれを理解しました。それはずっと明白な視界に隠れていました。

関数ConvertDepthFrameは、バイト配列を異なるサイズのconvertedDepthBitsに返します(4つの別々のチャネルなので元のサイズの4倍)。メソッド呼び出しで4 *Image.PixelDataLengthとしてコピーされるデータの長さを使用する必要があります:Marshal.Copy (...)

今はうまくいっています。ふぅ!:)

于 2013-03-15T20:12:46.523 に答える