Ask Your Question

How to get the distance and the angle (distance and bearing) from a depth camera

asked 2018-08-15 08:21:00 -0500

Ned gravatar image


I am using deep learning to detect an object and after I got the detection work, I made boxes around this object and I calculated the mid point of the box. Now I want to measure the distance and the angle between the camera and the mid point of the box around the object. I manged to get the coordinates of the mid point x,y in a frame but still don't know to calculate the distance and bearing. Thanks for advance. ps: I am using realsense camera D435.


edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2018-08-15 14:04:31 -0500

You should be able to find this information with a tangent function. With the pose of the camera and pose of the box in some global, identical frame (could even be camera frame) you can use the change in Y and change in X to pull out the azimuth angle if you're looking for 2D. You can use the change in Z as well to pull out azimuth and elevation angles.

It sounds like you already have distance, so that's a good start!

edit flag offensive delete link more


Thanks ! . Yes I got the distance so now I can measure the distance but need to figure out how to get the angle. I will try to apply what you have said. :)

Ned gravatar imageNed ( 2018-08-16 05:59:29 -0500 )edit

Awesome, can you mark the answer as correct then to close it out for maintainers?

stevemacenski gravatar imagestevemacenski ( 2018-08-16 18:20:15 -0500 )edit

answered 2018-08-15 15:41:51 -0500

zubair gravatar image

you can actually find the distance using point cloud 2 library, together with the topic /depth_registered_points. its working smooth.

edit flag offensive delete link more


you can use this :

int arrayPosition = y * my_pcl.row_step + x * my_pcl.point_step;
        int arrayPosZ = arrayPosition + my_pcl.fields[3].offset;

to get the depth, and then use :

memcpy(&Z_pos, &[arrayPosZ], sizeof(float));

to get them

zubair gravatar imagezubair ( 2018-08-15 15:44:11 -0500 )edit

Thank you !! I manged to get the distance but now need to get the angle because i will use those landmarks for feature-based SLAM.

Ned gravatar imageNed ( 2018-08-16 06:03:35 -0500 )edit

answered 2018-08-15 08:57:36 -0500

pavel92 gravatar image

Are you using the realsense2_camera package?
You should be able to access the depth value of the pixel from the depth image that you get with the camera. It should be published on a topic like depth/image_raw (sensor_msgs/Image) and it should contain uint16 depth values in mm. About the angle, I am not sure if there is a simpler way but based on the depth value that you get you can calculate the angle using simple trigonometry.

edit flag offensive delete link more


Thanks. Yes I am using this library and I manged to get the distance but I was wondering if there is already a simple solution for the angle.

Ned gravatar imageNed ( 2018-08-16 05:55:13 -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: 2018-08-15 08:21:00 -0500

Seen: 908 times

Last updated: Aug 15 '18