How to get accurate depth value from openni_camera?

asked 2014-08-20 09:13:52 -0500

Qt_Yeung gravatar image

updated 2014-08-20 09:14:45 -0500

Hi all, i'm using openni_camera to get depth value between a book and the ASUS Xtion pro camera. Let's say the depth value in the image center(240,320).

I followed the solution here, and i get the output depth as integer,such as 1 meter or 2 meters, but how to get the result more accurate? I mean how to get it as float or double.

I try to change the code into: double depth = cv_ptr-><double>(240, 320), i get the output 0 or 65537 or 131074. What's the meaning here?

edit retag flag offensive close merge delete



According to the openni_camera doc, published depth images contain uint16 depth in millimetres. Also, this question is a possible duplicate of this.

Murilo F. M. gravatar image Murilo F. M.  ( 2014-08-20 09:48:08 -0500 )edit

Yes, i also see and follow that solution, as i said, i can get the depth value as integer but not float. It is 0, 1, 2 or 3 meter and so on. My question is, how to get the accurate value in float?

This is my code:

void imageCallback(const sensor_msgs::ImageConstPtr& msg) {

cv_bridge::CvImagePtr cv_ptr;
  cv_ptr = cv_bridge::toCvCopy(msg, enc::TYPE_16UC1);
catch (cv_bridge::Exception& e)
  ROS_ERROR("cv_bridge exception: %s", e.what());

int depth = cv_ptr-><short int>(240, 320);
ROS_INFO("Depth: %d", depth);


Qt_Yeung gravatar image Qt_Yeung  ( 2014-08-20 14:24:11 -0500 )edit

Do you mean that, when you read the depth value as integer, you only get values such as 0, 1000, 2000, 3000, and nothing in between those values? In your code, depth will be in millimetres and hence converting to float (in metres) is just a matter of float depth_f = float(depth) / 1000.0;.

Murilo F. M. gravatar image Murilo F. M.  ( 2014-08-20 14:35:40 -0500 )edit

No, i mean i only get 0, 1, 2, 3, 4.......and nothing like 0.1, 0.2 and so on, i think it's in meters. But i want to get accurate values such as 0.1, 0.2, 0.3......1.0,1.1, 1.2...

Qt_Yeung gravatar image Qt_Yeung  ( 2014-08-20 15:16:19 -0500 )edit

You will not get floating point values from the depth image, it stores the values as integers, in millimetres. Suppose you know the depth of a given pixel should be 0.5 metres, then you should get a depth = 500 (you must convert to floating point metres yourself).

Murilo F. M. gravatar image Murilo F. M.  ( 2014-08-20 15:34:16 -0500 )edit