40

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

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Alankrit
  • 658
  • 1
  • 6
  • 17
  • 1
    Found one workaround. We can use `%matplotlib inline` in the code to use [tag:imshow]. Used as example here in In[28] - [Colab notebook git](https://github.com/alankritmishra/DL-CompVisionNotebooks/blob/master/Packt_CV_w_Python3_Getting_Started_with_Jupyter_notebooks_Done.ipynb) – Alankrit Mar 23 '19 at 07:45

7 Answers7

86

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

Kevin Patel
  • 605
  • 10
  • 11
Mohammed Waseem
  • 1,026
  • 8
  • 5
14

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

Ajai
  • 1,049
  • 1
  • 11
  • 23
11

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

Alankrit
  • 658
  • 1
  • 6
  • 17
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 enter image description here

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
  • Hi Bob, I need to use `imshow()` to plot images with different OpenCV functions. Is there any workaround to use it in Colab? – Alankrit Mar 22 '19 at 17:21
  • I'd recommend asking a distinct question that describes the precise scenario requiring `imshow`. It will be a terrific pain to start an X server and pipe a snapshot from its rendered output, but it's doable. I suspect it will be worth considering alternatives. – Bob Smith Mar 22 '19 at 20:19
  • Found one workaround. We can use `%matplotlib inline` in the code to use [tag:imshow]. Used as example here in In[28] - [link](https://github.com/alankritmishra/DL-CompVisionNotebooks/blob/master/Packt_CV_w_Python3_Getting_Started_with_Jupyter_notebooks_Done.ipynb) – Alankrit Mar 23 '19 at 07:39
1
cv2.imshow() 

does not work well in colab, you can use matplotlib for displaying.

import matplotlib.image as mpimg 
from matplotlib.pyplot import imshow
%matplotlib inline
testim = mpimg.imread('butterfly.jpg')
imshow(testim)

or you can do colab's own cv2_imshow version

from google.colab.patches import cv2_imshow
cv2_imshow('butterfly.jpg')
Muhammad Zakaria
  • 1,269
  • 6
  • 14
0

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.

Pratham
  • 1,522
  • 1
  • 18
  • 33
-1

I am also facing a same issue in google colab.

We can use cv2_imshow() instead of (cv2.imshow or cv.imshow):

#We must import first line of code
**#working module**

from google.colab.patches import cv2_imshow
import cv2 as cv
#Replace cv2.imshow() to cv2_imshow()
img = cv.imread('python.jpg') #mentioning a path of an image
cv2_imshow(img)

while we are using our local machine to visble any image then we use this code cv.imshow(), but when it comes to google colab we should switch to alternative code cv2_imshow()

example