Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cv2.imshow(img) is crashing the kernel #3935

Closed
epignatelli opened this issue Sep 19, 2018 · 15 comments
Closed

cv2.imshow(img) is crashing the kernel #3935

epignatelli opened this issue Sep 19, 2018 · 15 comments

Comments

@epignatelli
Copy link

As per title cv2.imshow(img) is crashing the server.

If we avoid cv2.waitForKey() and cv2.closeAllWindows(), and keep the windows open, the notebook will continue running.

Python version:

Python 3.5.6 |Anaconda custom (64-bit)| (default, Aug 26 2018, 16:05:27) [MSC v.1900 64 bit (AMD64)]

Kernel about:

Python 3.5.5 |Anaconda, Inc.| (default, Mar  9 2018, 12:39:44) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

To reproduce: (coming)


@ah3243
Copy link

ah3243 commented Feb 7, 2019

I'm having a similar problem, the imshow() window is displayed it crashes when it would normally be destroyed. Even it's the last of 10 windows created and destroyed in a loop the last one will always freeze as the program moves on. Gives no errors when running identical code in a terminal.

OSx Mojave 10.14.2
Python 3.5
opencv-python 4.0.0.12

@nvt-ak
Copy link

nvt-ak commented May 19, 2019

You can use this solution if you're using google colab:
from google.colab.patches import cv2_imshow
cv2_imshow(img)
It works fine!

@mitesh98
Copy link

image is appearing too big.I use below code
import matplotlib.pyplot as plt
plt.figure(figsize=(18,18))
plt.imshow(image)
plt.axis('off')
plt.show()

@Oshana
Copy link

Oshana commented Jul 18, 2019

how to convert this to cv2_imshow?

cv2.imshow('object detection', cv2.resize(image_np, (800, 600)))

@shivam-kotwalia
Copy link

@Oshana just say -
cv2_imshow(cv2.resize(image_np, (800, 600)))
cv2_imshow only takes one input parameter, https://github.com/googlecolab/colabtools/blob/8708337447efdef2732cca9764ede400da6c051f/google/colab/patches/__init__.py#L14

@epignatelli
Copy link
Author

epignatelli commented Jul 23, 2019

You can also consider using matplotlib rather than IPython.display, although, I would not recommend to mix the two.

import matplotlib.pyplot as plt
import cv2
def cv2_imshow(a, **kwargs):
    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)

    return plt.imshow(a, **kwargs)

@kyteinsky
Copy link

kyteinsky commented Feb 14, 2020

image is appearing too big.I use below code
import matplotlib.pyplot as plt
plt.figure(figsize=(18,18))
plt.imshow(image)
plt.axis('off')
plt.show()

You can use this insted:
plt.figure(figsize=(10,18))
for resizing the image.

@kyteinsky
Copy link

This fix also works for opening image using imshow() function from OpenCV::

cv.namedWindow(winname='my')
while True:
cv.imshow('my',img)
if cv.waitKey(1) & 0xFF == 27:
break
cv.destroyAllWindows()

@PrasadNR
Copy link

You can use this solution if you're using google colab:
from google.colab.patches import cv2_imshow
cv2_imshow(img)
It works fine!

This is just showing some black blank image.

@antopolskiy
Copy link

Copying the mentioned function cv2_imshow from colabtools and using it locally worked well for me. It displays the image inline.

@kilicsedat
Copy link

This approach worked for me, too.

@ozrite
Copy link

ozrite commented Apr 2, 2020

hi guys, here is the solution of the problem.

If you are using a 64-bit machine, you will have to modify
k = cv2.waitKey(0)
line as follows :
k = cv2.waitKey(0) & 0xFF

The full solution is like this:

```
cv2.imshow('image',img)
k = cv2.waitKey(0) & 0xFF
if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()

Now the kernel will not be stuck or crash. You just need to press esc to quit.

@KrishnaChaitanya-Gopaluni

You can also consider using matplotlib rather than IPython.display, although, I would not recommend to mix the two.

import matplotlib.pyplot as plt
import cv2
def cv2_imshow(a, **kwargs):
    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)

    return plt.imshow(a, **kwargs)

Thank you.

@kevin-bates
Copy link
Member

This is not a notebook issue and should probably have been redirected to ipykernel or ipython. However, the last few comments have been success stories, so, on that account, I'm closing this issue.

@szelesaron
Copy link

szelesaron commented Dec 4, 2020

You can use this solution if you're using google colab:
from google.colab.patches import cv2_imshow
cv2_imshow(img)
It works fine!

This is just showing some black blank image.

If you have this issue, check if the numpy array is of type float, if so then multiply the array by 255.

https://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html?highlight=imshow#imshow

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests