1

I have a research project, using 2 Basler cameras (both a2A1920-16ucPRO) to create a disparity map. I extract the images via ROS. For this I have subscribed to 2 Topics, where I regularly get images at 2 hertz. The problem is that these 2 images have a time difference of 5-10 milliseconds, but I need to get synchronously the 2 images. The cameras are connected to my PC via USB. Is there any way to set something in ROS that I get 2 images in the same time? Or is there another solution? Here is my code:

import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2
import time
import message_filters
Instantiate CvBridge

bridge = CvBridge()

def callback1(b1, b2):
    print("Aufruf beide Kameras!")
    try:
   
       cv2_img1 = bridge.imgmsg_to_cv2(b1, "bgr8")
       cv2_img2 = bridge.imgmsg_to_cv2(b2, "bgr8")

    except CvBridgeError, e:
           print(e)
    else:
  
       img_gray_left=cv2.cvtColor(cv2_img1, cv2.COLOR_BGR2GRAY) 
       img_gray_right=cv2.cvtColor(cv2_img2, cv2.COLOR_BGR2GRAY)
       dimension=(1024,440)
       resized1=cv2.resize(img_gray_left, dimension, interpolation=cv2.INTER_AREA)
       resized2=cv2.resize(img_gray_right, dimension, interpolation=cv2.INTER_AREA)
       cv2.imwrite('multilayer-stixel-world/build/myimages_ros/camera_image_left.pgm', resized1)
       cv2.imwrite('multilayer-stixel-world/build/myimages_ros/camera_image_right.pgm', resized2)
    



def main():
    rospy.init_node('image_listener', anonymous=True)
    image_topic1 = message_filters.Subscriber('/ace_RGB_Left/pylon_camera_node/image_raw', Image)
    image_topic2 = message_filters.Subscriber('/ace_RGB_Right/pylon_camera_node/image_raw',Image)
    ts = message_filters.ApproximateTimeSynchronizer([image_topic1, image_topic2],2, 0.005)
    ts.registerCallback(callback1)
    rospy.spin()

if _name_ == '_main_':
    main()

The message function "TimeSynchronizer" dont work, because the timestamps do not fit perfectly. So i use the function "ApproximateTimeSynchronizer" with a tolerance of 5 milliseconds. But that is not enough.

Benli
  • 21
  • 1
  • Because this is Python you will not be able to get both images at the exact same time; this would be true in any language really. Why do you need the delta to be less than < 5ms? – BTables Dec 06 '21 at 20:38
  • I selected delta smaller than 5 because I wanted to see how many messages would get through. I found out that about one message gets through for every two messages. So there is no way via Python to get these 2 messages synchronized? – Benli Dec 06 '21 at 21:27
  • I think it’s still a question of why you want them perfectly synchronized. The time msg time stamps will be produced when the actual image hits ROS. There is also no way to completely guarantee the time stamps are 100% synchronized in either Python or c++. That being said, depending on your application there might be another valid approach. – BTables Dec 06 '21 at 23:31
  • I need the images synchronous, because I create a disparity image from these two in the car while driving. And if you drive, for example, at 100 km/h, there is a difference of about 13 centimeters and that distorts the disparity image. – Benli Dec 07 '21 at 08:53

0 Answers0