使用rospy.Subscriber获取具有延迟的连续图像。

huangapple go评论106阅读模式
英文:

How to get consecutive images with a delay between using rospy.Subscriber

问题

I am trying to read two consecutive images from my rostopic using rospy.Subscriber to compare the orientation between these pictures but using the code below, I found out, two images I get are the same every time, so I am trying to find out if there is a way to get the two images with delay, more specifically, getting the second image 3 seconds after I get the first image. I tried putting time.sleep() between functions but it wouldn't work out.

Thanks for the help.
Aviationman

  1. import cv2
  2. import numpy as np
  3. import rospy
  4. import time
  5. import os
  6. from sensor_msgs.msg import Image
  7. from cv_bridge import CvBridge, CvBridgeError
  8. def cam_callback(msg):
  9. path = '/home/furki/img'
  10. try:
  11. # Converting ROS Image message to OpenCV2
  12. cv2_img1 = bridge1.imgmsg_to_cv2(msg, "bgr8")
  13. except CvBridgeError as e:
  14. print(e)
  15. else:
  16. # Save your OpenCV2 image as a jpeg
  17. img1 = cv2.imwrite(os.path.join(path, 'img1.jpeg'), cv2_img1)
  18. def cam_callback2(msg):
  19. path = '/home/furki/img'
  20. try:
  21. # Converting ROS Image message to OpenCV2
  22. cv2_img2 = bridge.imgmsg_to_cv2(msg, "bgr8")
  23. except CvBridgeError as e:
  24. print(e)
  25. else:
  26. # Save your OpenCV2 image as a jpeg
  27. img2 = cv2.imwrite(os.path.join(path, 'img2.jpeg'), cv2_img2)
  28. rospy.init_node("orientation_feature_detection_py")
  29. image_topic = '/s500/usb_cam/image_raw'
  30. rate = rospy.Rate(1)
  31. rospy.Subscriber(image_topic, Image, cam_callback)
  32. rate.sleep()
  33. rospy.Subscriber(image_topic, Image, cam_callback2)
  34. rate.sleep()
  35. # Reading the two images
  36. img1 = cv2.imread('/home/furki/img/img1.jpeg')
  37. img2 = cv2.imread('/home/furki/img/img2.jpeg')
英文:

I am trying to read two consecutive images from my rostopic using rospy.Subscriber to compare the orientation between these pictures but using the code below, I found out, two images I get are the same every time, so I am trying to find out if there is a way to get the two images with delay, more specifically, getting the second image 3 seconds after I get the first image. I tried putting time.sleep() between functions but it wouldn't work out.

Thanks for the help.
Aviationman

  1. import cv2
  2. import numpy as np
  3. import rospy
  4. import time
  5. import os
  6. from sensor_msgs.msg import Image
  7. from cv_bridge import CvBridge, CvBridgeError
  8. def cam_callback(msg):
  9. path = '/home/furki/img'
  10. try:
  11. # Converting ROS Image message to OpenCV2
  12. cv2_img1 = bridge1.imgmsg_to_cv2(msg,"bgr8")
  13. except CvBridgeError as e:
  14. print(e)
  15. else:
  16. # Save your OpenCV2 image as a jpeg
  17. img1 = cv2.imwrite(os.path.join(path, f'img1.jpeg'), cv2_img1)
  18. def cam_callback2(msg):
  19. path = '/home/furki/img'
  20. try:
  21. # Converting ROS Image message to OpenCV2
  22. cv2_img2 = bridge.imgmsg_to_cv2(msg,"bgr8")
  23. except CvBridgeError as e:
  24. print(e)
  25. else:
  26. # Save your OpenCV2 image as a jpeg
  27. img2 = cv2.imwrite(os.path.join(path, f'img2.jpeg'), cv2_img2)
  28. rospy.init_node("orientation_feature_detection_py")
  29. image_topic = '/s500/usb_cam/image_raw'
  30. rate = rospy.Rate(1)
  31. rospy.Subscriber(image_topic, Image, cam_callback)
  32. rate.sleep()
  33. rospy.Subscriber(image_topic, Image, cam_callback2)
  34. rate.sleep()
  35. # Reading the two images
  36. img1 = cv2.imread('/home/furki/img/img1.jpeg')
  37. img2 = cv2.imread('/home/furki/img/img2.jpeg')

答案1

得分: 1

以下是翻译好的内容:

制作两个订阅者在这里实际上没有任何作用。它们将在每次主题接收到新消息时被调用。如果您想限制处理图像的频率,您应该缓存图像并使用运行循环:

  1. latest_msg = None
  2. def cam_callback(self, msg):
  3. global latest_msg
  4. latest_msg = msg.data
  5. if __name__ == '__main__':
  6. rospy.init_node('some_node')
  7. sub = rospy.Subscriber('/img_topic', Image, cam_callback)
  8. rate = rospy.Rate(1/3)
  9. while not rospy.is_shutdown():
  10. # 在这里处理图像
  11. rate.sleep()
英文:

Making two subscribers doesn't really do anything here. They will be called every time a new message is received on the topic. If you want to limit how often you're processing images you should cache the image and use a run loop:

  1. latest_msg = None
  2. def cam_callback(self, msg):
  3. global latest_msg
  4. latest_msg = msg.data
  5. if __name__ == '__main__':
  6. rospy.init_node('some_node')
  7. sub = rospy.Subscriber('/img_topic', Image, cam_callback)
  8. rate = rospy.Rate(1/3)
  9. while not rospy.is_shutdown():
  10. #process image here
  11. rate.sleep()

huangapple
  • 本文由 发表于 2023年5月7日 18:00:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定