Reputation: 1
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')
Upvotes: 0
Views: 134
Reputation: 4843
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()
Upvotes: 1