When I am running this piece of code on ipython (MacOS /python 2.7.13)
cv2.startWindowThread()
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()
the kernel crashes. When the image appears, the only button that I can press is minimise
(the one in the middle and when I press any key then the spinning wheel shows up and the only thing I can do is force quit.
P.S. I have downloaded the latest python version through home-brew
.
cv2. imshow() method is used to display an image in a window. The window automatically fits to the image size.
waitKey() This function is very important, without this function cv2. imshow() won't work properly. Thus if the wait time is entered as 6000, the picture will be displayed for 6s and then get closed (provided you have cv2.
destroyAllWindows(). Inside the cv2. waitKey() function, you can provide any value to close the image and continue with further lines of code.
Do you just want to look at the image? I'm not sure what you want to do with startWindowThread, but if you want to install opencv the easiest way, open the image, and view it try this:
install conda (A better package manager for opencv than homebrew)
then create a cv environment:
conda create -n cv
activate it and install opencv from menpo's channel
source activate cv
conda install -c menpo opencv
then in python (hit q
to exit):
import cv2
cv2.namedWindow('imageWindow')
img = cv2.imread('path/to/your/image.png')
cv2.imshow('imageWindow',img)
wait = True
while wait:
wait = cv2.waitKey()=='q113' # hit q to exit
I have reproduced the jupyter kernel crash problem. The following is the test environment setup.
- macOS 10.12.16
- python 2.7.11
- opencv 4.0.0
- ipython 5.8.0
- jupyter notebook server 5.7.4
With the change on cv2.waitKey()
to waiting for a Q press, the problem goes away.
Here is the code:
import cv2
img = cv2.imread('sample.jpg')
cv2.startWindowThread()
cv2.imshow('img', img)
# wait forever, if Q is pressed then close cv image window
if cv2.waitKey(0) & 0xFF == ord('q'):
cv2.destroyAllWindows()
Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With