Confused during reshaping array of image

妖精的绣舞 提交于 2019-12-23 20:49:19

问题


At the moment I'm trying to run a ConvNet. Each image, which later feeds the neural net, is stored as a list. But the list is at the moment created using three for-loops. Have a look:

im = Image.open(os.path.join(p_input_directory, item))
pix = im.load()

image_representation = []

# Get image into byte array
for color in range(0, 3):
    for x in range(0, 32):
        for y in range(0, 32):
            image_representation.append(pix[x, y][color])

I'm pretty sure that this is not the nicest and most efficient way. Because I have to stick to the structure of the list created above, I thought about using numpy and providing an alternative way to get to the same structure.

from PIL import Image
import numpy as np

image = Image.open(os.path.join(p_input_directory, item))
image.load()
image = np.asarray(image, dtype="uint8")
image = np.reshape(image, 3072)
# Sth is missing here...

But I don't know how to reshape and concatenate the image for getting the same structure as above. Can someone help with that?


回答1:


One approach would be to transpose the axes, which is essentially flattening in fortran mode i.e. reversed manner -

image = np.asarray(im, dtype="uint8")
image_representation = image.ravel('F').tolist()

For a closer look to the function have a look to the numpy.ravel documentation.



来源:https://stackoverflow.com/questions/41863336/confused-during-reshaping-array-of-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!