Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.imshow() function is opening a window that always says not responding - python opencv

I am trying to run a very simple program. To open and jpg file and display it using the opencv library for python. Initially it all worked fine but now it just opens a window which doesn't show the image but says 'not responding'. I need to go to the task manager and close it!

from numpy import *
import matplotlib as plt
import cv2

img = cv2.imread('amandapeet.jpg')
print img.shape

cv2.imshow('Amanda', img)
like image 597
vicky Avatar asked Mar 08 '14 20:03

vicky


People also ask

How do I turn off cv2 Imshow?

destroyAllWindows(). Inside the cv2. waitKey() function, you can provide any value to close the image and continue with further lines of code.

What is cv2 Imshow ()?

cv2. imshow() method is used to display an image in a window. The window automatically fits to the image size. Syntax: cv2.imshow(window_name, image)

How do I import a cv2 Imshow in Python?

So open the command prompt or terminal as per the operating system you are using. In that type, “Python,” it will show you the python version you are using. Next, in that use command “pip install OpenCV-python,” it will install this for you. Along with that, it will also install you the NumPy library.

How do I show cv2 Imshow?

To display the image, we read with an image with an imread() function, and then we call the imshow() method of the cv2 module. The imshow() function will display the image in a window, and it receives as input the name of the window and the image.


2 Answers

You missed one more line:

cv2.waitKey(0)

Then the window shows the image until you press any key on keyboard. Or you can pass as following:

cv2.waitKey(1000)
cv2.destroyAllWindows()

Here, window shows image for 1000 ms, or 1 second. After that, the window would disappear itself. But in some cases, it won't. So you can forcefully destroy it using cv2.destroyAllWindows()

Please read more tutorials first : http://docs.opencv.org/trunk/doc/py_tutorials/py_tutorials.html

like image 134
Abid Rahman K Avatar answered Nov 01 '22 02:11

Abid Rahman K


None of the answers here worked in MacOS. The following works:

Just add a cv2.waitKey(1) after cv2.destroyAllWindows().

Example:

import cv2
image = cv2.imread('my_image.jpg')
cv2.imshow('HSV image', hsv_image); cv2.waitKey(0); cv2.destroyAllWindows(); cv2.waitKey(1)
like image 42
Julio Batista Silva Avatar answered Nov 01 '22 01:11

Julio Batista Silva