notebook: cv2.imshow(img) is crashing the kernel

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)


About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

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

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)

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

@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

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.

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

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.