英文:
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
import cv2
import numpy as np
import rospy
import time
import os
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
def cam_callback(msg):
path = '/home/furki/img'
try:
# Converting ROS Image message to OpenCV2
cv2_img1 = bridge1.imgmsg_to_cv2(msg, "bgr8")
except CvBridgeError as e:
print(e)
else:
# Save your OpenCV2 image as a jpeg
img1 = cv2.imwrite(os.path.join(path, 'img1.jpeg'), cv2_img1)
def cam_callback2(msg):
path = '/home/furki/img'
try:
# Converting ROS Image message to OpenCV2
cv2_img2 = bridge.imgmsg_to_cv2(msg, "bgr8")
except CvBridgeError as e:
print(e)
else:
# Save your OpenCV2 image as a jpeg
img2 = cv2.imwrite(os.path.join(path, 'img2.jpeg'), cv2_img2)
rospy.init_node("orientation_feature_detection_py")
image_topic = '/s500/usb_cam/image_raw'
rate = rospy.Rate(1)
rospy.Subscriber(image_topic, Image, cam_callback)
rate.sleep()
rospy.Subscriber(image_topic, Image, cam_callback2)
rate.sleep()
# Reading the two images
img1 = cv2.imread('/home/furki/img/img1.jpeg')
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
import cv2
import numpy as np
import rospy
import time
import os
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
def cam_callback(msg):
path = '/home/furki/img'
try:
# Converting ROS Image message to OpenCV2
cv2_img1 = bridge1.imgmsg_to_cv2(msg,"bgr8")
except CvBridgeError as e:
print(e)
else:
# Save your OpenCV2 image as a jpeg
img1 = cv2.imwrite(os.path.join(path, f'img1.jpeg'), cv2_img1)
def cam_callback2(msg):
path = '/home/furki/img'
try:
# Converting ROS Image message to OpenCV2
cv2_img2 = bridge.imgmsg_to_cv2(msg,"bgr8")
except CvBridgeError as e:
print(e)
else:
# Save your OpenCV2 image as a jpeg
img2 = cv2.imwrite(os.path.join(path, f'img2.jpeg'), cv2_img2)
rospy.init_node("orientation_feature_detection_py")
image_topic = '/s500/usb_cam/image_raw'
rate = rospy.Rate(1)
rospy.Subscriber(image_topic, Image, cam_callback)
rate.sleep()
rospy.Subscriber(image_topic, Image, cam_callback2)
rate.sleep()
# Reading the two images
img1 = cv2.imread('/home/furki/img/img1.jpeg')
img2 = cv2.imread('/home/furki/img/img2.jpeg')
答案1
得分: 1
以下是翻译好的内容:
制作两个订阅者在这里实际上没有任何作用。它们将在每次主题接收到新消息时被调用。如果您想限制处理图像的频率,您应该缓存图像并使用运行循环:
latest_msg = None
def cam_callback(self, msg):
global latest_msg
latest_msg = msg.data
if __name__ == '__main__':
rospy.init_node('some_node')
sub = rospy.Subscriber('/img_topic', Image, cam_callback)
rate = rospy.Rate(1/3)
while not rospy.is_shutdown():
# 在这里处理图像
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:
latest_msg = None
def cam_callback(self, msg):
global latest_msg
latest_msg = msg.data
if __name__ == '__main__':
rospy.init_node('some_node')
sub = rospy.Subscriber('/img_topic', Image, cam_callback)
rate = rospy.Rate(1/3)
while not rospy.is_shutdown():
#process image here
rate.sleep()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论