2
% a class for input and output functions
5
function im_data = load_image(im_file)
6
% im_data = load_image(im_file)
7
% load an image from disk into Caffe-supported data format
8
% switch channels from RGB to BGR, make width the fastest dimension
9
% and convert to single
10
% returns im_data in W x H x C. For colored images, C = 3 in BGR
11
% channels, and for grayscale images, C = 1
12
CHECK(ischar(im_file), 'im_file must be a string');
13
CHECK_FILE_EXIST(im_file);
14
im_data = imread(im_file);
15
% permute channels from RGB to BGR for colored images
16
if size(im_data, 3) == 3
17
im_data = im_data(:, :, [3, 2, 1]);
19
% flip width and height to make width the fastest dimension
20
im_data = permute(im_data, [2, 1, 3]);
21
% convert from uint8 to single
22
im_data = single(im_data);
24
function mean_data = read_mean(mean_proto_file)
25
% mean_data = read_mean(mean_proto_file)
26
% read image mean data from binaryproto file
27
% returns mean_data in W x H x C with BGR channels
28
CHECK(ischar(mean_proto_file), 'mean_proto_file must be a string');
29
CHECK_FILE_EXIST(mean_proto_file);
30
mean_data = caffe_('read_mean', mean_proto_file);
32
function write_mean(mean_data, mean_proto_file)
33
% write_mean(mean_data, mean_proto_file)
34
% write image mean data to binaryproto file
35
% mean_data should be W x H x C with BGR channels
36
CHECK(ischar(mean_proto_file), 'mean_proto_file must be a string');
37
CHECK(isa(mean_data, 'single'), 'mean_data must be a SINGLE matrix');
38
caffe_('write_mean', mean_data, mean_proto_file);