-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimageAugmentation.m
121 lines (119 loc) · 4.6 KB
/
imageAugmentation.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
% This script is to augment the existing positive images by rotation and
% mirroring.
% This script is different from negImgAugmentation
%% image rotation
files = dir('training_dataset/*.txt');
steps = 4;
parfor fileID = 1:numel(files)
filename = files(fileID).name;
fullname = fullfile('training_dataset',filename);
copyfile(fullname, 'generatedLabelFiles_rotate/')
imagefolder='positive';
[imagenames, positions] = readLabel(fullname,imagefolder);
trueboxes = cell2mat(positions);
image_name = imagenames{1};
copyfile(image_name, 'generated_positive_rotate/')
img = imread(image_name);
for step=1:steps
angle = -180+step*360/steps;
if (angle==0)
continue;
end
[rimg, rboxes]=viarotate(img,trueboxes,angle);
imname = ['generated_positive_rotate/rotate_', num2str(angle),'degree_',...
filename(1:end-4),'.jpg'];
imwrite(rimg,imname,'jpg');
dataname = ['generatedLabelFiles_rotate/rotate_', num2str(angle),'degree_',...
filename];
dlmwrite(dataname,rboxes,'delimiter',' ');
%showbbox(rimg,rboxes);
end
end
% %% image resize
% files = dir('generatedLabelFiles_rotate/*.txt');
% [steps, low, high] = deal(4,0.6,1.4);
% parfor fileID = 1:numel(files)
% filename = files(fileID).name;
% fullname = fullfile('generatedLabelFiles_rotate',filename);
% imagefolder='generated_positive_rotate';
% [imagenames, positions] = readLabel(fullname,imagefolder);
% trueboxes = cell2mat(positions);
% image_name = imagenames{1};
% img = imread(image_name);
% movefile(image_name, 'generated_positive_resize/')
% movefile(fullname, 'generatedLabelFiles_resize/')
% for step = 0:steps
% ratio = low+(high-low)*step/steps;
% if (ratio == 1)
% continue;
% end
% rimg = imresize(img,ratio);
% rboxes = round(trueboxes*ratio);
% [Y,X] = size(rimg);
% rboxes = checkboxes(rboxes,X,Y);
% imname = ['generated_positive_resize/resize', num2str(ratio),'ratio_',...
% filename(1:end-4),'.jpg'];
% imwrite(rimg,imname,'jpg');
% dataname = ['generatedLabelFiles_resize/resize', num2str(ratio),'ratio_',...
% filename];
% dlmwrite(dataname,rboxes,'delimiter',' ');
% %showbbox(rimg,rboxes);
% end
% end
% %% image change aspect ratio
% files = dir('generatedLabelFiles_resize/*.txt');
% [steps, low, high] = deal(4,0.8,1.2);
% parfor fileID = 1:numel(files)
% filename = files(fileID).name;
% fullname = fullfile('generatedLabelFiles_resize',filename);
% imagefolder='generated_positive_resize';
% [imagenames, positions] = readLabel(fullname,imagefolder);
% trueboxes = cell2mat(positions);
% image_name = imagenames{1};
% img = imread(image_name);
%
% for step = 0:steps
% ratio = low+(high-low)*step/steps;
% if (ratio == 1)
% continue;
% end
% [numrows, numcols] = size(img);
% rimg = imresize(img,[numrows,round(ratio*numcols)]);
% rboxes = trueboxes;
% rboxes(:,[1,3]) = round(rboxes(:,[1,3])*ratio);
% [Y,X] = size(rimg);
% rboxes = checkboxes(rboxes,X,Y);
% imname = ['generated_positive_aspect/reshape', num2str(ratio),'ratio_',...
% filename(1:end-4),'.jpg'];
% imwrite(rimg,imname,'jpg');
% dataname = ['generatedLabelFiles_aspect/reshape', num2str(ratio),'ratio_',...
% filename];
% dlmwrite(dataname,rboxes,'delimiter',' ');
% %showbbox(rimg,rboxes);
% end
% movefile(fullname, 'generatedLabelFiles_aspect/')
% movefile(image_name, 'generated_positive_aspect/')
% end
%% image mirror
files = dir('once/*.txt');
parfor fileID = 1:numel(files)
filename = files(fileID).name;
fullname = fullfile('once',filename);
imagefolder='positive';
[imagenames, positions] = readLabel(fullname,imagefolder);
trueboxes = cell2mat(positions);
image_name = imagenames{1};
% img = imread(image_name);
%
% rimg = flip(img,2);
% rboxes = trueboxes;
% [Y,X]=size(rimg);
% rboxes(:,1)=X-trueboxes(:,1)-trueboxes(:,3)+1;
% imname = ['aug_training_positive/fliped_', filename(1:end-4),'.jpg'];
% imwrite(rimg,imname,'jpg');
% dataname = ['aug_training_files/fliped_',filename];
% dlmwrite(dataname,rboxes,'delimiter',' ');
copyfile(fullname, 'aug_training_files/')
copyfile(image_name, 'aug_training_positive/')
end
%%