Ask Your Question

Depth image stream from kinect in matlab with ROS IO package

asked 2014-05-14 10:05:01 -0600

renangm gravatar image

Hello all,

I'm currently trying to generate image streams in Matlab from Kinect with the relatively new ROS IO Package. I am able to generate an RGB stream with the following code:

 width = message.getWidth();
        height = message.getHeight();
        offset = message.getData().arrayOffset();
        indexB = offset+1:3:width*height*3+offset;
        indexG = indexB+1;
        indexR = indexG+1;

        imgCol = typecast(message.getData().array(), 'uint8');
        img = reshape([imgCol(indexR); imgCol(indexG); imgCol(indexB)], width, height, 3);
        img = permute(img, [2 1 3]);

But I have no idea how to generate the depth stream. For the master, I'm running the freenect package in a terminal. Here's how I'm setting up the node:

>> node = rosmatlab.node('kinect', master_uri);
>> subdep = node.addSubscriber('/camera/depth/image_rect', 'sensor_msgs/Image', 10);
>> subdep.setOnNewMessageListeners({@function});

I read somewhere that the encoding type for the /image_rect topic is 32FC1 and for /image_raw is 16UC1. Is that relevant somehow?

Thanks for the help.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2014-05-14 12:13:43 -0600

renangm gravatar image

Figured it out. Might help someone down the line. The /camera/depth/image_raw topic provides data in a 8-bit integer array format of 307234 positions, which can be read with the getData().array() method. It has to be converted to the correct matrix format to be displayed as an image in matlab. Basically, you have to transform a 307234x1 vector into a 640x480 matrix. The code below gets the message array and transforms it to the matrix to be displayed. It was based on the one I used for RGB imaging.

function function2(message) 

width = message.getWidth();
height = message.getHeight();
offset = message.getData().arrayOffset();
index = offset+2:2:width*height*2+offset;

%Get the data in unsigned int8 format
msg = typecast(message.getData().array(), 'uint8');

%Reshape 614469x1 (614469 bytes) into 640x480 (307200 bytes)
img = reshape(msg(index), width, height);

%Flip dimensions
img = flipdim(img, 2);

%Rotate image in 90 degrees
img_rot = imrotate(img, 90);

%Show rotated image


I chose to use the flipdim() and imrotate() methods because I am more familiarized with them. The permute method should have the same result. It's also possible to use imagesc() instead of imshow(), for a different visual result.

edit flag offensive delete link more

answered 2014-09-12 08:02:12 -0600

dbs12 gravatar image

updated 2014-09-12 09:24:28 -0600


Am trying to do the same thing but struggling to get data from kinect. Since it is question regarding same topic..I did not create a new post. Here is what am doing, if you see something missing please let me know:

  1. In terminal (using ros-groovy):

    roslaunch openni_launch openi.launch
  2. In Matlab:

    node = rosmatlab.node('NODE',master_uri)
    subscriber = rosmatlab.subscriber('/camera/rgb/image_rect','sensor_msgs/Image',10,node);
    imgMsg = rosmatlab.message('sensor_msgs/Image',node);

It can be seen with rostopic info that NODE is subscribed to topic /camera/rb/image_rect but imgMsg.getData() is always zero.

Could you please tell if am missing any steps.


edit flag offensive delete link more

answered 2016-01-17 21:06:57 -0600

hi i got the same problem ,i want to get the depth and image stream from xtion with matlab and ros bridge, i can see the topic info but i can not take message, do you have done that problem @dbs12, thanks!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2014-05-14 10:05:01 -0600

Seen: 1,590 times

Last updated: Sep 12 '14