Ask Your Question

noctuelles's profile - activity

2020-03-19 12:03:23 -0600 received badge  Famous Question (source)
2019-03-09 17:56:47 -0600 received badge  Notable Question (source)
2018-12-05 19:33:38 -0600 received badge  Popular Question (source)
2017-04-24 11:03:10 -0600 commented question I cannot exit from "while loop" while showing images (python 2.7.13/OpenCV 3.2.0)

@pklab : It does not work... the only way to close the window (not in fullscreen mode) is using both commands: cv2.destroyAllWindows() and cv2.waitKey(1). But again, in fullscreen mode, it does not work...

2017-04-24 03:58:54 -0600 commented question I cannot exit from "while loop" while showing images (python 2.7.13/OpenCV 3.2.0)

@pklab : Thank you for your comment. I had to add cv2.waitKey(1) since it seems to be a well-known problem on Mac. Without this command cv2.destroyAllWindows() does not work (see e.g. here )

2017-04-22 05:14:21 -0600 commented question I cannot exit from "while loop" while showing images (python 2.7.13/OpenCV 3.2.0)

@LBerger : the lk_track.py script works smoothly. The problem appears when I add the two lines in order to show the images fullscreen. I do not want to show the upper bar, just to see the images, but for some reason I cannot close the window and end the script.

2017-04-22 05:12:31 -0600 commented question I cannot exit from "while loop" while showing images (python 2.7.13/OpenCV 3.2.0)

@berak : I see, thank you for your efforts anyway!

2017-04-22 03:33:37 -0600 commented question I cannot exit from "while loop" while showing images (python 2.7.13/OpenCV 3.2.0)

Thank you, berak, but I still have the same problem if I add the two lines to run the script for fullscreen mode. Pressing the ESC key, the image freezes, my mac is like frozen and the keyboard "beeps" for any pressed key and I can only reebot the mac.

Does anyone have the same problem running the above script (for fullscreen mode) on his/her computer? Thanks in advance.

2017-04-21 15:43:19 -0600 commented question I cannot exit from "while loop" while showing images (python 2.7.13/OpenCV 3.2.0)

Dear LBerger, thank you very much for your suggestion. I edited my question, because I still have a problem when running the code to show the images in fullscreen mode.

2017-04-21 14:58:49 -0600 asked a question I cannot exit from "while loop" while showing images (python 2.7.13/OpenCV 3.2.0)

Dear all, I have a MacBook Air Sierra 10.12.4, Python 2.7.13 installed with Homebrew and OpenCV 3.2.0. I would like to show two superposed images using the command cv2.addWeighted and random.uniform() in a while loop. I wrote the following code:

import random
import cv2

img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')

cv2.startWindowThread()
cv2.imshow('test',cv2.resize(img1,(1440,900)))

#1440,900 is the resolution of my laptop

while True :

    alpha = random.uniform(0,1)
    wei = cv2.addWeighted(img2,alpha,img1,1-alpha,0)
    cv2.imshow('test',cv2.resize(wei,(1440,900)))
    cv2.waitKey(100)

My problem is how to exit from the loop e.g. hitting the Esc key. I tried by including in the while loop

key=cv2.waitKey(0)
    if (key==27):
        cv2.destroyAllWindows()
        for i in range (1,5):
        cv2.waitKey(1)

(I found on the web that you need five times "cv2.waitKey(1)" to destroy effectively the window), but it does not work. The only way to interrupt the code is by clicking on the terminal window and hitting Ctrl+c.

To be more precise, I would like to show these two alternating images in fullscreen mode. This is obtained by adding the following two lines after the "cv2.startWindowThread()" command:

cv2.namedWindow('test',cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('test',cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

The further problem in this case is that I cannot interrupt the script, because I cannot see and select the terminal window: keyboard is frozen as I can see only the mouse pointer but without being able to click.

The only way to stop everything... is rebooting my mac, so I am doing something terribly wrong, of course.

I hope to have been clear and thank you in advance for any suggestions.

EDIT

Thanks to LBerger suggestion, I put the while loop inside a function so, the new version of the code is:

import random
import cv2

img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')

cv2.startWindowThread()
# cv2.namedWindow('test',cv2.WND_PROP_FULLSCREEN)
# cv2.setWindowProperty('test',cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN) 
cv2.imshow('test',cv2.resize(img1,(1440,900)))

#1440,900 is the resolution of my laptop

def main() :
    while True :

        alpha = random.uniform(0,1)
        wei = cv2.addWeighted(img2,alpha,img1,1-alpha,0)
        cv2.imshow('test',cv2.resize(wei,(1440,900)))
        ch = cv2.waitKey(1)
        if ch == 27:
            break
        cv2.waitKey(100)

if __name__ == '__main__':
    main()

cv2.destroyAllWindows()
cv2.waitKey(1)

But I still have a problem. If I add the two commented lines after "cv2.startWindowThread()" to display the images fullscreen, when I press the ESC key, the two images stop alternating, so I think we are out of the while loop, but the fullscreen window does not close! Everything is frozen again, the keyboard "beeps" when pressing any key... and I have to reboot the computer. Does anyone have the same problem running the above script (uncommenting the two lines for fullscreen mode? Thanks in advance.