首页 > 解决方案 > 如何在谷歌 Colab 中显示图像?

问题描述

当我使用

cv2.imshow(img)

colab 抛出一个DisabledFunctionErrorhttps://github.com/jupyter/notebook/issues/3935)。使用 cv2_imshow(img) 我得到另一个错误,AttributeError: 'NoneType' object has no attribute 'clip').

我该如何解决这个问题?

标签: pythonopencvgoogle-colaboratory

解决方案


在 Google Colab 中使用 cv2.imshow(img) 会返回以下输出:

DisabledFunctionError: cv2.imshow() is disabled in Colab, because it causes Jupyter sessions
to crash; see https://github.com/jupyter/notebook/issues/3935.
As a substitution, consider using
  from google.colab.patches import cv2_imshow

因此,您可以简单地使用:

from google.colab.patches import cv2_imshow
import matplotlib.pyplot as plt

img = "yourImage.png"
img = cv2.imread(img) # reads image
plt.imshow(img)

推荐阅读