Rosanswers logo

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->image.at<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)

Originally posted by shinnqy on ROS Answers with karma: 23 on 2014-05-04

Post score: 2

Original comments

Comment by rasoo on 2016-10-20:
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?


1 Answer 1


Rosanswers logo

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

Originally posted by Martin Günther with karma: 11816 on 2014-05-05

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by shinnqy on 2014-05-22:
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.

Comment by Jluis619 on 2016-06-06:
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... :)

Comment by rasoo on 2016-10-07:
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?

Comment by Abdu on 2017-05-19:
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

Comment by waschbaer on 2017-06-12:
But actually you should normalized it right?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.