测试np中数组的转置操作:
# coding=gbk
'''
Created on 2017年5月9日
'''
from scipy.misc.pilutil import * # read image
import matplotlib.pyplot as plt # show image
import numpy as np # 两个方法都用
from numpy import *
###############################################
################## 二维数组 ######################
arr = np.array([[1,2,3],[4,5,6]])
rows = arr.shape[0]
cols = arr.shape[1]
print "rows: ", rows,"cols: ",cols
# test python.numpy: rows major
arr2 = np.reshape(arr,[1,arr.shape[0]*arr.shape[1]])
print arr2 # result: [1 2 3 4 5 6]
################### 三维数组 ###########################
arr_3d = np.array([[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]],[[17,18,19,20],[21,22,23,24]]])
print arr_3d.shape # (3L,2L,4L) (页,行,列),按行存储
## reshpae
arr_3d_reshape = arr_3d.reshape((1,arr_3d.shape[0]*arr_3d.shape[1]*arr_3d.shape[2]))
print arr_3d_reshape # (1,24)
arr_3d_tran = arr_3d.transpose((2,1,0))
arr_3d_tran_reshape = arr_3d_tran.reshape((1,arr_3d.shape[0]*arr_3d.shape[1]*arr_3d.shape[2]))
print "arr_3d_tran_reshape : ",arr_3d_tran_reshape
print arr_3d_tran.shape #(4L,2L,3L)
print arr_3d_tran
print arr_3d
######################### 分析结果 #####################
##########
##########
#######################################################
######## 分析图像
img = imread("lena.jpg")
######################
print img.shape # (512L,512L,3L)
print type(img.shape) # tuple
########################
img_tran1 = img.transpose((2,1,0)) # change (0,1,2) to (2,1,0)
print img_tran1.shape #(3L,512L,512L)
gray_img = img[:,:,0]
plt.subplot(1,2,1)
plt.imshow(gray_img)
plt.subplot(1,2,2)
plt.imshow(img_tran1[0,:,:])
plt.show()
# 结论: 由 (512,513,3),即 高*宽*通道,转换为 :(3,512,512),即 通道*宽*高