cv2.imshow() crashes on Mac

不打扰是莪最后的温柔 提交于 2020-08-22 11:47:29

问题


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:


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



回答2:


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.




回答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))



回答4:


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!



来源:https://stackoverflow.com/questions/46348972/cv2-imshow-crashes-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!