-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaugmentData.m
48 lines (43 loc) · 1.46 KB
/
augmentData.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
function B = augmentData(A)
% Apply random horizontal flipping, and random X/Y scaling. Boxes that get
% scaled outside the bounds are clipped if the overlap is above 0.25. Also,
% jitter image color.
B = cell(size(A));
I = A{1};
sz = size(I);
if numel(sz)==3 && sz(3) == 3
I = jitterColorHSV(I,...
'Contrast',0.2,...
'Hue',0,...
'Saturation',0.1,...
'Brightness',0.2);
end
% Randomly flip and scale image.
tform = randomAffine2d('XReflection',true,'Scale',[1 1.1],'Rotation',[-20 20]);
rout = affineOutputView(sz,tform,'BoundsStyle','CenterOutput');
B{1} = imwarp(I,tform,'OutputView',rout);
% Sanitize boxes, if needed.
A{2} = helperSanitizeBoxes(A{2}, sz);
% Apply same transform to boxes.
[B{2},indices] = bboxwarp(A{2},tform,rout,'OverlapThreshold',0.25);
B{3} = A{3}(indices);
% Return original data only when all boxes are removed by warping.
if isempty(indices)
B = A;
end
end
% helperSanitizeBoxes Sanitize box data.
% If none of the boxes are valid, this function passes the data through to
% enable downstream processing to issue proper errors.
function boxes = helperSanitizeBoxes(boxes, ~)
persistent hasInvalidBoxes
valid = all(boxes > 0, 2);
if any(valid)
if ~all(valid) && isempty(hasInvalidBoxes)
% Issue one-time warning about removing invalid boxes.
hasInvalidBoxes = true;
warning('Removing ground truth bouding box data with values <= 0.')
end
boxes = boxes(valid,:);
end
end