Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
745 views
in Technique[技术] by (71.8m points)

python - reshape an array of images

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.

This is how I'm getting the images:

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


images = data["train_images"]

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

Someone suggested to me that:

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

Can someone explain what I need to do and why? (What it does)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

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

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

images may also be 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
....

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...