Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Subscribing to multiple image_raw topics with OpenCV

Hi all,

As per the title, may I know how I could go about subscribing and then display two images from different ROS topics? I have two topics, /camera/color/image_raw and /camera/aligned_depth_to_color/image_raw from a Realsense node that I wish to process together.

This is a short snippet of the structure of my script.

class image_converter:

  def __init__(self):
    self.bridge = CvBridge()
    self.image_sub = message_filters.Subscriber("/camera/color/image_raw",Image)
    self.depth_sub = message_filters.Subscriber("/camera/aligned_depth_to_color/image_raw",Image)
    ts = message_filters.TimeSynchronizer([self.image_sub, self.depth_sub], 10)
    ts.registerCallback(self.callback)

  def callback(self,data):
    try:
      frame = self.bridge.imgmsg_to_cv2(self.image_sub, "bgr8")
    except CvBridgeError as e:
      print(e)

def main(args):
  ic = image_converter()
  rospy.init_node('image_converter', anonymous=True)
  try:
    rospy.spin()
  except KeyboardInterrupt:
    print("Shutting down")
  cv2.destroyAllWindows()

if __name__ == '__main__':
    main(sys.argv)

The above would yield an error

[ERROR] [1521687036.626594]: bad callback: <bound method Subscriber.callback of <message_filters.Subscriber object at 0x7f7efda8bdd0>>
...
...
TypeError: callback() takes exactly 2 arguments (3 given)