My program generates a series of windows using the following code:
def display(img, name, fun):
global clicked
cv.NamedWindow(name, 1)
cv.ShowIm
This solution works for me (under Ubuntu 12.04 with python open in the shell):
Re-invoke cv.ShowImage
after the window is 'destroyed'.
There are a few peculiarities with the GUI in OpenCV. The destroyImage
call fails to close a window (atleast under Linux, where the default backend was Gtk+ until 2.1.0) unless waitKey
was called to pump the events. Adding a waitKey(1)
call right after destroyWindow
may work.
Even so, closing is not guaranteed; the the waitKey
function is only intercepted if a window has focus, and so if the window didn't have focus at the time you invoked destroyWindow
, chances are it'll stay visible till the next destroyWindow
call.
I'm assuming this is a behaviour that stems from Gtk+; the function didn't give me any trouble when I used it under Windows.
Fiddling around with this issue in the python console I observed the following behavior:
cv2.imshow
after cv2.destroyWindow
sometimes closes the window. Albeit the old window pops up again with the next highgui
call, e.g., cv2.namedWindow
cv2.waitKey
after cv2.destroyWindow
closed the window every time I tried. Additionally the closed window remained closed, even when using cv2.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)
After searching aroung for some time, none of the solutions provided worked for me so since there's a bug in this function and I did not have time to fix it, I did not have to use the cv2 window to show the frames. Once a few frames have been saved, you can open the file in a different viewer, like VLC or MoviePlayer ( for linux ).
Here's how i did mine.
import cv2
threadDie = True # change this to false elsewhere to stop getting the video
def getVideo(Message):
print Message
print "Opening url"
video = cv2.VideoCapture("rtsp://username:passwordp@IpAddress:554/axis-media/media.amp")
print "Opened url"
fourcc = cv2.cv.CV_FOURCC('X','V','I','D')
fps = 25.0 # or 30.0 for a better quality stream
writer = cv2.VideoWriter('out.avi', fourcc,fps, (640,480),1)
i = 0
print "Reading frames "
while threadDie:
ret, img = video.read()
print "frame number: ",i
i=i+1
writer.write(img)
del(video)
print "Finished capturing video"
Then open the file with a different viewer, prabably in a nother function, like if you like vlc, you can start it and pass the saved file as a parameter. On the terminal, i would do this
vlc out.avi #out.avi is my video file being saved by the function above.
This worked for me on arch linux.
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:
WINDOW_NAME = "win"
image = cv.imread("foto.jpg", 0)
cv.namedWindow(WINDOW_NAME, cv.CV_WINDOW_AUTOSIZE)
cv.startWindowThread()
cv.imshow(WINDOW_NAME, image)
cv.waitKey()
cv.destroyAllWindows()
This worked for me in spyder :
import cv2 as cv
cv.namedWindow("image")
img = cv.imread("image_name.jpg")
cv.imshow("image",img)
cv.waitKey(5000) # 5 sec delay before image window closes
cv.destroyWindow("image")
Remember use only cv.waitKey(positive Integer)
for this to work