ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org

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

Hi,

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 close merge delete

Sort by ยป oldest newest most voted

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!

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. :)

( 2018-08-16 05:59:29 -0600 )edit
1

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

( 2018-08-16 18:20:15 -0600 )edit

@Jad, hi, would you please share your code with me as wwll? i have the same problem, get the distance and bearing of the nearest object

( 2022-08-05 08:50:39 -0600 )edit

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.

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.

( 2018-08-16 05:55:13 -0600 )edit

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

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, &my_pcl.data[arrayPosZ], sizeof(float));


to get them

( 2018-08-15 15:44:11 -0600 )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.

( 2018-08-16 06:03:35 -0600 )edit