DestroyWindow does not close window on Mac using Python and OpenCV

后端 未结 12 1019
野趣味
野趣味 2020-11-27 05:36

My program generates a series of windows using the following code:

def display(img, name, fun):
    global clicked

    cv.NamedWindow(name, 1)
    cv.ShowIm         


        
相关标签:
12条回答
  • 2020-11-27 05:48

    This solution works for me (under Ubuntu 12.04 with python open in the shell):

    Re-invoke cv.ShowImage after the window is 'destroyed'.

    0 讨论(0)
  • 2020-11-27 05:50

    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.

    0 讨论(0)
  • 2020-11-27 05:53

    Fiddling around with this issue in the python console I observed the following behavior:

    • issuing a 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
    • the third call of cv2.waitKey after cv2.destroyWindow closed the window every time I tried. Additionally the closed window remained closed, even when using cv2.namedWindow afterwards

    Hope this helps somebody.

    (I used Ubuntu 12.10 with python 2.7.3 but OpenCV 2.4.2 from the 13.04 repos)

    0 讨论(0)
  • 2020-11-27 05:55

    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.

    0 讨论(0)
  • 2020-11-27 05:56

    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()
    
    0 讨论(0)
  • 2020-11-27 05:57

    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

    0 讨论(0)
提交回复
热议问题