[英]Memory error converting large RGB image to grayscale
I am trying to load original color fundus images in python for some learning project. 我正在尝试为一些学习项目在python中加载原始的彩色眼底图像。 These are RGB images of sizes approx.
这些是大小约为的RGB图像。 4000*2000*3.
4000 * 2000 * 3。 I want to convert each image to grayscale before sending them for training into my model.
我想先将每个图像转换为灰度,然后再将其发送到模型中进行训练。 For this, I've tried the following 2 approaches:
为此,我尝试了以下两种方法:
Method 1: 方法1:
import matplotlib.image as mpimg
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
def load_images(folder):
images = []
for filename in os.listdir(folder):
img = mpimg.imread(os.path.join(folder, filename))
gray = rgb2gray(img)
images.append(gray)
return images
Method 2: 方法2:
from PIL import Image
def load_images(folder):
images = []
for filename in os.listdir(folder):
img = Image.open(os.path.join(folder, filename))
gray = img.convert('L')
images.append(gray)
return images
In both the methods, I get the same Memory Error
at lines gray = rgb2gray(img)
and gray = img.convert('L')
respectively. 在这两种方法中,我分别在
gray = rgb2gray(img)
和gray = img.convert('L')
行得到相同的Memory Error
。
Is there any way I can convert such large images to grayscale one at a time and store it as a list? 有什么方法可以一次将如此大的图像转换为灰度图像并将其存储为列表吗?
Turns out that the problem is not with the commands but with the size of my data. 事实证明,问题不在于命令,而在于数据的大小。 As @user894763 correctly pointed out, my size of data amounts upto 10 GB which is the cause of Memory Error.
正如@ user894763正确指出的那样,我的数据大小高达10 GB,这是内存错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.