问题
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