-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveas.m
87 lines (63 loc) · 2.26 KB
/
saveas.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function saveas(R, saveDir, listFile, varargin)
% Save extracted regions on disk as image files.
%
V = @validateattributes;
isString = @(x)V(x, {'char'}, {'vector'});
checkType = @(x)(any(validatestring(x, {'jpg', 'jpeg', 'tif', 'png', 'mat'})));
checkQuality = @(x)V(x, {'numeric'}, {'scalar', 'integer', 'positive', '<=', 100});
checkLabels = @(x)V(x, {'numeric'}, {'2d', 'nonempty'});
parser = inputParser();
parser.addParameter('filetype', 'jpg', checkType);
parser.addParameter('quality', [], checkQuality);
parser.addParameter('suffix', '', isString);
parser.addParameter('labels', [], checkLabels);
parser.addParameter('filenameFormat', [], isString);
parser.addParameter('labelsFormat', [], isString);
parser.parse(varargin{:});
P = parser.Results;
%--------------------------------------------------------------------------
N = size(R, 4);
fid = fopen(listFile, 'a');
if fid == -1
error('Could not open list file for writing.');
end
if ~isempty(P.suffix)
P.suffix = ['_' P.suffix];
end
if isempty(P.filenameFormat)
filenameWidth = length(num2str(N));
P.filenameFormat = ['%0' num2str(filenameWidth) 'd'];
end
if ~isempty(P.labels)
if isempty(P.labelsFormat)
labelsWidth = length(num2str(max(P.labels(:))));
P.labelsFormat = ['%' num2str(labelsWidth) 'd'];
end
end
for i_R = 1:N
currentPath = fullfile(saveDir, ...
[num2str(i_R, P.filenameFormat) P.suffix '.' P.filetype]);
if ismember(P.filetype, {'jpg', 'jpeg'})
imwrite(R(:,:,:,i_R), currentPath, 'Quality', P.quality);
elseif ismember(P.filetype, {'mat'})
data__ = R(:,:,:,i_R); %#ok<NASGU>
save(currentPath, 'data__');
else
imwrite(R(:,:,:,i_R), currentPath);
end
fprintf(fid, currentPath);
if ~isempty(P.labels)
if size(P.labels, 1) > 1
for k = 1:size(P.labels,2)
fprintf(fid, [' ' P.labelsFormat], P.labels(i_R, k));
end
else % one set of labels for all files
for k = 1:length(P.labels)
fprintf(fid, [' ' P.labelsFormat], P.labels(k));
end
end
end
fprintf(fid, '\n');
end
fclose(fid);
end