Hi,
The command I have runed:
roslaunch realsense2_camera rs_rgbd.launch filters:=colorizer align_depth:=true
#!/usr/bin/env python
import rospy
from cv_bridge import CvBridge, CvBridgeError
from sensor_msgs.msg import Image
import numpy as np
import cv2
def convert_depth_image(ros_image):
bridge = CvBridge()
# Use cv_bridge() to convert the ROS image to OpenCV format
try:
# Convert the depth image using the default passthrough encoding
depth_image = bridge.imgmsg_to_cv2(ros_image, desired_encoding="passthrough")
depth_array = np.array(depth_image, dtype=np.float32)
center_idx = np.array(depth_array.shape) / 2
print('center depth:', depth_array[int(center_idx[0]), int(center_idx[1])]) # Changed Over here
#
# cv2.imshow("Depth_Sub", depth_image)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# rospy.signal_shutdown('Quit')
# cv2.destroyAllWindows()
except CvBridgeError:
print('Error!!')
# Convert the depth image to a Numpy array
def pixel2depth():
rospy.init_node('pixel2depth', anonymous=True)
rospy.Subscriber("/camera/aligned_depth_to_color/image_raw", Image, callback=convert_depth_image, queue_size=1)
rospy.spin()
if __name__ == '__main__':
pixel2depth()
I have changed this line : print('center depth:', depth_array[int(center_idx[0]), int(center_idx[1])]) # because of flot issue while dividing by 2.
What is this please provide more clarification on this above lists and How can I get the distance in mm?
Thanks and Regards
Hi @ranjitkathiriya The tutorial in the link below that uses Python and ROS provides a method for converting 2D xy to 3D xyz that may be simpler:
https://medium.com/@yasuhirachiba/converting-2d-image-coordinates-to-3d-coordinates-using-ros-intel-realsense-d435-kinect-88621e8e733a
It makes use of ROS information provided by sensor_msgs/CameraInfo
Actually, I found this code in this issue and It was uploaded by @doronhi.
https://github.com/IntelRealSense/realsense-ros/issues/714
In this comment so I want an explanation of the outputs: like what is this :: center depth: [213. 255. 41.]
https://github.com/IntelRealSense/realsense-ros/issues/714#issuecomment-561036465
I guess you are referring to the edited my_depth_subscriber.txt text file version of the original script that was provided by @doronhi
https://github.com/IntelRealSense/realsense-ros/issues/714#issuecomment-561036465
Apparently the values that are output from the script are in millimeters once converted to OpenCV format.
Okay, That's the explanation I wanted to thank you so much.
here Z is in form of mm or I have to apply some OpenCV Technique and calculate mm from these three coordinates?
I have checked the code many times but I cannot be sure whether the XYZ value output after conversion to OpenCV is in mm or needs to undergo further conversion to display the distance in mm.
@doronhi Can you advise on this please? Thanks!
From the README.md file:
colorizer: will color the depth image. On the depth topic an RGB image will be published, instead of the 16bit depth values .
So, while using the filter:=colorizer option, you no longer get the depth but RGB values.
Remove that filter and you'll get depth in millimeters.
Hi @doronhi,
Thanks for providing me the explanation. I will check and update you back thanks
Thanks @doronhi it's working
Most helpful comment
From the README.md file:
So, while using the
filter:=colorizeroption, you no longer get the depth but RGB values.Remove that filter and you'll get depth in millimeters.