0

I'm extracting depth informations form a scene and I want to write X,Y,Z positions of points to a file.

using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    frameWidth = depthFrame.Width;
                    frameHeight = depthFrame.Height;

                    depthFrame.CopyDepthImagePixelDataTo(this.depthPixels); //Per essere visualizzati nella finestra
                    depthFrame.CopyPixelDataTo(this.shortDepth); //Per essere scritti nel file

                    mapper.MapDepthFrameToSkeletonFrame(DepthImageFormat.Resolution640x480Fps30, depthPixels, this.realPoints);

                    minDepth = depthFrame.MinDepth;
                    maxDepth = depthFrame.MaxDepth;

                    colorizer.ConvertDepthFrame(this.depthPixels, minDepth, maxDepth, this.depthTreatment, this.depthFrame32);
                    this.writableBitmap.WritePixels(
                            new Int32Rect(0, 0, frameWidth, frameHeight),
                            this.depthFrame32,
                            frameWidth * Bgr32BytesPerPixel,
                            0);
                    this.kinectDepthImage.Source = this.writableBitmap;

                    if (this.record)
                    {
                        using (StreamWriter writer = new StreamWriter(File.Open(mypath,FileMode.Append)))
                        {
                            writer.Write(realPoints);
                            writer.WriteLine();
                        }
                    }
                }
            }

Now I want to write a SkeletonPoint variable (realPoints) to file in this way:

  1. All realPoints X in the first line
  2. All realPoints Y in the second line
  3. All realPoints Z in the third line

So I'll have three lines with (640*480) elements per frame.

My questions:

  1. How can I write data this way?
  2. Is Stream Writer efficient enought to work at 30 fps?
Marco
  • 1,454
  • 1
  • 16
  • 30
  • What tutorials and documentation have you read regarding writing data to a file? This is pretty basic stuff. As for StreamWriter, set a large enough buffer size and try it out. – Bas Sep 18 '13 at 14:23
  • Thank for your answer. I found a way to access X,Y and Z, but StreamWriter cannot work at 30 fps writing all this amount of data. – Marco Sep 18 '13 at 14:37

2 Answers2

0

I found a way, with

realPoints.Select(p => p.X).ToArray()

I can access to the X,Y and Z coordinates independently. My problem now is that

using (StreamWriter writer = new StreamWriter(File.Open(mypath, FileMode.Append)))
{
    writer.Write(this.framenumber + " " + timestamp.ToString("F3", CultureInfo.InvariantCulture.NumberFormat));
    writer.WriteLine();
    writer.Write(String.Join(",", realPoints.Select(p => p.X).ToArray()));
    writer.WriteLine();
    writer.Write(String.Join(",", realPoints.Select(p => p.Y).ToArray()));
    writer.WriteLine();
    writer.Write(String.Join(",", realPoints.Select(p => p.Z).ToArray()));
    writer.WriteLine();
    writer.WriteLine();

    framenumber++;
}

Is extremely slow! It can work at 2fps or so. And during the writing it cannot update the image shown in the GUI...

Marco
  • 1,454
  • 1
  • 16
  • 30
0

Solved! To be able to save a stream this big (307,200 shorts per frame) maintaining an elevate framerate I had to use the FileStream class and the method Write(byte[] array, int offset, int count). This way it writes a big binary file with a sequence of bytes (every short is made of 2 bytes). Obviously on the reading side you'll have to take in account that you are reading bytes and not strings. Now I can write the file and visualize the depth image simultaneously (with just a little lag sometimes).

using (FileStream fs = File.Open(mypath, FileMode.Append))
{
    fs.Write(byteDepth, 0, byteDepth.Length);
}

I hope that this can help others with the same problem.

Marco
  • 1,454
  • 1
  • 16
  • 30