Python 创建 Numpy 图像数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37747021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Numpy array of images
提问by Abhishek Bansal
I have some (950) 150x150x3 .jpg image files that I want to read into an Numpy array.
我有一些 (950) 150x150x3 .jpg 图像文件,我想将它们读入 Numpy 数组。
Following is my code:
以下是我的代码:
X_data = []
files = glob.glob ("*.jpg")
for myFile in files:
image = cv2.imread (myFile)
X_data.append (image)
print('X_data shape:', np.array(X_data).shape)
The output is (950, 150)
. Please let me know why the list is not getting converted to np.array
correctly and whether there is a better way to create the array of images.
输出是(950, 150)
。请让我知道为什么列表没有被np.array
正确转换,以及是否有更好的方法来创建图像数组。
Of what I have read, appending to numpy arrays is easier done through python lists and then converting them to arrays.
在我读过的内容中,通过 python 列表更容易地附加到 numpy 数组,然后将它们转换为数组。
EDIT: Some more information (if it helps), image.shape
returns (150,150,3)
correctly.
编辑:更多信息(如果有帮助),正确image.shape
返回(150,150,3)
。
回答by DomTomCat
I tested your code. It works fine for me with output
我测试了你的代码。输出对我来说很好用
('X_data shape:', (4, 617, 1021, 3))
('X_data shape:', (4, 617, 1021, 3))
however, all images were exactly the same dimension.
然而,所有图像的尺寸都完全相同。
When I add another image with different extents I have this output:
当我添加另一个具有不同程度的图像时,我有以下输出:
('X_data shape:', (5,))
('X_data shape:', (5,))
So I'd recommend checking the sizes and the same number of channels (as in are really all images coloured images)? Also you should check if either all images (or none) have alpha channels (see @Gughan Ravikumar's comment)
所以我建议检查尺寸和相同数量的通道(因为实际上所有图像都是彩色图像)?此外,您应该检查所有图像(或没有)是否具有 alpha 通道(请参阅@Gughan Ravikumar 的评论)
If only the number of channels vary (i.e. some images are grey), then force loading all into the color format with:
如果只有通道数不同(即某些图像是灰色的),则强制将所有图像加载到颜色格式中:
image = cv2.imread (myFile, cv2.IMREAD_COLOR)
EDIT: I used the very code from the question, only replaced with a directory of mine (and "*.PNG"):
编辑:我使用了问题中的代码,仅替换为我的目录(和“*.PNG”):
import cv2
import glob
import numpy as np
X_data = []
files = glob.glob ("C:/Users/xxx/Desktop/asdf/*.PNG")
for myFile in files:
print(myFile)
image = cv2.imread (myFile)
X_data.append (image)
print('X_data shape:', np.array(X_data).shape)
回答by Mridul Pandey
Appending images in a list and then converting it into a numpy array, is not working for me. I have a large dataset and RAM gets crashed every time I attempt it. Rather I append the numpy array, but this has its own cons. Appending into list and then converting into np array is space complex, but appending a numpy array is time complex. If you are patient enough, this will take care of RAM crasing problems.
将图像附加到列表中,然后将其转换为 numpy 数组,这对我不起作用。我有一个大型数据集,每次尝试时 RAM 都会崩溃。相反,我附加了 numpy 数组,但这有其自身的缺点。附加到列表然后转换为 np 数组是空间复杂的,但附加 numpy 数组是时间复杂的。如果您足够耐心,这将解决 RAM 崩溃问题。
def imagetensor(imagedir):
for i, im in tqdm(enumerate(os.listdir(imagedir))):
image= Image.open(im)
image= image.convert('HSV')
if i == 0:
images= np.expand_dims(np.array(image, dtype= float)/255, axis= 0)
else:
image= np.expand_dims(np.array(image, dtype= float)/255, axis= 0)
images= np.append(images, image, axis= 0)
return images
I am looking for better implementations that can take care of both space and time. Please comment if someone has a better idea.
我正在寻找可以兼顾空间和时间的更好的实现。如果有人有更好的主意,请发表评论。
回答by Bob
Your definition for the .JPG frame that will be put into a matrix of the same size should should be x, y, R, G, B, A. "A" is not used, but it does take up 8 bits at the end of each pixel.
您对将放入相同大小矩阵的 .JPG 帧的定义应为 x、y、R、G、B、A。未使用“A”,但它在末尾确实占用了 8 位每个像素的。