Python 堆叠不同大小图像的Numpy阵列

Python 堆叠不同大小图像的Numpy阵列,python,numpy,opencv,image-processing,image-resizing,Python,Numpy,Opencv,Image Processing,Image Resizing,我正在使用OpenCV创建一组图像,以便在TensorFlow中进行分析 我创建了以下函数: def files_to_img_array(path, files_list): ''' Reads a list of image files and creates a Numpy array. ''' # Instantiate arrays files = [path+file for file in files_list] img_array

我正在使用OpenCV创建一组图像,以便在TensorFlow中进行分析

我创建了以下函数:

def files_to_img_array(path, files_list):
    '''
    Reads a list of image files and creates a Numpy array.
    '''
    # Instantiate arrays
    files = [path+file for file in files_list]
    img_array = np.zeros(72000000) # for flattened 4000x6000 images
    image_names = []

    for file in tqdm.tqdm(files_list):
        full_file = path+file
        image_names.append(file.split('.')[0])
        img = cv2.imread(full_file, 1)
        print(img.shape)
        img = img.flatten()
        
        img_array = np.vstack([img_array, img])
    img_array = img_array[1:] # remove instantiating zeroes
    return img_array 
问题在于图像大小不一致:

 0%|                                     | 0/10 [00:00<?, ?it/s](4000, 6000, 3)
 10%|███████▊                    | 1/10 [00:00<00:03,  2.64it/s](4000, 6000, 3)
 20%|███████████████▌            | 2/10 [00:00<00:03,  2.51it/s](2848, 4288, 3)
 20%|███████████████▌            | 2/10 [00:00<00:03,  2.18it/s]
Traceback (most recent call last):
...
ValueError: all the input array dimensions for the concatenation axis
must match exactly, but along dimension 1, the array at index 0 has
size 72000000 and the array at index 1 has size 36636672

0%| | 0/10[00:00以下是如何使用透明填充在Python/OpenCV中垂直堆叠任意大小的图像

输入图像:


结果:
感谢@hpaulj在问题中的评论,这导致了我的调查和回答

以下代码依赖于KERA和基础PIL:

import PIL
import tensorflow
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import concurrent.futures

def keras_pipeline(file):
    TARGET_SIZE = (100,150)
    img = load_img(file, target_size=TARGET_SIZE)
    img_array = img_to_array(img)
    return img_array
 
def files_to_array(path, files_list):
    files = [path+file for file in files_list]
    with concurrent.futures.ProcessPoolExecutor() as executor:
        img_map = executor.map(keras_pipeline, files)
    return img_map
keras\u pipeline()
为每个图像创建一个转换管道。
将转换管道映射到每个图像上,并返回一个生成器。然后可以使用
np.hstack()
将该生成器作为Numpy数组传递:


我不知道任何简单的解决方法。我过去做的是检查所有尺寸,并在每个宽度和高度中找到最大值。然后用黑色或透明填充图像,使其尺寸相同。你可以使用cv2.copyMakeBorder来填充图像。
cv2
应该有一个
调整大小的方法。我认为图像调整大小比填充好,但是您的
tensorflow
docs(或其他ML)我们应该讨论这类问题。@fmw42我也考虑过这样做。问题是它会抛开直方图分析,所以我只能想象它会对分类器造成多大的破坏…@Yehuda好的。但你应该在你最初的问题中这样说。从我读过的ML论文中,人们通常会将图像调整为逗号在将它们传递给ML算法之前,请先查看大小。感谢您的回答--我特别尝试堆叠
cv2.imread()的Numpy数组输出
,但是。我不明白为什么会有不同。你把你的图像放在一个列表中。你可以在阅读完所有图像后在列表上循环。或者你可以在检查新图像和上一个堆栈的宽度后在上一个堆栈上堆叠一个新图像。但我对张量流及其要求知之甚少。Cool!基本上,你通过填充使所有图像大小相同?
import PIL
import tensorflow
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import concurrent.futures

def keras_pipeline(file):
    TARGET_SIZE = (100,150)
    img = load_img(file, target_size=TARGET_SIZE)
    img_array = img_to_array(img)
    return img_array
 
def files_to_array(path, files_list):
    files = [path+file for file in files_list]
    with concurrent.futures.ProcessPoolExecutor() as executor:
        img_map = executor.map(keras_pipeline, files)
    return img_map
for img in img_map:
    existing_array = np.hstack([existing_array, img])