【问题标题】:Map plt.imread to Fixed Sized array将 plt.imread 映射到固定大小的数组
【发布时间】:2020-04-08 12:24:18
【问题描述】:

我正在制作一个需要我将图像转换为图像的每像素 RGB 值的固定大小数组的项目。读取图像如下:

import matplotlib.pyplot as plt
image = plt.imread("dummy_image.jpg") #Example image with a shape of 32x32
print(image.shape)

应该输出:

(32, 32, 3)

问题是我循环了数百张图像,对于每张图像,我分配了一个固定大小的数组(例如 64x64x3)并通过循环单独分配每个像素的值。

import numpy as np
arr1 = np.zeros((64, 64, 3))

#Then I looped for each pixel value and assigned it to arr1
x, y, w = image.shape
for i in range(x):
    for j in range(y):
        arr1[i][j] = image[i][j]

您可以推断,这段代码需要很长时间才能完成。更重要的是,由于每个图像的大小都是不同的,我无法合并它并为numpy.pad 创建一个函数以将图像大小固定为零。

谁能给我提示或技巧,让我以更节省内存和/或节省时间的方式做到这一点?

【问题讨论】:

  • 一定要用python吗?

标签: python image numpy matplotlib


【解决方案1】:

试试这个:

创建初始化输出图像

>>> output = np.zeros((3,5,5))
>>> output
array([[[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]]])

假设输入图像的尺寸为 2x2

>>> img = np.random.randint(0,256,size=(3,2,2))
>>> img
array([[[ 33, 245],
        [ 77,   8]],

       [[173,  64],
        [ 37, 182]],

       [[130, 225],
        [  1, 117]]])

将图像复制到输出图像,如下所示:

>>> d,r,c = img.shape
>>> output = np.zeros((3,5,5))
>>> output[:, 0:r, 0:c] = img
>>> output
array([[[ 33., 245.,   0.,   0.,   0.],
        [ 77.,   8.,   0.,   0.,   0.],
        [  0.,   0.,   0.,   0.,   0.],
        [  0.,   0.,   0.,   0.,   0.],
        [  0.,   0.,   0.,   0.,   0.]],

       [[173.,  64.,   0.,   0.,   0.],
        [ 37., 182.,   0.,   0.,   0.],
        [  0.,   0.,   0.,   0.,   0.],
        [  0.,   0.,   0.,   0.,   0.],
        [  0.,   0.,   0.,   0.,   0.]],

       [[130., 225.,   0.,   0.,   0.],
        [  1., 117.,   0.,   0.,   0.],
        [  0.,   0.,   0.,   0.,   0.],
        [  0.,   0.,   0.,   0.,   0.],
        [  0.,   0.,   0.,   0.,   0.]]])

【讨论】:

    【解决方案2】:

    我的理解是,您有许多图像要存储在 RAM 中。您需要问自己的第一个问题是,您是否真的需要同时存储它们?比如,你能不能读取一张,做一些处理,然后只保留处理的结果再读取下一张?

    当涉及到实际存储时,我不明白您为什么需要var1 变量。图片不够用吗?如果 var1 的目的是让所有图像具有相同的形状,那么为什么不能使用 pad?例如,imageNew = numpy.pad(image, [(0, x - y) for x, y in zip(arr1.shape, image.shape)])

    如果您不能使用 numpy.pad,那么只需 arr1[:image.shape[0], :image.shape[1], :] = image 为您工作吗?

    如果你想用循环来做,那就用 numba。

    from numba import jit
    import numpy as np
    
    
    @jit(nopython=True)
    def resize(image):
        arr1 = np.zeros((64, 64, 3))
        for i in range(image.shape[0]):
            for j in range(image.shape[1]):
                arr1[i][j] = image[i][j]
        return arr1
    

    【讨论】:

    • 感谢您的回答!您提供的numpy.pad 代码运行良好!
    • 不用担心,愉快的处理。 :)
    猜你喜欢
    • 2019-02-05
    • 2015-01-01
    • 2016-01-25
    • 1970-01-01
    • 2013-09-07
    • 2012-03-17
    • 2019-06-27
    • 1970-01-01
    • 2017-05-16
    相关资源
    最近更新 更多