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

How can I get depth data from Kinect?

asked 2014-05-04 20:52:49 -0600

shinnqy gravatar image

updated 2014-05-22 01:54:56 -0600

Hi guys, I'm quite new to ROS. I'm using the Ubuntu 12.04 and openni driver.

What I've been trying to do is to use Kinect to detect whether a door is open or closed. My idea is to write a node to subscribe to camera/depth/image, and and can retrieve a distance at the center point in the image.

But when I use rostopic echo camera/depth/image I just get a lot of numbers like 0, 0, 65, 128, 36, 56, 77, 102, 140, 65. So does anyone know what should I do with these numbers, or are there other ways to get depth data?

Firstly, thank Martin Günther's answer, it really help me a lot. And I already solve the problem by using OpenCV. Here is part of my code, it's very easy. If some beginners cannot understand that hackish solution, maybe you should try this one:

#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include ..........

namespace enc = sensor_msgs::image_encodings;
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
    cv_bridge::CvImagePtr cv_ptr;
      cv_ptr = cv_bridge::toCvCopy(msg, enc::TYPE_16UC1);//now cv_ptr is the matrix, do not forget "TYPE_" before "16UC1"
    catch (cv_bridge::Exception& e)
      ROS_ERROR("cv_bridge exception: %s", e.what());

    int depth = cv_ptr-><short int>(cv::Point(240,320));//you can change 240,320 to your interested pixel
    ROS_INFO("Depth: %d", depth);

int main(int argc, char **argv)
edit retag flag offensive close merge delete


Hello, thanks for updating your solution here. It is quite easy to understand. But I have a question. Is there any quick solution to get the minimum depth of pixels. I know doing a for loop will give me the minimum depth. But is there any other solution for this?

rasoo gravatar image rasoo  ( 2016-10-20 06:48:41 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2014-05-05 23:24:09 -0600

No, you're on the right track, subscribing to the depth image is a good way to figure out the distance. The numbers you mention are a one-dimensional integer array (data); what you now have to do is convert that into a two-dimensional float array that is indexed by pixels and where each float specifies the depth in meters. The encoding field tells you which encoding to use.

Some pointers:

  • OpenCV seems to be a good option (see this answer) to do what you want
  • There is a horribly hackish solution here
  • REP 118 specifies the representation of depth images in ROS
  • the comments in sensor_msgs/Image are very helpful to understand what's going on
edit flag offensive delete link more


Thank you very much. I already solved the problem. The hackish solution you mentioned works, but it's difficult for a beginner like me to understand. So I tried OpenCV as you answered and I will update it under my question for other beginners.

shinnqy gravatar image shinnqy  ( 2014-05-22 01:39:24 -0600 )edit

Hello, could you give me the full code? I'm new to this. and I would like to work with images in depth. Thank you... :)

Jluis619 gravatar image Jluis619  ( 2016-06-06 23:58:21 -0600 )edit

Hi, I was using the rostopic "/camera/depth/points" .... I thought I was in the right track for getting depth data to measure the distance... Can you please suggest me if it works in a right way or not?

rasoo gravatar image rasoo  ( 2016-10-07 08:27:34 -0600 )edit

thx for sharing your answer, can u plz provide the full file, I am getting numbers for depth data and I don't know how to read it. plz guide me more

Abdu gravatar image Abdu  ( 2017-05-19 10:51:23 -0600 )edit

But actually you should normalized it right?

waschbaer gravatar image waschbaer  ( 2017-06-12 11:35:58 -0600 )edit

Question Tools



Asked: 2014-05-04 20:52:49 -0600

Seen: 6,674 times

Last updated: Jun 12 '17