0
$\begingroup$

Rosanswers logo

Hi, I wrote a subscriber to subscribe the image message send from the topic /gi/simulation/left/image_raw and convert it using CV.bridge(). However, the problem is that, this subscriber only shows a single frame of image when the subscriber starts. Is there a way that my subscriber can achieve the similar effect when I using rviz to subscribe to the same topic. In simple, how to achieve real time image capture by using the subscriber. The code for subscriber is shown below: Thank you for the help.

#!/usr/bin/env python
import rospy
import sys
import cv2
from cv_bridge import CvBridge
from sensor_msgs.msg import Image

def process_image(msg):
    bridge = CvBridge()
    img = bridge.imgmsg_to_cv2(msg, "bgr8")
    cv2.imshow("image",img)
    cv2.waitKey(0)
if __name__ == '__main__':
    while not rospy.is_shutdown():
        rospy.init_node('image_sub')
        rospy.loginfo('image_sub node started')
        rospy.Subscriber("/gi/simulation/left/image_raw", Image, process_image)
        
        rospy.spin()

Originally posted by mingcheng on ROS Answers with karma: 3 on 2019-10-02

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The problem is caused by cv2.waitKey(0) in you image callback. If you pass zero to waitKey it will pause until you press a key with the window focused, and block execution of your program.

If you change this :

cv2.waitKey(50)

This instead it will wait for 50 milliseconds or until you press a key and then continue execution, allowing your code to receive and display more images.


Originally posted by PeteBlackerThe3rd with karma: 9529 on 2019-10-02

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by mingcheng on 2019-10-03:
Thank you very much for helping, the problem is solved. :D

Comment by PeteBlackerThe3rd on 2019-10-03:
Great. Can you accept the answer by clicking on the tick icon. That way other users can easily see that your question has a solution.

Comment by mingcheng on 2019-10-03:
ok, done thank you~

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.