更改图像阵列形式

我有 60000 train_images, 带来了形式矩阵的形式 /28,28,60000/. 它 numpy.ndarray. 我想将它转换为一维图像的数组,即,每个图像都表示为单行/数组数字和我想要 60000 阵列。 换句话说,我想从 /28, 28, 60000/ 到 /60000, 28*28/. 在 python, 这将是:


images_features = []
for image in images:
imageLine = []
for y in range/len/image//:
for x in range/len/image[0]//:
imageLine.append/image[y][x]/
images_features.append/imageLine/


我怎样才能做到这一点? 我怀疑我需要使用 reshape, 但我无法准确理解我可以做到的事情。

这是我如何获得图像:


data = scipy.io.loadmat/'train.mat'/


images = data["train_images"]


所以, "images"- 这是我在谈论的阵列。

有人建议我:

"您可能必须更改轴或组合它们以获取所需的功能。 如果图像侧身,我建议在其建造它们。 确保仔细联系轴以避免在那里进一步的问题。"

我不知道我们在谈论什么以及如何考虑到上述内容。

有人可以解释我需要做什么以及为什么? /他所做的

/
已邀请:

喜特乐

赞同来自:

因为这发生了
loadmat

, 表格
/28,28,60000/

有意思 - MATLAB 从最后一个索引开始重复。


images.transpose// # or images.T


重新排序轴,因此获得结果
/60000,28,28/

. 最后两个维度可以与形状的变化相结合。


images.T.reshape/60000,28*28/
images.T.reshape/60000,-1/ # short hand


您需要转换图像 28x28, 例如


images.transpose/[2,0,1]/ # instead of the default [2,1,0]



.T

- 这与 MATLAB
'

/或者
.'

/.


images

也许
order='F'

.


octave:38> images=reshape/1:30,2,3,5/;
octave:39> save test.mat -v7 images
octave:40> images
images =

ans/:,:,1/ =

1 3 5
2 4 6

ans/:,:,2/ =

7 9 11
8 10 12
....


我选择了测试尺寸缩小,并轻松区分各种轴。

在会话中 Ipython:


In [15]: data=io.loadmat/'test.mat'/

In [16]: data
Out[16]:
{'__globals__': [],
'__header__': 'MATLAB 5.0 MAT-file, written by Octave 3.8.2, 2016-02-10 05:19:18 UTC',
'__version__': '1.0',
'images': array/[[[ 1., 7., 13., 19., 25.],
[ 3., 9., 15., 21., 27.],
[ 5., 11., 17., 23., 29.]],

[[ 2., 8., 14., 20., 26.],
[ 4., 10., 16., 22., 28.],
[ 6., 12., 18., 24., 30.]]]/}

In [18]: data['images'].T
Out[18]:
array/[[[ 1., 2.],
[ 3., 4.],
[ 5., 6.]],

[[ 7., 8.],
[ 9., 10.],
[ 11., 12.]],
....
In [19]: data['images'].transpose/[2,0,1]/
Out[19]:
array/[[[ 1., 3., 5.],
[ 2., 4., 6.]],

[[ 7., 9., 11.],
[ 8., 10., 12.]],
....
In [22]: data['images'].transpose/[2,1,0]/.reshape/5,-1/
Out[22]:
array/[[ 1., 2., 3., 4., 5., 6.],
[ 7., 8., 9., 10., 11., 12.],
...

裸奔

赞同来自:

我想你只需要使用 reshape:


>>> images = np.ndarray/[60000, 28, 28]/
>>> images.shape
/60000, 28, 28/
>>> images_rs = images.reshape/[60000, 28*28]/
>>> images_rs.shape
/60000, 784/

龙天

赞同来自:

您可以更改表单
train_images

并检查它,建筑图形图像,

重组:


train_features_images = train_images.reshape/train_images.shape[0],28,28/


构建图像图:


import matplotlib.pyplot as plt
def show_images/features_images,labels,start, howmany/:
for i in range/start, start+howmany/:
plt.figure/i/
plt.imshow/features_images[i], cmap=plt.get_cmap/'gray'//
plt.title/labels[i]/
plt.show//
show_images/train_features_images, labels, 1, 10/

要回复问题请先登录注册