Image is not displaying in Google Colab while using imshow()

前端 未结 5 1740
甜味超标
甜味超标 2020-12-09 11:48

I am working on a project which requires functions from OpenCV to plot images. I am trying to display image using the below code in Google Colab. But nothing shows up in the

相关标签:
5条回答
  • 2020-12-09 12:05

    The cv2.imshow() and cv.imshow() functions from the opencv-python package are incompatible with Jupyter notebook; see https://github.com/jupyter/notebook/issues/3935.

    As a replacement, you can use the following function:

    from google.colab.patches import cv2_imshow
    

    For example, here we download and display a PNG image of the Colab logo:

    !curl -o logo.png https://colab.research.google.com/img/colab_favicon_256px.png
    import cv2
    img = cv2.imread('logo.png', cv2.IMREAD_UNCHANGED)
    cv2_imshow(img)
    

    Credits: Code Snippets in Google Colab

    0 讨论(0)
  • 2020-12-09 12:05

    imshow requires an X server, which isn't available in a web browser.

    Instead, use the IPython.display.Image library. Here's an example: https://colab.research.google.com/drive/1jWHKR6rhhyZtUulttBD6Pxd_AJhgtVaV

    0 讨论(0)
  • 2020-12-09 12:09

    Found one workaround. We can use %matplotlib inline in the code to use imshow. Used as example here in In[28] - link

    0 讨论(0)
  • 2020-12-09 12:11

    Google colab crashes if you try to display image using cv2.imshow() instead import from google.colab.patches import cv2_imshow and display using cv2_imshow(<image>)

    0 讨论(0)
  • 2020-12-09 12:23

    Instead of using cv2.imshow() try this:

    1. Change the import to from google.colab.patches import cv2_imshow
    2. Replace cv2.imshow() to cv2_imshow()

    I tried it and it worked for me.

    0 讨论(0)
提交回复
热议问题