Items with no label
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
3338 Discussions

MATLAB depth image with real depth values (no colorizer)

JBos3
New Contributor I
4,251 Views

How can I acquire a depth image in MATLAB with the actual depth values? Depth_example.m demonstrates how to acquire a depth stream and colorize for visualization, but I need the real depth values in units of length. For example, I would like a 840x480 image where each pixel value is the depth in mm or meters, or alternatively an image where each pixel value is an integer and we have the corresponding depth scale factor. I've seen the C++ examples, but it is not clear how these translate to MATLAB.

 

Thank you.

0 Kudos
1 Solution
JBos3
New Contributor I
2,946 Views

Got it... hope this helps someone else.

 

 % Make Pipeline object to manage streaming

  pipe = realsense.pipeline();

 

  % Start streaming on an arbitrary camera with default settings

  profile = pipe.start();

   

  % Get streaming device's name

  dev = profile.get_device();

  name = dev.get_info(realsense.camera_info.name);

 

  % Get frames. We discard the first couple to allow

  % the camera time to settle

  for i = 1:5

    fs = pipe.wait_for_frames();

  end

   

  % Stop streaming

  pipe.stop();

   

  % Select depth frame

  depth = fs.get_depth_frame();

   

  % get depth image parameters

  depthSensor = dev.first('depth_sensor');

  depthScale = depthSensor.get_depth_scale();

  depthWidth = depth.get_width();

  depthHeight = depth.get_height();

   

  % retrieve UINT16 depth vector

  depthVector = depth.get_data();

   

  % reshape vector, and scale to depth in meters

  depthMap = double(transpose(reshape(depthVector, [depthWidth,depthHeight]))) .* depthScale;

   

  figure(1);

  imshow(depthMap);

  title('Depth Map (m)');

View solution in original post

0 Kudos
5 Replies
MartyG
Honored Contributor III
2,946 Views

If you are satisfied with getting the depth data from an image file, an Intel support agent offered advice about getting depth in MATLAB from a .raw file created in the RealSense Viewer.

 

*****

 

In order to read a raw file produced with Intel.RealSense.Viewer.exe, use this code:

 

FID = fopen('IntelDepth.raw');

 

DepthImage = fread(FID, [1280, 720], 'uint16')';

 

fclose(FID);

 

Make sure you configure the depth image as z16 (16 bit). To capture a stream of images, use the record button.

 

0 Kudos
JBos3
New Contributor I
2,946 Views

Thanks Marty. I've seen this solution and it works very well, but for my application I need to stream real depth images directly into MATLAB.

0 Kudos
MartyG
Honored Contributor III
2,946 Views

Of the commercial products available, the Image Acquisition Toolbox that Intel agent Eliza recommended in your MATLAB RGB point cloud post is the most likely paid solution for what you want ti achieve.

 

https://uk.mathworks.com/products/imaq.html

 

Alternatively, MATLAB has a 'Robotics System Toolbox' for interfacing with ROS.

 

https://uk.mathworks.com/products/robotics.html

 

The ideal situation would be to do it via the RealSense SDK MATLAB wrapper, if you can use Windows 10. I am not an expert in MATLAB wrapper programming, unfortunately. To avoid getting colorization, a 'hacky' approach might be to delete line 26 of the depth colorizer example that initiates the colorizing, and see if that gives you a non-colorized stream.

 

https://github.com/IntelRealSense/librealsense/blob/master/wrappers/matlab/depth_example.m#L26

0 Kudos
JBos3
New Contributor I
2,946 Views

I agree - one would reasonably expect to retrieve depth values from the MATLAB SDK wrapper. Unfortunately, commenting line 26 doesn't work, since the 'color' object is used in line 30 to retrieve the data. Hopefully, someone from the RealSense team will see this and respond.

0 Kudos
JBos3
New Contributor I
2,947 Views

Got it... hope this helps someone else.

 

 % Make Pipeline object to manage streaming

  pipe = realsense.pipeline();

 

  % Start streaming on an arbitrary camera with default settings

  profile = pipe.start();

   

  % Get streaming device's name

  dev = profile.get_device();

  name = dev.get_info(realsense.camera_info.name);

 

  % Get frames. We discard the first couple to allow

  % the camera time to settle

  for i = 1:5

    fs = pipe.wait_for_frames();

  end

   

  % Stop streaming

  pipe.stop();

   

  % Select depth frame

  depth = fs.get_depth_frame();

   

  % get depth image parameters

  depthSensor = dev.first('depth_sensor');

  depthScale = depthSensor.get_depth_scale();

  depthWidth = depth.get_width();

  depthHeight = depth.get_height();

   

  % retrieve UINT16 depth vector

  depthVector = depth.get_data();

   

  % reshape vector, and scale to depth in meters

  depthMap = double(transpose(reshape(depthVector, [depthWidth,depthHeight]))) .* depthScale;

   

  figure(1);

  imshow(depthMap);

  title('Depth Map (m)');

0 Kudos
Reply