本文介绍了重塑图像阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我带来了作为一个形状(28,28,60000)矩阵60000 train_images。它是一个numpy.ndarray。我想将其转换为1二维图像阵列,这意味着每个图像重新psented为数字单行/阵列$ P $,我想60000阵列。换句话说,我想从(28,28,60000)去(60000,28 * 28)。在Python中,这将是:

I have 60000 train_images brought in as a shape (28,28,60000) matrix. It is a numpy.ndarray. I want to convert it to an array of 1 dimensional images, meaning each image is represented as a single line/array of numbers, and I want 60000 arrays. In other words, I want to go from (28, 28, 60000) to (60000, 28*28). In python, it would be:

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)

我怎样才能做到这一点?我怀疑我需要使用重塑,但我无法弄清楚我究竟是如何能做到这一点。

How can I do this? I suspect that I need to use reshape but I couldn't figure out how exactly I can do this.

这是我应得的图像:

data = scipy.io.loadmat('train.mat')


images = data["train_images"]

所以,图像是我说的数组。

So the "images" is the array I'm talking about.

有人向我建议:

你可能需要改变的轴或将它们组合起来做得到你想要的功能。我建议在情况下的图像横盘结束绘制它们。确保你的勤奋与轴,以避免出现更多的问题。

"You may need to change axes or combine them do get the functionality you want. I recommend plotting them as well in case an image ends up sideways. Make sure you are diligent with your axes to avoid further problems there."

我不知道什么叫轴正在这里所指的,以及如何采取什么上面说的考虑。

I have no idea what "axes" is being referred to here and how to take what's said above into account.

有人能解释什么,我需要做的,为什么呢? (做什么)

推荐答案

由于这是通过 loadmat 即将到来,的形状(28,28 ,60000)是有道理的 - 从最后指数MATLAB进行迭代,

Since this is coming via loadmat, a shape of (28,28,60000) makes sense - MATLAB iterates starting with the last index.

images.transpose()  # or images.T

重新排序的轴,所以结果是(60000,28,28)。最后两个尺寸可以与重塑组合

reorders the axes, so the result is (60000,28,28). The last two dimensions can combined with a reshape

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

您很多需要调换28x28的图像,例如

You many need to transpose the 28x28 images, e.g.

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

.T 是一样的MATLAB (或)。

.T is the same as the MATLAB ' (or .').

图片也可能是为了='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
....

我选择测试的尺寸要小,并可以很容易地分辨出不同的轴。

I chose test dimensions to be small, and to make it easy to distinguish the different axes.

在一个会话IPython的:

In a Ipython session:

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

这篇关于重塑图像阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:50