My program generates a series of windows using the following code:
def display(img, name, fun):
global clicked
cv.NamedWindow(name, 1)
cv.ShowImage(name, img)
cv.SetMouseCallback(name, fun, img)
while cv.WaitKey(33) == -1:
if clicked == 1:
clicked = 0
cv.ShowImage(name, img)
cv.DestroyWindow(name)
I press "q" within the gui window to close it. However, the code continues to the next call of the display function and displays a second gui window while not closing the first. I'm using a Mac with OpenCV 2.1, running the program in Terminal. How can I close the gui windows? Thanks.
This worked for me in spyder :
Remember use only
cv.waitKey(positive Integer)
for this to workIt seems that none of the above solutions worked for me if I run it on Jupyter Notebook (the window hangs when closing and you need to force quit Python to close the window).
I am on macOS High Sierra 10.13.4, Python 3.6.5, OpenCV 3.4.1.
The below code works if you run it as a .py file (source: https://www.learnopencv.com/read-write-and-display-a-video-using-opencv-cpp-python/). It opens the camera, records the video, closes the window successfully upon pressing 'q', and saves the video in .avi format.
Here is what worked for me:
Fiddling around with this issue in the python console I observed the following behavior:
cv2.imshow
aftercv2.destroyWindow
sometimes closes the window. Albeit the old window pops up again with the nexthighgui
call, e.g.,cv2.namedWindow
cv2.waitKey
aftercv2.destroyWindow
closed the window every time I tried. Additionally the closed window remained closed, even when usingcv2.namedWindow
afterwardsHope this helps somebody.
(I used Ubuntu 12.10 with python 2.7.3 but OpenCV 2.4.2 from the 13.04 repos)
If you are using Spyder ( Anaconda Package ) there is the problem.
None of the solutions worked for me. I discovered that the problem wasn't the functions, but a problem on Spyder really. Try to use a texteditor plus running on terminal and you be fine using simply:
I solved the problem by calling
cv2.waitKey(1)
in a for loop, I don't know why it worked but gets my job done, so I didn't bother myself further.you are welcome to explain.