TofOnImage.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import rospy
4 from sensor_msgs.msg import CompressedImage, Image
5 from sensor_msgs.msg import Range
6 from cv_bridge import CvBridge, CvBridgeError
7 import cv2
8 
9 def draw_dot_on_image(cv_image, range_value:float, color=(0, 0, 255)):
10 
11  # Draw a red dot at the center of the image
12  height, width, _ = cv_image.shape
13  center = (int((width / 2)), int((height / 2)))
14  center_off = (center[0] + 6, center[1] - 19)
15 
16  cv2.circle(cv_image, center_off, 2, (0, 0, 255), -1)
17 
18  # Add a caption with the TOF range value near the center of the image
19  caption = f"{range_value:.2f}m"
20  font = cv2.FONT_HERSHEY_SIMPLEX
21  font_scale = 0.4
22  color = (0, 0, 255) # Red
23  thickness = 1
24  text_size, _ = cv2.getTextSize(caption, font, font_scale, thickness)
25  text_x = center_off[0] - text_size[0] // 2
26  text_y = center_off[1] - text_size[1] +5 # Slightly above the circle
27 
28  cv2.putText(
29  cv_image,
30  caption,
31  (text_x, text_y),
32  font,
33  font_scale,
34  color,
35  thickness,
36  )
37 
38 
39 class TofOnImage:
40  def __init__(self) -> None:
41 
42  nicla_name = rospy.get_param("~nicla_name", "nicla")
43  nicla_cam_topic = rospy.get_param(
44  "~nicla_cam_topic",
45  "/" + nicla_name + "/camera/image_raw/compressed",
46  )
47  img_out_topic = rospy.get_param(
48  "~img_out_topic",
49  "/" + nicla_name + "/camera_with_tof/image_raw/compressed",
50  )
51  self.nicla_cam_compressed = rospy.get_param(
52  "~nicla_cam_compressed", True
53  )
54  nicla_range_topic = rospy.get_param(
55  "~nicla_range_topic", "/" + nicla_name + "/tof"
56  )
57 
58  if self.nicla_cam_compressed:
59  self.image_sub = rospy.Subscriber(
60  nicla_cam_topic, CompressedImage, self.image_callback
61  )
62  self.image = CompressedImage()
63  else:
64  self.image_sub = rospy.Subscriber(
65  nicla_cam_topic, Image, self.image_callback
66  )
67  self.image = Image()
68 
69  self.range_sub = rospy.Subscriber(
70  nicla_range_topic, Range, self.range_callback
71  )
72 
73  self.img_pub = rospy.Publisher(
74  img_out_topic, CompressedImage, queue_size=10
75  )
76 
77  self.out_image = CompressedImage()
78  self.range = Range()
79 
80  self.bridge = CvBridge()
81 
82  self.img_arrived = False
83 
84  rospy.loginfo("TofOnImage initialized")
85 
86  def image_callback(self, data):
87  self.img_arrived = True
88  self.image = data
89 
90  def range_callback(self, data):
91  self.range = data
92 
93  def run(self):
94 
95  if not self.img_arrived:
96  return
97 
98  try:
99  if self.nicla_cam_compressed:
100  cv_image = self.bridge.compressed_imgmsg_to_cv2(
101  self.image, "passthrough"
102  )
103  else:
104  cv_image = self.bridge.imgmsg_to_cv2(self.image, "bgr8")
105  except CvBridgeError as e:
106  rospy.logerr(e)
107 
108  draw_dot_on_image(cv_image, self.range.range)
109 
110  try:
111  self.out_image.data = self.bridge.cv2_to_compressed_imgmsg(
112  cv_image, dst_format="jpg"
113  ).data
114  except CvBridgeError as e:
115  rospy.logerr(e)
116 
117  self.img_pub.publish(self.out_image)
118 
119  pass
nicla_receiver.int
int
Definition: nicla_receiver.py:14
nicla_vision_ros.TofOnImage.TofOnImage.bridge
bridge
Definition: TofOnImage.py:80
nicla_vision_ros.TofOnImage.TofOnImage.run
def run(self)
Definition: TofOnImage.py:93
nicla_vision_ros.TofOnImage.TofOnImage.range
range
Definition: TofOnImage.py:78
nicla_vision_ros.TofOnImage.TofOnImage
Definition: TofOnImage.py:39
nicla_vision_ros.TofOnImage.TofOnImage.nicla_cam_compressed
nicla_cam_compressed
Definition: TofOnImage.py:51
nicla_vision_ros.TofOnImage.TofOnImage.img_pub
img_pub
Definition: TofOnImage.py:73
nicla_vision_ros.TofOnImage.TofOnImage.range_sub
range_sub
Definition: TofOnImage.py:69
nicla_vision_ros.TofOnImage.TofOnImage.range_callback
def range_callback(self, data)
Definition: TofOnImage.py:90
nicla_vision_ros.TofOnImage.draw_dot_on_image
def draw_dot_on_image(cv_image, float range_value, color=(0, 0, 255))
Definition: TofOnImage.py:9
nicla_vision_ros.TofOnImage.TofOnImage.img_arrived
img_arrived
Definition: TofOnImage.py:82
nicla_vision_ros.TofOnImage.TofOnImage.image
image
Definition: TofOnImage.py:62
nicla_vision_ros.TofOnImage.TofOnImage.image_sub
image_sub
Definition: TofOnImage.py:59
nicla_vision_ros.TofOnImage.TofOnImage.out_image
out_image
Definition: TofOnImage.py:77
nicla_vision_ros.TofOnImage.TofOnImage.image_callback
def image_callback(self, data)
Definition: TofOnImage.py:86
nicla_vision_ros.TofOnImage.TofOnImage.__init__
None __init__(self)
Definition: TofOnImage.py:40


nicla_vision_ros
Author(s): Davide Torielli , Damiano Gasperini , Edoardo Del Bianco , Federico Rollo
autogenerated on Sat Nov 16 2024 03:38:18