Python:
arr = np.load('ilsvrc_2012_mean.npy')
print arr.shape # (3,256,256) channel * height * width
########## 数据的转置 ###############
arr = np.transpose(arr,[1,2,0]) # height * width * channel
print arr.shape
########## 通道的交换 ###############
arr = arr[:,:,[2,1,0]]
print arr.shape
################## test lena ##########
img = imread('lena.jpg')
img = img[:,:,(2,1,0)] # change from rgb to gbr
img = img[:,:,(2,1,0)] # change back
plt.imshow(arr)
plt.show()
Matlab:
img = imread('lena.jpg'); % height * width * channel[rgb]
%%%%%%%%%%%%%%%%% [h,w,c] to [w,h,c] %%%%%%%%%%%%%
%%%%%%%%%%%% 因为matlab是按列存储的,caffe和python是按行存储的
imgforwd = permute(img,[2 1 3]);
%%%%%%%%%%%%%%% rgb to bgr %%%%%%%%%%%%%%
%%%%%%%%%%%因为caffe利用了opencv,opencv图像的存储就是bgr
imgforwd = imgforwd(:,:,[3 2 1]);
%%%%%%%%%%% 逆向回来 %%%%%%%%%%%%
imgback = permute(imgforwd,[2,1,3]);
imgback = imgback(:,:,[3,2,1]);
figure;
subplot(121),imshow(imgforwd);
subplot(122),imshow(imgback);