Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.imshow() crashes on Mac

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.

like image 753
thanasissdr Avatar asked Sep 21 '17 16:09

thanasissdr


People also ask

What is cv2 Imshow in Python?

cv2. imshow() method is used to display an image in a window. The window automatically fits to the image size.

Is cv2 waitKey necessary?

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.

How do I close an image after cv2 Imshow?

destroyAllWindows(). Inside the cv2. waitKey() function, you can provide any value to close the image and continue with further lines of code.


2 Answers

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
like image 131
pale bone Avatar answered Oct 10 '22 01:10

pale bone


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.

like image 20
thewaywewere Avatar answered Oct 10 '22 00:10

thewaywewere