user8400839
user8400839

Reputation: 105

Python, OpenCV can't close last window

I am using the Python 2.7.12 interpreter and OpenCV 3.0.0.

It is not able for me to close all windows of OpenCV. If I close the windows one by one (by "cv2.destroyWindow()"), the last window doesn't disapear from screen. Also "cv2.destroyAllWindows()" dosn't work.

I have tried with "cv2.startWindowThread()" at the begin and some waitKeys. But both failed. Here is my code:

import time, cv2
print(cv2.__version__)

cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
#cv2.startWindowThread()
cv2.waitKey(10)

while True:
    val1, img1 = cap1.read()
    val2, img2 = cap2.read()
    cv2.namedWindow("Camera 1", cv2.WINDOW_NORMAL)
    cv2.namedWindow("Camera 2", cv2.WINDOW_NORMAL)
    cv2.imshow("Camera 1", img1)
    cv2.imshow("Camera 2", img2)

    cv2.resizeWindow("Camera 1", 400, 400)
    cv2.resizeWindow("Camera 2", 400, 400)
    cv2.moveWindow("Camera 1" , 0, 0)
    cv2.moveWindow("Camera 2", 400, 0)

    if cv2.waitKey(50) & 0xFF == ord('q'):
        break

cap1.release()
cap2.release()
cv2.waitKey(10)
cv2.destroyWindow("Camera 1")
cv2.waitKey(10)
cv2.destroyWindow("Camera 2")
cv2.waitKey(10)
#cv2.destroyAllWindows()

time.sleep(1)
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
print("finish")

Do you know how to close the OpenCV windows? Can I terminate the window thread of them?

I have to use this Version of OpenCV because I got in truble with newer versions of OpenCV and multithreading.

Upvotes: 0

Views: 2797

Answers (1)

Hao Li
Hao Li

Reputation: 1762

Try to add some cv2.waitKey after destroyAllWindows:

cv2.destroyAllWindows()
for i in range(5):    # maybe 5 or more
    cv2.waitKey(1)

Sometimes, it works.

Upvotes: 0

Related Questions