librealsense icon indicating copy to clipboard operation
librealsense copied to clipboard

How to get distance map(or depth map) in Unity

Open wodndb opened this issue 7 years ago • 1 comments


Required Info
Camera Model D435
Firmware Version 05.11.01.00
Operating System & Version Win10(2018.3.0.f2)
Platform Unity
SDK Version 2.18.0
Language C#
Segment others

Issue Description

<Describe your issue / question / feature request / etc..> I want to get 2D distance map for detecting keypoint based on distance map. When I used RealSense SDK 2.17.1, I can get distance map in DepthFrame from OnNewSampleSet action event. However, I can't get RealSense SDK 2.17.0 and 2.18.0 because these SDK versions don't provide OnNewSampleSet action event in RsDevice.cs

void getDepthDataFromFrame(Frame frame)
    {
        try
        {
            if(frame.Profile.Format == Format.Z16) {
                using (var fs = FrameSet.FromFrame(frame))
                {
                    vidFrame = fs.DepthFrame;
                    data = data ?? new byte[vidFrame.Stride * vidFrame.Height];
                    vidFrame.CopyTo(data);
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogException(e, this);
        }
    }

I add this method to OnNewSample action event and it just work for 1 sec in live streaming.

How can I get distance map or information using SDK 2.16.0 or 2.18.0 for unity?

wodndb avatar Jan 24 '19 02:01 wodndb

I fixed this problem based on 2.17.0

I made OnNewSample method to get frameSet. and I added this method to RsDevice.OnNewSample Action Event in Unity Start function.

    public RsDevice rsDevice;
    
    Texture2D resultTexture;
    int dfWidth = 0, dfHeight = 0, dfStride = 0;
    byte[] data;
    bool isInitTexture = false;

    void OnNewSample(Frame frame)
    {
        using (var frameSet = frame.AsFrameSet())
        {
            using (var depthFrame = frameSet.DepthFrame as DepthFrame)
            {
                if (depthFrame != null)
                {
                    dfWidth = depthFrame.Width;
                    dfHeight = depthFrame.Height;
                    dfStride = depthFrame.Stride;
                    data = data ?? new byte[dfStride * dfHeight];
                    depthFrame.CopyTo(data);
                    Debug.Log("Depth Frame data is not null");
                }
            }
        }
    }

wodndb avatar Jan 24 '19 07:01 wodndb