OpenCV "namedwindow" goes grey, is not getting events
I am experimenting with opencv and ROS and Gazebo. This is a variant of an example I found in the Programming Robots with ROS book. I am running on Ubuntu, with Kinetic.
I run the program below, expecting that the window containing the image from the camera on the robot is displayed. It does do this, but after about 10 seconds, the window greys out. I looked at all the logs I can find and see no smoking gun. I have also used rqt_image_view and it does show the image as expected.
Apparently (thanks @gvdhoorn) the greying is because event handling for the graphical window is stalled. To debug this I've added "print n" lines all over the place. The prints show me I think that the loop in the main program "while(not rospy.is_shutdown()):" is not getting a "turn" after a short while, whereas the messages that are coming in via the subscription are getting processed. It is almost as if they are coming so fast that the main program doesn't get a "turn" and so doesn't display the window over and over again. I thought with the "is_shutdown" the processing of the main and the callback would play nice together. Or maybe the problem is something totally different.
Any idea?
import rospy
import cv2
import sys
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
# Follower callback
class Follower:
def __init__(self):
self.cv_img = None
# Open CV Bridge, and create a window
self.bridge = CvBridge()
# subscribe to the topic, and give each image to the callback
self.image_sub = rospy.Subscriber('camera/rgb/image_raw', Image, self.image_callback)
# Called once per image received
def image_callback(self, frame):
# Convert image to cv2 and display it
try:
print 21
self.cv_img = self.bridge.imgmsg_to_cv2(frame, desired_encoding="bgr8")
print 23
except:
print("Unexpected error:", sys.exc_info()[0])
raise
def main():
cv2.namedWindow('Original')
print 30
follower = Follower()
rospy.init_node('follower', anonymous=True)
while(not rospy.is_shutdown()):
print 35
if follower.cv_img is not None:
print 37
cv2.imshow('Original', follower.cv_img)
print 39
key = cv2.waitKey(3) & 0xFF
if key == 27:
break
print 43
rospy.spin()
if __name__ == '__main__':
main()
A grey window does not mean the "process died", it just means that input even handling for that window is (temporarily) stalled. That could be because the process that spawned it has gone, or it could be because of something else.
It's also an Ubuntu UI thing.