ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Importing rgbd data from bag file into matlab [closed]

asked 2017-04-27 08:41:09 -0500

Keithicus gravatar image

I'm trying to plot the points measured from a microsoft kinect sensor in matlab. I connected to my sensor using the freenect_launch package, I can visualize the sensor output in rviz, and i recorded the topic /camera/depth_registered/points into a bag file. Unfortunately i dont have the ROS tools for matlab, but i did find this open source solution to read the bag file into matlab:

https://github.com/unl-nimbus-lab/bag...

I can see that the data from the bag file is imported. A single frame looks something like this:

  • rosbagTimestamp 1.49246E+18
  • header
  • seq 205
  • stamp
  • secs 1492455101
  • nsecs 150824989
  • frame_id camera_rgb_optical_frame
  • height 480
  • width 640
  • fields
  • name x; offset 0; datatype 7; count 1
  • name y; offset 4; datatype 7; count 1
  • name z; offset 8; datatype 7; count 1
  • name rgb; offset 16; datatype 7; count 1
  • is_bigendian false
  • point_step 32
  • row_step 20480
  • data [0, 0, 192, 127, 0, 0, 192, 127, 0, 0, 192, 127, 0, 0, 0, 0, 68, 84, 124, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...

My question is how do i convert the values in "data" to xyz points? If i'm interpreting this correctly, each data point is 32 bytes, bytes 0 - 3 correspond to X, bytes 4 - 7 correspond to Y, bytes 8 - 15 correspond to Z, and bytes 16 - 31 correspond to "rgb" (though i dont see how thats an rgb value). I dont know what "datatype 7" means, but it appears that the data is little endian. I'm wondering what datatype X, Y, Z and RGB are (unit32? float?) So that I can make the conversion. Has anyone ever dealt with this before?

Thanks!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Keithicus
close date 2017-05-01 16:44:12.618162

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-05-01 16:40:56 -0500

Keithicus gravatar image

I figured it out! So the X, Y and Z values are all single precision floats represented with 4 bytes. In the 32 byte packet, bytes 0-3 are X, bytes 4-7 are Y, bytes 8-11 are Z, byte 17 is Blue, byte 18 is Green, and byte 19 is Red. As far as I can tell, the remaining 17 bytes in the packet are unused, which is horribly inefficient and makes for data files about twice as large as they need to be.

Suppose you wanted to get these values in matlab. If you use the bagReader function above (must be used in matlab for linux) you can do the type casting for a pixel in a frame as so (this presumes the only topic you recorded was /camera/depth_registered/points):

clear; close all; clc;
[data, names] = bagReader('myKinectScan.bag');

frameNumber = 1;
frameData = data.data{frameNumber,:};
pixelX = 324;
pixelY = 243;
pixelLocation = (pixelX-1)*32 + (pixelY-1)*32*640 + 1;
dataPacket = frameData(pixelLocation:32+pixelLocation);

x = typecast(uint8(dataPacket(1:4)),'single');
y = typecast(uint8(dataPacket(5:8)),'single');
z = typecast(uint8(dataPacket(9:12)),'single');
b = uint8(dataPacket(17));
g = uint8(dataPacket(18));
r = uint8(dataPacket(19));

Hope this helps someone else!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-04-27 08:41:09 -0500

Seen: 235 times

Last updated: May 01 '17