-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_training_data_single_category.m
executable file
·132 lines (113 loc) · 4.25 KB
/
prepare_training_data_single_category.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
% TODO: remove un-labeled regions
% Set threshold depending on video
clear all
close all
clc
numImagesForBackground = 1000;
img_h = 240;
img_w = 320;
threshold = [0 0.05 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]; % threshold for simple background subtraction
dataPath = '/datasets/backsub/cdnet2014';
% Video categories:
% 1:PTZ, 2:badWeather, 3:baseline, 4:camerajitter, 5:dynamicBackground,
% 6:intermittentObjectMotion, 7:lowFramerate, 8:nightVideos, 9:shadow,
% 10:thermal, 11:turbulence
category = 8;
datasetPath = fullfile(dataPath, 'dataset');
categoryList = filesys('getFolders', datasetPath);
categoryPath = fullfile(datasetPath, categoryList{category});
videoList = filesys('getFolders', categoryPath);
output_dir = fullfile(dataPath, sprintf('dataForTraining%s',categoryList{category}))
output_imgs = fullfile(output_dir, 'images');
output_labels = fullfile(output_dir, 'groundtruth');
img = zeros(img_h, img_w, 1, 'double');
figure,
subplot(2,3,1)
h1 = imshow(img); % background image
title('Background image')
subplot(2,3,2)
h2 = imshow(img); % current image
title('Current frame')
subplot(2,3,3)
h3 = imshow(img); % simple foreground
title('Simple foreground')
subplot(2,3,4)
h4 = imshow(img); % image for model
title('Image for training')
subplot(2,3,5)
h5 = imshow(img); % image for model
title('Label image')
% Create output directories, if already exists
if ~exist(output_dir,'dir')
mkdir(output_dir)
mkdir(output_imgs)
mkdir(output_labels)
else
rmdir(output_dir, 's')
pause(1);
mkdir(output_dir)
mkdir(output_imgs)
mkdir(output_labels)
end
% For each video on the category
count_img = 0;
for vIdx = 1:length(videoList)
video = videoList{vIdx};
videoPath = fullfile(categoryPath, video);
imagesPath = fullfile(videoPath, 'input');
labelsPath = fullfile(videoPath, 'groundtruth');
imageFiles = filesys('getFiles', imagesPath);
labelFiles = filesys('getFiles', labelsPath);
% Get the temporal ROI
tempROI = dlmread(fullfile(videoPath, 'temporalROI.txt'));
initFrame = tempROI(1);
finalFrame = tempROI(2);
% Estimate the background
fprintf('Estimating background of video %s ...\n', video)
if numImagesForBackground >= (finalFrame - initFrame)
numImagesForBackground = finalFrame - initFrame - 1;
end
backgroundImg = estimateBackground(imagesPath, initFrame, numImagesForBackground);
backgroundImg = rgb2gray(im2double(backgroundImg));
backgroundImg = imresize(backgroundImg,[img_h img_w]);
% for each frame on the video
disp('Creating frames ...')
for i = initFrame:finalFrame
% Get the frame names
imName = fullfile(imagesPath, imageFiles{i});
labelImgName = fullfile(labelsPath, labelFiles{i});
% Get the image and resize it
img = imread(imName);
img = imresize(img,[img_h img_w]);
img = rgb2gray(im2double(img));
% Get the label and resize it
labelImg = imread(labelImgName);
labelImg = imresize(labelImg,[img_h img_w], 'nearest');
% Get the foreground by simple background subtraction
bgsub = img;
bgsub(abs(img - backgroundImg) <= threshold(category) ) = 0;
bgsub(bgsub > 0) = 1;
% bgsub = imopen(bgsub, strel('rectangle', [3,3]));
% bgsub = imclose(bgsub, strel('rectangle', [3, 3]));
% bgsub = imfill(bgsub, 'holes');
% Zero-out the unlabeled regions
img(labelImg == 85) = 0;
backgroundImg(labelImg == 85) = 0;
bgsub(labelImg == 85) = 0;
labelImg(labelImg == 85) = 0;
% Create input data by stacking the 3 frames
imgForModel = cat(3, img, backgroundImg, bgsub);
% Display the frames
set(h1, 'CData', backgroundImg);
set(h2, 'CData', img);
set(h3, 'CData', bgsub);
set(h4, 'CData', imgForModel);
set(h5, 'CData', labelImg);
drawnow;
% Write image and label to disk
imwrite(imgForModel,fullfile(output_imgs, sprintf('in%.6d.jpg', count_img)))
imwrite(labelImg,fullfile(output_labels, sprintf('gt%.6d.png', count_img)))
count_img = count_img + 1;
end
end
disp('Finish!');