只处理一个图像使用所有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如果你正在使用任何python笔记本内核,如jupyter笔记本,google colab或kaggle笔记本。