ros中同时订阅两个topic(2张图像)合并成一个topic(1张图像)

2019-12-06 15:42:39

先暂时做个资料保存

要同时用两个红外相机,但是没有做硬件上的 时间戳同步,就是笔记本上同时插着两个相机。

两个topic发布各自相机的图像,然后要有个节点同时订阅两个topic并把两张图像拼接成一张图像再做处理。

直接搜 ros sub two topic结果都是2个callback函数,各自处理各自的topic,和想要的不太一样。

自己融合两个线程中时间戳不那么严格一致的数据太麻烦了

想要的是两张图片的时间戳如果相近,就当做是在同一时刻采集的,然后合成一张图像或者一个topic。

然后发现了下面的链接:

https://stackoverflow.com/questions/55458218/is-it-possible-to-time-synchronize-two-topics-in-ros-of-same-message-type 

https://www.cnblogs.com/gdut-gordon/p/10293446.html  

http://wiki.ros.org/message_filters#Example_.28Python.29-1  

https://blog.csdn.net/chishuideyu/article/details/77479758  

https://github.com/DiaboloKiat/sis_lab_all/blob/de74759c3779aa32916a5c61dbc735f8d17c909c/03-ROS_tutorial_1/catkin_ws/src/tutorial/src/test_my_message.py

应该是一个 message_filters ,使用其  ApproximateTime Policy  ,然后就只有一个callback了 

 

https://stackoverflow.com/questions/48830056/use-data-from-multiple-topics-in-ros-python 

这个看起来也有点用,先保存  

2019-12-06 16:42:49 

下面是部分代码,直接改的上面的链接里的代码,384*288 的两张图像

 1 def callback(left_data, right_data):
 2     temper_left  = bridge1.imgmsg_to_cv2(left_data, "mono16")
 3     temper_right = bridge2.imgmsg_to_cv2(right_data, "mono16")
 4 
 5 
 6     disp1 = cv2.normalize(temper_left, None, 0, 255, cv2.NORM_MINMAX)
 7     disp1 = disp1.astype(np.uint8)
 8     disp_color1 = cv2.applyColorMap(disp1, color_map_choice )
 9 
10 
11     disp2 = cv2.normalize(temper_right, None, 0, 255, cv2.NORM_MINMAX)
12     disp2 = disp2.astype(np.uint8)
13     disp_color2 = cv2.applyColorMap(disp2, color_map_choice )
14 
15     disp = np.concatenate((disp_color1, disp_color2), axis=1 )
16 
17     cv2.imshow("Image window", disp)
18     cv2.waitKey( 10 )
19 
20 
21 
22 def gotdesired():
23     rospy.init_node('image_converter', anonymous=True)
24 
25 
26     sub_left  = message_filters.Subscriber("left_infra", Image,  queue_size=1, buff_size=110592*6 )
27     sub_right = message_filters.Subscriber("right_infra", Image, queue_size=1, buff_size=110592*6 )
28 
29     ts = message_filters.ApproximateTimeSynchronizer([sub_left, sub_right], 10, 0.1, allow_headerless = True)
30     ts.registerCallback(callback)
31 
32     # spin() simply keeps python from exiting until this node is stopped
33     rospy.spin()
原文地址:https://www.cnblogs.com/shepherd2015/p/11995769.html