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

realtime image subscriber

asked 2019-10-02 03:35:46 -0500

mingcheng gravatar image

updated 2019-10-02 05:02:33 -0500

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()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-10-02 05:05:15 -0500

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.

edit flag offensive delete link more

Comments

Thank you very much for helping, the problem is solved. :D

mingcheng gravatar image mingcheng  ( 2019-10-03 02:17:18 -0500 )edit

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.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-10-03 03:58:12 -0500 )edit

ok, done thank you~

mingcheng gravatar image mingcheng  ( 2019-10-03 04:16:01 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-10-02 03:35:46 -0500

Seen: 927 times

Last updated: Oct 02 '19