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

OpencvPython 2.7

Opencv Problem Overview


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)

Opencv Solutions


Solution 1 - Opencv

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

Solution 2 - Opencv

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)

Solution 3 - Opencv

The solution that worked for me: Switch from inline graphics to auto. It worked both in Spyder and in Jupyter notebooks.

  • To change Spyder setting: Go to Tools > Preferences > IPhyton console > Graphics > Backend: Automatic (Change backend from Inline to Automatic)

  • To change Notebook setting: Enter command:

    %matplotlib auto


Some background for my case (for those who may be quick to judge):

It used to work fine: I could open an image, it would load, and it would be responsive (doesn't say "Not responding", can close, focus, etc.) Then I installed some packages and ran some demo notebooks that apparently messed up some settings (Spyder open files were reset too).

I tried adding waitKey(1) (and 0, 30, 1000, etc values too). It made the image load, at least. But the image frame was "Not Responding": didn't refresh, couldn't close, didn't come to top, etc. Had to close using cv2.destroyAllWindows().

Note that everything worked fine during the duration of waitKey. I put this in a loop that shows the same image in the same named window and waits for a few seconds. During the loop everything works fine. As soon as the loop ends, the image window is "Not responding" (which looks like a GUI thread issue). I tried using cv2.startWindowThread(), and didn't make any difference.

Finally, changing from Inline graphics to Auto brought everything back to order.

Solution 4 - Opencv

I've been working with opencv 3.2 and matplotlib too recently and discovered (through trial and error of commenting out lines) that the import of pyplot from matplotlib has some sort of interference with the cv2.imshow() function. I'm not sure why or how it really works but in case anyone searches for this issue and comes across this old forum, this might help. I'm working to try to find a solution around this interference bu

Solution 5 - Opencv

I did also face the same issue. I am running through command line python prompt in centos 7 with the following code

>> import cv2, numpy as np
>> cap=cv2.VideoCapture(0)
>> img=cap.read()
>> cap.release()
>> cv2.imshow('image',img[1])
>> cv2.waitKey(0)
>> cv2.destroyAllWindows()
>> cv2.waitKey(1)

Even then the problem persisted and didn't solve. So I added

>> cv2.imshow('image',img[1])

Adding this did close the image window.Running the command again would create a new instance. Hope you can try if you still face any issues.

Solution 6 - Opencv

The cv2.imshow() function always takes two more functions to load and close the image. These two functions are cv2.waitKey() and cv2.destroyAllWindows(). Inside the cv2.waitKey() function, you can provide any value to close the image and continue with further lines of code.

    # First line will provide resizing ability to the window
    cv.namedWindow('Amanda', cv.WINDOW_AUTOSIZE)
    
    # Show the image, note that the name of the output window must be same
    cv.imshow('Amanda', img)
    
    # T0 load and hold the image
    cv.waitKey(0)
    
    # To close the window after the required kill value was provided
    cv.destroyAllWindows()
    

Hoping that you will get the image in a separate window now.

Solution 7 - Opencv

Nothing helped me on this thread, so I found a solution. Just use the matplotlib library to show the image:

import cv2
from matplotlib import pyplot as plt
#import matplotlib as mpl
#mpl.rcParams['toolbar'] = 'None' # if you want to remove toolbar from bottom
img = cv2.imread('path/to/image.jpg')

plt.figure('Your Title')
plt.imshow(img)
#plt.axis('off') # if you want to remove coordinates
plt.show()

UPD: I've installed opencv-contrib-python library instead of opencv-python and now cv2.imshow() function works as expected.

Solution 8 - Opencv

If you have used python notebooks then there is a problem in using cv2.waitKey(0) and cv2.destroyallwindows() in Unix based system to run a program of opencv.

I have an alternative method which would prevent from freezing your image

Steps: -Copy the code from python notebooks and create new filename.py and paste it

  • Open terminal
  • cd path/to/file
  • source activate VirtualEnvironment
  • python filename.py

This will run code directly from terminal. Hope this helps you. Example Link: https://youtu.be/8O-FW4Wm10s

Solution 9 - Opencv

I was having this same error until I added the below lines of code. For the waitKey, you can input figures above 0(i.e 1, 100 and above). It serves as the delay time for the window and it is in milliseconds.

----> cv2 waitKey(0) ----> cv2 destroyAllWindows()

Solution 10 - Opencv

I found that i had a breakpoint on the

cv2.waitkey()

funtion. removing that fixed the issue for me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvickyView Question on Stackoverflow
Solution 1 - OpencvAbid Rahman KView Answer on Stackoverflow
Solution 2 - OpencvJulio Batista SilvaView Answer on Stackoverflow
Solution 3 - OpencvKadir A. PekerView Answer on Stackoverflow
Solution 4 - OpencvMadelyn A.View Answer on Stackoverflow
Solution 5 - OpencvshivaView Answer on Stackoverflow
Solution 6 - OpencvRajkamal MishraView Answer on Stackoverflow
Solution 7 - OpencvdatmcView Answer on Stackoverflow
Solution 8 - OpencvR K BhalodiaView Answer on Stackoverflow
Solution 9 - OpencvTemitope OladokunView Answer on Stackoverflow
Solution 10 - OpencvZain NomanView Answer on Stackoverflow