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