-
Notifications
You must be signed in to change notification settings - Fork 3
/
negImgAugmentation.m
143 lines (115 loc) · 4.5 KB
/
negImgAugmentation.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
% This script is to augment the existing negative images by rotation,
% resizing, changing aspect ratio, and mirroring.
% The script can be a little complicated as it deals with different image
% data folders. You may need to change some folder name and block some
% section of code. For example, if you want to augment the "good" negative
% more times than "not-so-good" negative images, you may put the "good" and
% "not-so-good" negative images in seperate folders. And run the script for
% "good" image with this script. Then run the script for "not-so-good" image
% folder with some sections of the code blocked, such as changing aspect
% ratio or resizing section. Thus we can augment images selectively, making
% "good" images more frequent.
%% image rotation
% change the folder name if needed. Note you may also need to chaneg the
% corresponding name in side the fullfile() function.
files = dir('good_negative_images/*.jpg');
steps = 4;
parfor fileID = 1:numel(files)
filename = files(fileID).name;
fullname = fullfile('good_negative_images',filename);
image_name = fullname;
copyfile(image_name, 'generated_negative_rotate/')
img = imread(image_name);
for step=1:steps
angle = -180+step*360/steps;
if (angle==0)
continue;
end
rimg = imrotate(img,angle,'crop');
imname = ['generated_negative_rotate/rotate_', num2str(angle),'degree_',...
filename];
imwrite(rimg,imname,'jpg');
bimg = imgaussfilt(rimg, 2);
% add some blurred negative images
imname = ['aug_negative_images/blur_rotate', num2str(angle),'degree_',...
filename];
imwrite(bimg,imname,'jpg');
%showbbox(rimg,rboxes);
end
end
%% image resize
% change the folder name if needed. Note you may also need to chaneg the
% corresponding name in side the fullfile() function.
files = dir('generated_negative_rotate/*.jpg');
[steps, low, high] = deal(2,0.7,1.3);
parfor fileID = 1:numel(files)
filename = files(fileID).name;
fullname = fullfile('generated_negative_rotate',filename);
image_name = fullname;
img = imread(image_name);
for step = 1:steps
ratio = low+(high-low)*step/steps;
if (ratio == 1)
continue;
end
rimg = imresize(img,ratio);
imname = ['generated_negative_resize/resize', num2str(ratio),'ratio_',...
filename];
imwrite(rimg,imname,'jpg');
%showbbox(rimg,rboxes);
end
movefile(image_name, 'generated_negative_resize/')
end
%% image change aspect ratio
% change the folder name if needed. Note you may also need to chaneg the
% corresponding name in side the fullfile() function.
files = dir('generated_negative_resize/*.jpg');
[steps, low, high] = deal(2,0.8,1.2);
parfor fileID = 1:numel(files)
filename = files(fileID).name;
fullname = fullfile('generated_negative_resize',filename);
image_name = fullname;
img = imread(image_name);
for step = 0:steps-1
ratio = low+(high-low)*step/steps;
if (ratio == 1)
continue;
end
[numrows, numcols] = size(img);
rimg = imresize(img,[numrows,round(ratio*numcols)]);
imname = ['generated_negative_aspect/reshape', num2str(ratio),'ratio_',...
filename];
imwrite(rimg,imname,'jpg');
%showbbox(rimg,rboxes);
end
movefile(image_name, 'generated_negative_aspect/')
end
%% image mirror
% change the folder name if needed. Note you may also need to chaneg the
% corresponding name in side the fullfile() function.
files = dir('generated_negative_aspect/*.jpg');
parfor fileID = 1:numel(files)
filename = files(fileID).name;
fullname = fullfile('generated_negative_aspect',filename);
image_name = fullname;
img = imread(image_name);
rimg = flip(img,2);
imname = ['aug_negative_images/fliped_', filename];
imwrite(rimg,imname,'jpg');
movefile(image_name, 'aug_negative_images/')
end
% %% image blur
% files = dir('good_images/*.jpg');
% parfor fileID = 1:numel(files)
% filename = files(fileID).name;
% fullname = fullfile('good_images',filename);
%
% image_name = fullname;
%
% img = imread(image_name);
%
% rimg = imgaussfilt(img, 2);
%
% imname = ['aug_negative_images/blur_', filename];
% imwrite(rimg,imname,'jpg');
% end