locked
Writing depth image in .bin using C# RRS feed

  • Question

  • I want to save depth images captured using Kinect v2. I can save the images in (.png, .tif, .gif etc) but it took much time in saving data and it miss some frames while saving. So I want to save the images in .bin format. Here is my code for the depth image:

    <g class="gr_ gr_19 gr-alert gr_gramm Punctuation replaceWithoutSep" data-gr-id="19" id="19">Please anyone</g> tell me how can write the depth image <g class="gr_ gr_36 gr-alert gr_gramm Punctuation multiReplace" data-gr-id="36" id="36">in .</g>bin file.

    IplImage depth = new IplImage(512, 424, BitDepth.U16, 1);
    CvVideoWriter DepthWriter;
    Width = sensor.DepthFrameSource.FrameDescription.Width;
    DHeight = sensor.DepthFrameSource.FrameDescription.Height;
    WbDepth = new WriteableBitmap(DWidth, DHeight, 96, 96, PixelFormats.Gray16, null);
    int depthshft = (int)SliderDepth.Value;
    using (DepthFrame depthframe = frame.DepthFrameReference.AcquireFrame())
    ushort* depthdata = (ushort*)depth.ImageData;
    if (depthframe != null)
       {
        Depthdata = new ushort[DWidth * DHeight];
        ushort[] Depthloc = new ushort[DWidth * DHeight];
        depthframe.CopyFrameDataToArray(Depthdata);
        for (int i = 0; i < DWidth * DHeight; i++)
            {
              Depthloc[i] = 0x1000;
            }
            colorspacePoint = new ColorSpacePoint[DWidth * DHeight];
            depthspacePoint = new DepthSpacePoint[CWidth * CHeight];
            sensor.CoordinateMapper.MapDepthFrameToColorSpace(Depthloc, colorspacePoint);
            for (int y = 0; y < DHeight; y++)
                {
                for (int x = 0; x < DWidth; x++)
                    {
                    if (depthshft != 0)
                       {
                        Depthdata[y * DWidth + x] = (ushort)((Depthdata[y * DWidth + x]) << depthshft);
                       }
    
                     }
                 }
             depth.CopyPixelData(Depthdata);
     }
    WbDepth.WritePixels(new Int32Rect(0, 0, DWidth, DHeight), Depthdata, strideDep, 0);
    ImageDepth.Source = WbDepth;
    if (depth != null )
                {
                    string s = string.Format("{0}", count_depth);
                    Cv.SaveImage("G:\\Kinect V2 Recording\\depth\\depth" + s + ".png", depth);
    
    
                    count_depth++;
                }
    Cv.ReleaseVideoWriter(DepthWriter);
        CheckBox_saveD.IsEnabled = false;
    if (CheckBox_saveD.IsChecked == true) Cv.ReleaseVideoWriter(DepthWriter);

    • Moved by Kristin Xie Friday, June 12, 2015 7:43 AM more related to Kinect V2
    Thursday, June 11, 2015 8:25 AM

All replies

  • No that question is related to video codec.. Here i am asking about the saving the image in .Bin format
    Thursday, June 11, 2015 12:52 PM
  • No that question is related to video codec.. Here i am asking about the saving the image in .Bin format
    I think that forum is still more appropriate for this question also.

    Fouad Roumieh

    Thursday, June 11, 2015 12:55 PM
  • I think this problem is related to the C# for saving the image in .bin format. If you have any solution for this please tell me
    Thursday, June 11, 2015 1:07 PM
  • Not from my side, I have no experience with kinect or even .bin format for image, I was trying to help to redirect it to the right forum.

    Let's wait other contributors maybe there is solution.


    Fouad Roumieh

    Thursday, June 11, 2015 1:15 PM
  • Can i post the same question there?
    Thursday, June 11, 2015 1:22 PM
  • Just write a note here asking the moderator to move it, that's better than duplicating and confusing contributors.

    Fouad Roumieh

    Thursday, June 11, 2015 1:24 PM
  • Can i post the same question there?

    Hi Adhii,

    This forum is discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools. Based on your code, your case more related to Kinect V2.   I will move your thread to Kinect for Windows SDKs> Kinect for Windows v2 SDK forum for better support.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 12, 2015 7:42 AM
  • There are two things to note here:

    • Kinect doesn't need to run in foreground thread. It may work in some background thread.
    • You need the UI thread only to render bitmap and to turn it into stream. However, I bet you can also find a library that doesn't need UI thread either.

    So what you might do is the following:

    • Run kinect sensor in background thread.
    • Once frame arrives copy it to some buffer and store somewhere (for example by using ConcurrentQueue). Then you'll let kinect continue to run without hiccup.
    • Run another background task to convert raw data you got in previous point into color pixels (you may use Parallel.For for that).
    • Put the processed frame in another buffer and call UI thread to render it.
    • IO operations are relatively slow, so you may want to 'save' bitmap data to MemoryStream (which is fast) and then save generated MemoryStream to file in another background thread.

    Hope this helps.

    • Proposed as answer by GregKol Friday, June 12, 2015 12:04 PM
    Friday, June 12, 2015 11:54 AM