Python OpenCV namedWindow and imshow freeze
Hi there,
I try to view a subscribed image via cv2.imshow() like the following:
#!/usr/bin/env python
import sys, rospy, traceback, cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
window_name = 'detection'
class imShow():
def __init__(self):
self.bridge = CvBridge()
self.sub = rospy.Subscriber('/camera/rgb/image_raw', Image, self.callback)
def callback(self, data):
try:
cv_img = self.bridge.imgmsg_to_cv2(data, "bgr8")
except CvBridgeError as exc:
print('###CVBridgeError###')
print(traceback.format_exc())
print(exc)
cv2.imshow(window_name, cv_img)
cv2.waitKey(1)
def main(args):
cv2.namedWindow(window_name)
ims = imShow()
rospy.init_node('imshow', anonymous=True)
try:
rospy.spin()
except KeyboardInterrupt:
print('Shutting down...')
cv2.destroyAllWindows()
if __name__ == '__main__':
main(sys.argv)
But this way the namedWindows shows up at the first call and does'nt update any more and freezes.
Because I want to add some trackbars to adjust values, which will be processed in the callback function, I want to declare and initialize the window and the trackbars outside of the class. I had it working with doing it inside of the callback function, but after a few seconds the node crashed with the error "Process finished with exit code 139". I thought the reason for this may have been a memory overflow because the window and the trackbars were created again and again with each call of the callback function.
So does anybody have an idea how to handle this?
Thanks and regards.
You have some indentation issues in your try-except block, but maybe that is just a copy-paste error. Have you verified that an image is actually being published on the
/camera/rgb/image_raw
topic? If no images are published your callback will never run and you'll never see an image.