Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, the second one would destroy the first one. But looks like you want to have both the information pieces accessible and process them at the same time. If that's the case, you need to use TimeSynchronizer from message_filters package. It allows you to have a single callback function for various topics. In your case, it would be:

#!/usr/bin/env python
import sys
import message_filters ### ADD THIS
import rospy
import numpy as np
import math
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2

#------Start of Class
class image_converter:
    def __init__(self):
        self.bridge = CvBridge()
        self.image_rgb_sub = message_filters.Subscriber("/kinect/rgb",Image)      #Case 1
        self.image_depth_sub = message_filters.Subscriber("/kinect/depth",Image)  #Case 2

    def callback(self, rgb,depth):
        cv_image_rgb = self.bridge.imgmsg_to_cv2(rgb, "bgr8")
        cv2.imshow("RGB_Sub", cv_image_rgb)
        cv_image_depth = self.bridge.imgmsg_to_cv2(depth, "bgr8")
        cv2.imshow("Depth_Sub", cv_image_depth)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            rospy.signal_shutdown('Quit')
            cv2.destroyAllWindows()

def main():
    img_cvt = image_converter()
    rospy.init_node('image_converter', anonymous=True)
    try:
        ts = message_filters.TimeSynchronizer([img_cvt.image_rgb_sub,img_cvt.image_depth_sub],10)
        ts.registerCallback(callback)
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")

if __name__ == '__main__':
    main()

NOTE:

TimeSynchronizer checks for timestamps in the header of the messages and pools them. So, if there is a huge lag somewhere in one topic, the rgb and depth information might need some error handling.

Yes, the second one would destroy the first one. But looks like you want to have both the information pieces accessible and process them at the same time. If that's the case, you need to use TimeSynchronizer from message_filters package. It allows you to have a single callback function for various topics. In your case, it would be:

#!/usr/bin/env python
import sys
import message_filters ### ADD THIS
import rospy
import numpy as np
import math
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2

#------Start of Class
class image_converter:
    def __init__(self):
        self.bridge = CvBridge()
        self.image_rgb_sub = message_filters.Subscriber("/kinect/rgb",Image)      #Case 1
        self.image_depth_sub = message_filters.Subscriber("/kinect/depth",Image)  #Case 2

    def callback(self, rgb,depth):
        cv_image_rgb = self.bridge.imgmsg_to_cv2(rgb, "bgr8")
        cv2.imshow("RGB_Sub", cv_image_rgb)
        cv_image_depth = self.bridge.imgmsg_to_cv2(depth, "bgr8")
        cv2.imshow("Depth_Sub", cv_image_depth)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            rospy.signal_shutdown('Quit')
            cv2.destroyAllWindows()

def main():
    img_cvt = image_converter()
    rospy.init_node('image_converter', anonymous=True)
    try:
        ts = message_filters.TimeSynchronizer([img_cvt.image_rgb_sub,img_cvt.image_depth_sub],10)
        ts.registerCallback(callback)
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")

if __name__ == '__main__':
    main()

NOTE:

  1. TimeSynchronizer checks for timestamps in the header of the messages and pools them. So, if there is a huge lag somewhere in one topic, the rgb and depth information might need some error handling.

  2. The error doesn't seem to be related to this issue.

Peace!

Yes, the second one would destroy the first one. But looks like you want to have both the information pieces accessible and process them at the same time. If that's the case, you need to use TimeSynchronizer from message_filters package. It allows you to have a single callback function for various topics. In your case, it would be:

#!/usr/bin/env python
import sys
import message_filters ### ADD THIS
import rospy
import numpy as np
import math
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2

#------Start of Class
class image_converter:
    def __init__(self):
        self.bridge = CvBridge()
        self.image_rgb_sub = message_filters.Subscriber("/kinect/rgb",Image)      #Case 1
        self.image_depth_sub = message_filters.Subscriber("/kinect/depth",Image)  #Case 2

    def callback(self, rgb,depth):
        cv_image_rgb = self.bridge.imgmsg_to_cv2(rgb, "bgr8")
        cv2.imshow("RGB_Sub", cv_image_rgb)
        cv_image_depth = self.bridge.imgmsg_to_cv2(depth, "bgr8")
        cv2.imshow("Depth_Sub", cv_image_depth)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            rospy.signal_shutdown('Quit')
            cv2.destroyAllWindows()

def main():
    img_cvt = image_converter()
    rospy.init_node('image_converter', anonymous=True)
    try:
        ts = message_filters.TimeSynchronizer([img_cvt.image_rgb_sub,img_cvt.image_depth_sub],10)
message_filters.ApproximateTimeSynchronizer([img_cvt.image_rgb_sub,img_cvt.image_depth_sub],10,0.1)
        ts.registerCallback(callback)
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")

if __name__ == '__main__':
    main()

NOTE:More info can be found on this documentation.

  1. TimeSynchronizer checks for timestamps in the header of the messages and pools them. So, if there is a huge lag somewhere in one topic, the rgb and depth information might need some error handling.

  2. The error doesn't seem to be related to this issue.

Peace!