1
+ #!/usr/bin/python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # ============================================
5
+ __author__ = "ShigemichiMatsuzaki"
6
+ __maintainer__ = "ShigemichiMatsuzaki"
7
+ # ============================================
8
+
9
+ import rospy
10
+ import cv2
11
+ import message_filters
12
+ from sensor_msgs .msg import Image
13
+ from geometry_msgs .msg import PointStamped
14
+ from cv_bridge import CvBridge
15
+ import copy
16
+
17
+ class Visualizer :
18
+ """ """
19
+ def __init__ (self ):
20
+ """ Constructor of Visualizer class
21
+
22
+ - define the class variables (ROS publisher and subscribers etc.)
23
+
24
+ """
25
+ # Subscribers
26
+ self .image_sub = message_filters .Subscriber ('image' , Image )
27
+ self .start_point_sub = message_filters .Subscriber ('start_point' , PointStamped )
28
+ self .end_point_sub = message_filters .Subscriber ('end_point' , PointStamped )
29
+ # Time synchronizer
30
+ self .ts = message_filters .TimeSynchronizer ([self .image_sub , self .start_point_sub , self .end_point_sub ], 100 )
31
+ self .ts .registerCallback (self .image_points_callback )
32
+
33
+ # Publisher
34
+ self .image_pub = rospy .Publisher ('image_and_path' , Image , queue_size = 100 )
35
+
36
+ self .bridge = CvBridge ()
37
+
38
+ def image_points_callback (self , img_msg , start_point_msg , end_point_msg ):
39
+ """Callback of image and point messages
40
+
41
+ Args:
42
+ img_msg(sensor_msgs/Image)
43
+ start_point_msg(geometry_msgs/PointStamped)
44
+ end_point_msg(geometry_msgs/PointStamped)
45
+ """
46
+ # Convert the image message to
47
+ cv_image = self .bridge .imgmsg_to_cv2 (img_msg , desired_encoding = 'passthrough' )
48
+
49
+ cv_image_with_line = self .draw_line (cv_image ,
50
+ (int (start_point_msg .point .x ), int (start_point_msg .point .y )),
51
+ (int (end_point_msg .point .x ), int (end_point_msg .point .y )))
52
+
53
+ vis_img_msg = self .bridge .cv2_to_imgmsg (cv_image_with_line )
54
+
55
+ self .image_pub .publish (vis_img_msg )
56
+
57
+ def draw_line (self , cv_image , start_point , end_point ):
58
+ """Draw a line, whose start and end points are given as PointStamped messages, on the image
59
+
60
+ Args:
61
+ cv_image(OpenCV image)
62
+ start_point_msg(geometry_msgs/PointStamped)
63
+ end_point_msg(geometry_msgs/PointStamped)
64
+
65
+ Return:
66
+ OpenCV image with a line drawn
67
+ """
68
+ ret_image = copy .deepcopy (cv_image )
69
+
70
+ cv2 .line (ret_image , start_point , end_point , color = (0 , 0 , 255 ), thickness = 10 )
71
+
72
+ return ret_image
73
+
74
+ def main ():
75
+ """Main function to initialize the ROS node"""
76
+ rospy .init_node ("visualizer" )
77
+
78
+ visualizer = Visualizer ()
79
+
80
+ rospy .loginfo ('visualizer is initialized' )
81
+
82
+ rospy .spin ()
83
+
84
+
85
+ if __name__ == '__main__' :
86
+ main ()
0 commit comments