ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Multiple Camera Sub

asked 2022-08-03 21:30:52 -0500

irfanrah gravatar image

updated 2022-08-04 19:33:59 -0500

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 !

edit retag flag offensive close merge delete

Comments

It is advised to keep your code in your question. That way, a reader doesn't need to look at an external website.

ravijoshi gravatar image ravijoshi  ( 2022-08-03 22:09:25 -0500 )edit

Thanks ! I am new in this forum

irfanrah gravatar image irfanrah  ( 2022-08-04 19:34:17 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-08-03 22:15:54 -0500

ravijoshi gravatar image

updated 2022-08-03 22:21:50 -0500

Looking at your code, it seems you want to subscribe to RGB and depth images. Please use the message_filters. A sample code taken from the wiki page is shown below:

import message_filters
from sensor_msgs.msg import Image

def callback(rgb_image, depth_image):
  # Solve all of perception here...

image_sub = message_filters.Subscriber('rgb_topic_here', Image)
depth_sub = message_filters.Subscriber('depth_topic_here', Image)

ts = message_filters.TimeSynchronizer([image_sub, depth_sub], 10)
ts.registerCallback(callback)
rospy.spin()

I recommend checking the message_filters to apply additional filters such as Time Synchronizer etc.

edit flag offensive delete link more

Comments

Hi, Thanks for your answer and suggestion. But I think I ask how to utilize ROS code with Python OOP Class, so it will make the code more clean.

As you can see in the part

camera_1 = CameraSub('camera1/color/image_raw', 'camera1/depth/image_raw')
rgb_img_c1, d_img_c1 = camera_1.RGBD_output()

I want to make the code work, and we can get the rgb and depth camera with this setting.

So, if we want to sub other camera topic we just need to create some other function like this

camera_10 = CameraSub('camera10/color/image_raw, 'camera10/depth/image_raw')
rgb_img_c10, d_img_c10 = camera_10.RGBD_output()
irfanrah gravatar image irfanrah  ( 2022-08-04 19:36:20 -0500 )edit
1

That's how it is done. Please see a similar answer here. However, instead of defining two subscribers and corresponding callbacks, you may use message_filters. On a side note, self.loop_rate is an unused variable.

ravijoshi gravatar image ravijoshi  ( 2022-08-04 21:00:03 -0500 )edit

Thank you, Really appreciate your answer !

irfanrah gravatar image irfanrah  ( 2022-08-08 20:14:26 -0500 )edit

You are welcome. It is advised to click on the "accept" button to mark the question as solved.

ravijoshi gravatar image ravijoshi  ( 2022-08-08 21:26:59 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-08-03 21:30:52 -0500

Seen: 54 times

Last updated: Aug 04 '22