Image is not displaying in Google Colab while using imshow()
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:
Google colab crashes if you try to display image using
cv2.imshow()
instead importfrom google.colab.patches import cv2_imshow()
and display usingcv2_imshow()
Found one workaround. We can use
%matplotlib inline
in the code to use imshow. Used as example here in In[28] - linkimshow
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_AJhgtVaVThe 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:
For example, here we download and display a PNG image of the Colab logo:
Credits: Code Snippets in Google Colab
Instead of using
cv2.imshow()
try this:from google.colab.patches import cv2_imshow
cv2.imshow()
tocv2_imshow()
I tried it and it worked for me.
while in colab I run a script containing cv2_imshow(image) it returns
why this isn't working?
edit: i just had to add
to the script. The cell run works but returns
and no image. is there an inline for this?