简体   繁体   English

创建 Numpy 图像数组

[英]Create Numpy array of images

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) .输出是(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.请让我知道为什么列表没有正确转换为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)

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 (ie 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)

Appending images in a list and then converting it into a numpy array, is not working for me.将图像附加到列表中,然后将其转换为 numpy 数组,这对我不起作用。 I have a large dataset and RAM gets crashed every time I attempt it.我有一个大型数据集,每次尝试时 RAM 都会崩溃。 Rather I append the numpy array, but this has its own cons.相反,我附加了 numpy 数组,但这有其自身的缺点。 Appending into list and then converting into np array is space complex, but appending a numpy array is time complex.附加到列表然后转换为 np 数组是空间复杂的,但附加 numpy 数组是时间复杂的。 If you are patient enough, this will take care of RAM crasing problems.如果您足够耐心,这将解决 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.如果有人有更好的主意,请发表评论。

Here is a solution for images that have certain special Unicode characters, or if we are working with PNGs with a transparency layer, which are two cases that I had to handle with my dataset.这是具有某些特殊 Unicode 字符的图像的解决方案,或者如果我们使用带有透明层的 PNG,这是我必须处理我的数据集的两种情况。 In addition, if there are any images that aren't of the desired resolution, they will not be added to the Numpy array.此外,如果有任何不符合所需分辨率的图像,它们将不会添加到 Numpy 数组中。 This uses the Pillow package instead of cv2.这使用Pillow 包而不是 cv2。

resolution = 150

import glob
import numpy as np
from PIL import Image

X_data = []
files = glob.glob(r"D:\Pictures\*.png")
for my_file in files:
    print(my_file)
    
    image = Image.open(my_file).convert('RGB')
    image = np.array(image)
    if image is None or image.shape != (resolution, resolution, 3):
        print(f'This image is bad: {myFile} {image.shape if image is not None else "None"}')
    else:
        X_data.append(image)

print('X_data shape:', np.array(X_data).shape)
# If you have 950 150x150 images, this would print 'X_data shape: (950, 150, 150, 3)'

If you aren't using Python 3.6+, you can replace the r-string with a regular string (except with \\\\ instead of \\ , if you're using Windows), and the f-string with regular string interpolation.如果您不使用 Python 3.6+,您可以用常规字符串替换 r 字符串(除非使用\\\\代替\\ ,如果您使用的是 Windows),以及带有常规字符串插值的 f 字符串。

您对将放入相同大小矩阵的 .JPG 帧的定义应为 x、y、R、G、B、A。未使用“A”,但它在末尾确实占用了 8 位每个像素的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM