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

*爱你&永不变心* 提交于 2019-12-01 22:08:21

问题


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 output. Can anybody help me with this?

%pylab notebook
import cv2

testim = imread('butterfly.jpg')
figure()
imshow(testim)
plt.show()

Screenshot: enter image description here

Link to my Colab Notebook


回答1:


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




回答2:


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()




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/55288657/image-is-not-displaying-in-google-colab-while-using-imshow

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