首页 > 解决方案 > 仅处理一张图像会使用所有 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)

它甚至不会产生最后一个代码片段的输出。

标签: pythonpython-3.xnumpyopencvimage-processing

解决方案


利用:

  • 如果您使用的是终端/控制台,请用于plt.show()显示 matplotlib 输出。
  • 如果您使用%matplotlib inline任何 ipython notebook 内核,例如 jupyter notebooks、google colab 或 kaggle notebooks,请使用。

推荐阅读