none
Saving Color and Depth data as png to filestream RRS feed

  • Question

  • I try to save both color and depth data to separate files as PNGs using PngBitmapEncoder but I get much less depth frames saved than color.

    If I save color as Jpeg with JpegBitmapEncoder and depth as PNG using PngBitmapEncoder I get the same amount of frames from both streams.

    Can anyone explain me why?

    private void myKinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
            {
                using (ColorImageFrame color = e.OpenColorImageFrame())
                {
                    if (color != null)
                    {
                        colorbits = new byte[color.PixelDataLength];
                        color.CopyPixelDataTo(colorbits);
                        PngBitmapEncoder enc = new PngBitmapEncoder();
                        enc.Frames.Add(BitmapFrame.Create(BitmapSource.Create(color.Width, color.Height, 96, 96, PixelFormats.Bgr32, null, colorbits, color.Width * color.BytesPerPixel)));
                            
                        if (StartSavingFrames)
                        {
                            string temppath = System.IO.Path.Combine(@"../output/kinect1/color/", colorcnt.ToString() + ".jpeg");
                            FileStream fs = new FileStream(temppath, FileMode.Create);
                            enc.Save(fs);
                            fs.Close();
                            fs = null;
                            colorcnt++;
                        }
                        else { colorcnt = 0; }
                    }
                }
            }

    private void myKinectSensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
            {
                using (DepthImageFrame depth = e.OpenDepthImageFrame())
                {
                    
                    if (depth != null)
                    {
                        frame = new short[depth.PixelDataLength];
                        depth.CopyPixelDataTo(frame);
                        for (int i = 0; i < depth.PixelDataLength; i++)
                        {
                            frame[i] = (short)(((ushort)frame[i]) >> 3);
                        }
                        PngBitmapEncoder enc = new PngBitmapEncoder();
                        enc.Frames.Add(BitmapFrame.Create(BitmapSource.Create(depth.Width, depth.Height, 96, 96, PixelFormats.Gray16, null, frame, depth.Width * depth.BytesPerPixel)));
               
                        if (StartSavingFrames)
                        {
                            string temppath = System.IO.Path.Combine(@"../output/kinect1/depth/", cnt.ToString() + ".png");
                            FileStream fs = new FileStream(temppath, FileMode.Create);
                            enc.Save(fs);
                            fs.Close();
                            fs = null;
                            cnt++;
                        }
                        else { cnt = 0; }
                    }
                }
            }

    Wednesday, April 22, 2015 9:52 PM

All replies

  • performance of the write operations and the operations of the system at the time you are calling them. You need to ensure your functions are returning and releasing(disposing) of the frames faster than the callback times. When you hold onto data to long, that just prevents your application from getting new data.

    Have a look at the coordinate mapping sample from the toolkit for a way to copy both the depth and color at the same time. From there you need to create a new thread to save that data since write speeds are very slow.


    Carmine Sirignano - MSFT

    Monday, April 27, 2015 7:44 PM