python - 仅处理一张图像会使用所有 Ram
问题描述
我在 google colab 中运行此代码(我也在本地尝试过)。我只使用一张图片,它使用了所有的内存。我做错了吗?使用 16 GB 内存正常吗?我添加了 %matplotlib 内联,它仍然崩溃?
import cv2
import matplotlib.pyplot as plt
import numpy as np
f = cv2.imread('/content/gdrive/MyDrive/grass.png')
f = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY).astype(float)
plt.imshow(f)
plt.colorbar()
def gauss1(sigma, width):
hwidth = round((width-1)/2)
x = np.arange(-hwidth, hwidth+1,1)
g = np.exp(-x**2/(2*sigma**2))
return g/np.sum(g)
g1=gauss1(2,11)
g1=np.reshape(g1,(1,-1))
plt.imshow(g1)
f1 = cv2.filter2D(f,-1,g1)
fig = plt.figure(figsize=(10, 20))
fig.add_subplot(1,2,1)
plt.imshow(f)
fig.add_subplot(1,2,2)
plt.imshow(f1)
它甚至不会产生最后一个代码片段的输出。
解决方案
利用:
- 如果您使用的是终端/控制台,请用于
plt.show()
显示 matplotlib 输出。 - 如果您使用
%matplotlib inline
任何 ipython notebook 内核,例如 jupyter notebooks、google colab 或 kaggle notebooks,请使用。
推荐阅读
- spell-checking - CKEditor 4 - How to deactivate spellcheck in Source view
- c# - Xunit 测试和 MOQ IActionResult 返回类型
- html - Adding an image into an .html envelope for signature via REST API
- javascript - How can I get top 10 points?
- jquery - taffyDB query for "not in" set
- sql - How to get the records where the term data is not in the presiding effective date
- python - 如何遍历 MRJob 中的行
- javascript - 如何在执行另一个函数后每次都检查一个函数是否被调用
- javascript - HTML表单输入无效然后javascript函数
- python - Kivy password check