重塑图像数组时感到困惑 [英] Confused during reshaping array of image

查看:60
本文介绍了重塑图像数组时感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在尝试运行ConvNet。每个稍后供入神经网络的图像都存储为一个列表。但是此列表是使用三个for循环创建的。看看:

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])

我很确定这不是最好,最有效的方法。因为我必须坚持上面创建的列表的结构,所以我考虑使用 numpy 并提供另一种获取相同结构的方法。

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...

但是我不知道如何重塑和连接 image 以获得与上述相同的结构。有人可以帮忙吗?

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

推荐答案

一种方法是转置轴,这实际上是在 fortran 模式,即反转方式-

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

近距离查看该函数请查看 numpy.ravel文档

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

这篇关于重塑图像数组时感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆