7 from cv_bridge
import CvBridge, CvBridgeError
8 from sensor_msgs.msg
import Image, LaserScan
11 import tf2_geometry_msgs
12 from geometry_msgs.msg
import Point, PointStamped, Pose
24 self.
fx = 439.5975502749937
25 self.
fy = 439.5975502749937
47 while not rospy.is_shutdown():
50 "/relax/nicla/camera/image_raw", Image
55 ts = message_filters.ApproximateTimeSynchronizer(
56 [image_sub, depth_sub], 10, 0.1
64 self.
cv2_img = bridge.imgmsg_to_cv2(msg_rgb,
"bgr8")
72 self.
depth = msg_depth.ranges[0]
77 print(
"INFO: Received frame and depth.")
81 Convert central pixel coordinates to 3D coordinates using camera intrinsics and extrinsics.
82 Note: the central pixel is assumed to be the one for which the laser gives the depth info.
85 - depth: Depth value of the pixel
88 - Point in 3D space (x, y, z)
96 cam_point = [0, 0, 1.0]
97 cam_point[0] = (u - self.
cx) / self.
fx
98 cam_point[1] = (v - self.
cy) / self.
fy
101 cam_point = [value * depth
for value
in cam_point]
109 point_wrt_source = Point(x, y, z)
112 print(point_wrt_target)
118 Convert pixel coordinates to 3D coordinates using camera intrinsics and extrinsics.
121 - u, v: Pixel coordinates in the image
122 - depth: Depth value of the pixel
123 - K: Intrinsic camera matrix (3x3)
124 - T: Extrinsic camera matrix (4x4)
127 - Point in 3D space (x, y, z)
131 pixel_homogeneous = np.array([[u], [v], [1]])
134 K_inv = np.linalg.inv(K)
137 cam_point = np.dot(K_inv, pixel_homogeneous)
139 print(cam_point.flatten())
142 print(cam_point.flatten())
145 world_point = np.dot(T[:3, :3], cam_point) + T[:3, 3:]
147 return world_point.flatten()
150 point_wrt_target = tf2_geometry_msgs.do_transform_point(
151 PointStamped(point=point_wrt_source), transformation
153 return [point_wrt_target.x, point_wrt_target.y, point_wrt_target.z]
161 transformation = tf_buffer.lookup_transform(
162 target_frame, source_frame, rospy.Time(0), rospy.Duration(1.0)
165 tf2_ros.LookupException,
166 tf2_ros.ConnectivityException,
167 tf2_ros.ExtrapolationException,
170 "Unable to find the transformation from %s to %s" % source_frame,
173 return transformation
178 rospy.init_node(
"deproject_2D_to_3D", anonymous=
False)
184 if __name__ ==
"__main__":