Multiple Camera Sub
Hi, I am trying to create a code for multiple camera subscriber, I usualy use global variable to subscriber the RGBD topic from camera, but I want to make cleaner code.
Here is my code:
class CameraSub:
def __init__(self, rgb_topic, d_topic):
self.rgb_img = None
self.d_img = None
self.rgb_topic = rgb_topic
self.d_topic = d_topic
self.bridge = cv_bridge.CvBridge()
self.loop_rate = rospy.Rate(10)
self.rgb_sub = rospy.Subscriber(self.rgb_topic,
Image, self.RGBimage_callback)
self.d_sub = rospy.Subscriber(self.d_topic,
Image, self.depthImage_callback)
def RGBimage_callback(self, img):
self.rgb_img = self.bridge.imgmsg_to_cv2(img, desired_encoding='bgr8')
def depthImage_callback(self, img_d):
self.d_img = self.bridge.imgmsg_to_cv2(img_d, )
self.d_img = np.array(self.d_img, dtype=np.float32)
self.d_img = cv2.normalize(self.d_img, self.d_img, 0, 255, cv2.NORM_MINMAX)
def RGBD_output(self):
if self.rgb_img is not None and self.d_img is not None:
return self.rgb_img, self.d_img
else:
return 0,0
rospy.init_node('dualCameraRGBD')
camera_1 = CameraSub('camera1/color/image_raw', 'camera1/depth/image_raw')
rgb_img_c1, d_img_c1 = camera_1.RGBD_output()
cv2.imshow("rgb_img_c1", rgb_img_c1)
cv2.imshow("d_img_c1", d_img_c1)
Thank you for the help !
It is advised to keep your code in your question. That way, a reader doesn't need to look at an external website.
Thanks ! I am new in this forum