您的位置:首页 > 编程语言 > MATLAB

caffe的matlab和python接口数据的处理

2017-05-13 15:47 369 查看
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);


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: