Ask Your Question

How to use depth value from /camera/depth/image?

asked 2014-02-01 00:54:14 -0500

Safeer gravatar image

updated 2014-04-20 14:09:25 -0500

ngrennan gravatar image

I am using this code

topic is: /camera/depth/image
type: /sensor_msgs/Image

According to the code, (msg->data[0]) is providing the value of Top-Left Corner.

I wanted to ask:

  1. How can I get the value of the Top-Right Corner, Centre, etc.
  2. How can I get the normal depth value that can tell me the distance from the object? Do I need to do some mathematical work or is there a method?
edit retag flag offensive close merge delete


Your reference code helped, thanks

askkvn gravatar image askkvn  ( 2021-06-24 23:42:56 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2014-02-06 23:50:08 -0500

Wolf gravatar image

In the code the message is already converted to a cv::Mat ( cv_ptr->image ). Use this Matrix if you want to do some processing of the data.

E.g. you can use

float dist_val = cv_ptr-><float>( 10, 10 );

to get the value at 10, 10. (There are much faster methods if you not just want to access one pixel, though).

See, for infos on how to handle your data with OpenCV.

edit flag offensive delete link more


Thanks for your help. I visited the link you gave. Please guide me which topics/tutorials should I follow?

Safeer gravatar image Safeer  ( 2014-02-10 01:37:27 -0500 )edit

That very much depends on what you actually want to do. Probably some of the core and improc tutorials. If you want to do object detection: "Creating Bounding boxes and circles for contours"(particularly cv::findContours() usage). Threshold works also for dist imgs. Likely you need some normalizing.

Wolf gravatar image Wolf  ( 2014-02-10 19:37:54 -0500 )edit

Thanks for your reply.

Safeer gravatar image Safeer  ( 2014-02-17 01:16:32 -0500 )edit

Thanks, @Wolf, this solution worked for me. I have used /camera/depth/image_raw as an input rostopic.

My answer with complete code (c++):


  • To get image dimension: std::cout << "Image dimension (Row x Col): " << cv_ptr->image.rows << " x " << cv_ptr->image.cols << endl;

  • To get image global max and min depth values: double min = 0.0; double max = 0.0; cv::minMaxLoc(cv_ptr->image, &min, &max, 0, 0); std::cout << "Min value: " << min << " " << "Max value: " << max << endl;

askkvn gravatar image askkvn  ( 2021-06-24 23:44:35 -0500 )edit

answered 2017-04-18 09:56:03 -0500

zubair gravatar image

hi but this is giving me always dist_val=0 !! why is that so ??

edit flag offensive delete link more


Check if your sensor is working fine and is at an acceptable distance from an object.

Safeer gravatar image Safeer  ( 2017-04-18 13:49:44 -0500 )edit

Hello, I have the same problem. Did you find any solution for this issue? Thank you.

User gravatar image User  ( 2018-07-06 06:42:56 -0500 )edit

Again, check if the sensor is working fine.

Safeer gravatar image Safeer  ( 2018-07-09 00:53:24 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2014-02-01 00:54:14 -0500

Seen: 6,547 times

Last updated: Apr 18 '17