读取多个图像并存储到单个 numpy 数组中的最快方法

Fastest way to read multiple images and store into a single numpy array

我有一个应用程序,我将多次重复以下一组操作:

Operations:
-> Read N images (all have the same dimension (H,W))
-> Normalize each image to (0,1)
-> Store these N images in a single numpy array
-> Return the array (of shape (N, H, W))

将其翻译成代码,类似于:

def load_block(im_paths, H, W):
    N = len(im_paths)
    im_block = np.empty((N, H, W), dtype=np.float32) 

    for i, im_path in enumerate(im_paths):
        image = cv2.imread(im_path, 0)
        im_block[i, :, :] = (image-image.min())/(image.max()-image.min())
    return im_block

所以我想加快这个过程。我最初的目标是 numba,但是我不确定它是否在这里有用,因为我正在做 I/O 操作。

我不认为 numba 可以帮助加载图像。它可能有助于规范化。也许以下内容可能会让您有所收获。绝对值得一试。

images = [cv2.imread(im_path) for im_path in im_paths]

@njit
def load_block(images, H, W):
     ... loop over images rather than image paths ...