forked from davidstutz/nyu-depth-v2-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrop_dataset.m
183 lines (149 loc) · 6.4 KB
/
crop_dataset.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
function [] = crop_dataset()
% function [] = crop_dataset()
%
% Due to the Kinect, there is a small border around the image which is
% white and not important for the labelling. For using superpixel
% algorithms, this area should be removed.
%
% This function removes this border for all images of the dataset which has
% been converted using convert_dataset.m.
%
% Therefore it first determines the maximum white region at each border and
% prints them (maxLeft, maxRight, maxTop, maxBottom). The maximum of these
% values is the number of pixels to be cut off at each border.
%
% Currently the number of pixels cropped at each border is 16px! See below
% to change this!
%
% Note that in the adapted version of convert_dataset.m the background
% label is one.
%
% David Stutz <[email protected]>
directories = {'train'; 'test'};
% The directories to use for cropping the dataset.
% Remember to relabel the ground truth segmentations first if semantic
% segmentation is not needed.
imgBaseDir = './NYUDepthV2/original/data/images/';
depthBaseDir = './NYUDepthV2/original/data/depth/';
gtBaseDir = './NYUDepthV2/original/data/groundTruth/';
% The output directories, use different directories to not overwrite
% the original dataset.
imgOutBaseDir = './NYUDepthV2/cropped-original/data/images/';
depthOutBaseDir = './NYUDepthV2/cropped-original/data/depth/';
gtOutBaseDir = './NYUDepthV2/cropped-original/data/groundTruth/';
maxLeft = 0;
maxRight = 0;
maxTop = 0;
maxBottom = 0;
for d = 1: size(directories, 1)
inDir = [imgBaseDir directories{d}];
segDir = [gtBaseDir directories{d}];
images = dir(sprintf('%s/*.jpg', inDir));
for i = 1: numel(images)
image = imread(sprintf('%s/%s', inDir, images(i).name));
groundTruth = load(sprintf('%s/%s.mat', segDir, images(i).name(1:end - 4)));
seg = groundTruth.groundTruth{1,1}.Segmentation;
% determine the number of white pixels at the left and right border
for k = 10: size(image, 1) - 10
% left border
l = 1;
count = 0;
while l <= size(image, 2) && image(k, l, 1) == 255 && image(k, l, 2) == 255 && image(k, l, 3) == 255 && seg(k, l) == 1
l = l + 1;
count = count + 1;
end;
if count > maxLeft
maxLeft = count;
end;
% right border
l = size(image, 2);
count = 0;
while l > 0 && image(k, l, 1) == 255 && image(k, l, 2) == 255 && image(k, l, 3) == 255 && seg(k, l) == 1
l = l - 1;
count = count + 1;
end;
if count > maxRight
maxRight = count;
end;
end;
% determine the number of white pixels at top and bottom border
for l = 10: size(image, 2) - 10
k = 1;
count = 0;
while k <= size(image, 1) && image(k, l, 1) == 255 && image(k, l, 2) == 255 && image(k, l, 3) == 255 && seg(k, l) == 1
k = k + 1;
count = count + 1;
end;
if count > maxTop
maxTop = count;
end;
% right border
k = size(image, 1);
count = 0;
while k > 0 && image(k, l, 1) == 255 && image(k, l, 2) == 255 && image(k, l, 3) == 255 && seg(k, l) == 1
k = k - 1;
count = count + 1;
end;
if count > maxBottom
maxBottom = count;
end;
end;
end;
end;
cutOff = maxTop;
if maxBottom > cutOff
cutOff = maxBottom;
end;
if maxLeft > cutOff
cutOff = maxLeft;
end;
if maxRight > cutOff
cutOff = maxRight;
end;
cutOff = 16;
fprintf('%d pixels will be cut off at each border!\n', cutOff);
for d = 1: size(directories, 1)
inDir = [imgBaseDir directories{d}];
depthDir = [depthBaseDir directories{d}];
segDir = [gtBaseDir directories{d}];
outDir = [imgOutBaseDir directories{d}];
outDepthDir = [depthOutBaseDir directories{d}];
outSegDir = [gtOutBaseDir directories{d}];
if ~exist(outDir)
system(['mkdir -p ' outDir]);
end;
if ~exist(outDepthDir)
system(['mkdir -p ' outDepthDir]);
end;
if ~exist(outSegDir)
system(['mkdir -p ' outSegDir]);
end;
images = dir(sprintf('%s/*.jpg', inDir));
for i = 1: numel(images)
% crop image
imagePath = sprintf('%s/%s', inDir, images(i).name);
image = imread(imagePath);
height = size(image, 1);
width = size(image, 2);
newImage = image(cutOff + 1:height - cutOff, cutOff + 1:width - cutOff, :);
imwrite(newImage, sprintf('%s/%s.jpg', outDir, images(i).name(1:end - 4)));
% crop depth image
depthPath = sprintf('%s/%s.png', depthDir, images(i).name(1:end - 4));
depthImage = imread(depthPath);
newDepthImage = depthImage(cutOff + 1:height - cutOff, cutOff + 1:width - cutOff, :);
imwrite(newDepthImage, sprintf('%s/%s.png', outDepthDir, images(i).name(1:end - 4)));
% crop ground truth segmentation
gtPath = sprintf('%s/%s.mat', segDir, images(i).name(1:end - 4));
oldGroundTruth = load(gtPath);
seg = oldGroundTruth.groundTruth{1,1}.Segmentation;
bdry = oldGroundTruth.groundTruth{1,1}.Boundaries;
newSeg = seg(cutOff + 1:height - cutOff, cutOff + 1:width - cutOff);
newBdry = bdry(cutOff + 1:height - cutOff, cutOff + 1:width - cutOff);
groundTruth = cell(1);
groundTruth{1}.Segmentation = newSeg;
groundTruth{1}.Boundaries = newBdry;
save(sprintf('%s/%s.mat', outSegDir, images(i).name(1:end - 4)), 'groundTruth');
csvwrite(sprintf('%s/%s.csv', outStructureGTDir, images(i).name(1:end - 4)), newLabels);
end;
end;
end