Software Archive
Read-only legacy content
17060 Discussions

RealSense SDK for Depth Information

Shrutish_R_
Beginner
2,404 Views

Hello,

Is there an updated SDK for getting the depth information (distance) of each pixels in an image ? I am using Intel RealSense SR300. Please Help !

0 Kudos
1 Solution
jb455
Valued Contributor II
2,384 Views

Assuming you already have a depth image, this code gives you an array of depth values (rounded to the nearest mm) the size of the depth image:

dImage.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH, out ddata);
dwidth = dImage.info.width;
var dheight = dImage.info.height;
dpitch = dwidth; //(int)(mdata.pitches[0] / 2.0);
Get the depth values:
dPixels = ddata.ToShortArray(0, dwidth * dheight);
dImage.ReleaseAccess(mdata);
dImage.Dispose();

 

So if you want the depth for a specific pixel, say (u, v), it'll be dPixels[v * dpitch + u]

View solution in original post

0 Kudos
22 Replies
Alex_P_5
Beginner
475 Views

The Raw Streams sample is a good place to start if you want to learn how it all works. Be aware, if you want to get depth data for points in the colour image, you'll have to map between the colour and depth images as the two cameras point to different locations and have different fields of view etc. Search the docs and this forum for 'projection' and there's information on most use cases.

0 Kudos
Niyonsaba_E_
Beginner
475 Views

@Alex

Thank for your quick response.I just want to display depth data for points in depth image. The problem i have is that i got zero for all points.

I don't know if i have an mistake.Here is my simple code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Threading;
using System.IO;

namespace DepthApp
{    
    
    public partial class MainWindow : Window
    {
        private PXCMSession session;
        private PXCMSenseManager senseManager;
        private Thread update;
        PXCMPoint3DF32[]vertices;
        int tablelength;
        ushort[] distance;

        public MainWindow()
        {
            InitializeComponent();
            session = PXCMSession.CreateInstance();
            senseManager = session.CreateSenseManager();
            distance = new ushort[320 * 240];
            senseManager.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_DEPTH, 320, 240, 30);
            senseManager.Init();
            update = new Thread(new ThreadStart(Update));
            update.Start();
           
           
        }

        private void Update()
        {
            while (senseManager.AcquireFrame(true) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
            {
                
              
                PXCMCapture.Sample sample = senseManager.QuerySample();
                Bitmap depthBitmap;
                PXCMImage.ImageData depthData;
                PXCMImage.ImageData depthimage;
                PXCMImage img=sample.depth;
                img.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32, out depthData);
                img.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH, out depthimage);
                depthBitmap = depthData.ToBitmap(0, sample.depth.info.width, sample.depth.info.height);
                Render(depthBitmap);
                distance = depthimage.ToUShortArray(0, img.info.width * img.info.height);
                depthBitmap.Dispose();
                sample.depth.ReleaseAccess(depthData);
                senseManager.ReleaseFrame();
        
             
            }

            
        }

        private void Render(Bitmap bitmap)
        {
            BitmapImage bitmapImage = ConvertBitmap(bitmap);
            if (bitmapImage != null)
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                {
                    depthimage.Source = bitmapImage;

                }
                    ));
            }

        }

        private BitmapImage ConvertBitmap(Bitmap bitmap)
        {
            BitmapImage bitmapImage = null;
            if (bitmap != null)
            {
                MemoryStream memoryStream = new MemoryStream();
                bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
                memoryStream.Position = 0;
                bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                bitmapImage.Freeze();

            }

            return bitmapImage;
        }


    }
}

distance array i got for all depth values zero.Please help me.

0 Kudos
Reply