10

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.

1
  • OpenCV gui doesn't play well with jupyter/iPython. Why don't you use a local IDE like pyCharm? Commented Sep 22, 2017 at 3:29

5 Answers 5

6

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
0
4

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.

0
3

Had the same issue and no solutions I've found worked for me. I was able to resolve it only by copying this function from google colab tools. It doesn't show the image in a new window, but inline in the Jupyter notebook.

import cv2
from IPython import display
from PIL import Image

def cv2_imshow(a):
    """A replacement for cv2.imshow() for use in Jupyter notebooks.
    Args:
    a : np.ndarray. shape (N, M) or (N, M, 1) is an NxM grayscale image. shape
      (N, M, 3) is an NxM BGR color image. shape (N, M, 4) is an NxM BGRA color
      image.
    """
    a = a.clip(0, 255).astype('uint8')
    # cv2 stores colors as BGR; convert to RGB
    if a.ndim == 3:
        if a.shape[2] == 4:
            a = cv2.cvtColor(a, cv2.COLOR_BGRA2RGBA)
        else:
            a = cv2.cvtColor(a, cv2.COLOR_BGR2RGB)
    display.display(Image.fromarray(a))
2

I have the latest version of python (2.7.15) on Mac OS X 10.14.3.

Why can't we just save the contents to a file and run it using the command python filename.py. It's still the same nonetheless and it works!!

The sample code I tested is:

import cv2

img = cv2.imread('sample.jpg')

cv2.startWindowThread()
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()

Hope it helps!

0

This is working for me. The window is shut down, the program can continue. The fly in the ointment is that the window icon is still in the dock.

while cv2.waitKey(100) != 27:# loop if not get ESC
    cv2.imshow("Window_Name", Your_Image)

cv2.destroyAllWindows()
cv2.waitKey(1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.