-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen.m
93 lines (78 loc) · 3.54 KB
/
gen.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
function gen
addpath ./FCRN-DepthPrediction/matlab/
addpath ./mcg/pre-trained/
install();
% -------------------------------------------------------------------------
% Setup MatConvNet
% -------------------------------------------------------------------------
matconvnet_path = '/export/wangqingze/matconvnet-1.0-beta20';
setupMatConvNet(matconvnet_path);
% -------------------------------------------------------------------------
% Options for depth prediction model
% -------------------------------------------------------------------------
opts.interp = 'nearest'; % interpolation method applied during resizing
opts.imageSize = [460, 345]; % desired image size for evaluation
opts.dataDirImages = '/export/wangqingze/py-faster-rcnn/data/VOCdevkit2007/VOC2007/JPEGImages/';
opts.dataDir = fullfile(pwd, 'depth'); % working directory
netOpts.gpu = false; % set to true to enable GPU support
netOpts.plot = false; % set to true to visualize the predictions during inference
net = get_dp_model(opts);
img_list=dir('/export/wangqingze/py-faster-rcnn/data/VOCdevkit2007/VOC2007/JPEGImages/*.jpg');
fd = fopen('img_list', 'w');
for i = 1:numel(img_list)
fprintf(fd, img_list(i).name);
fprintf(fd, '\n');
end
%filename = 'dset.h5';
%if exist(filename, 'file')
% delete(filename);
%end
basename = 'dset';
batch = 500;
for i = 1:numel(img_list)
%for i = 1:1:5
if mod(i - 1, batch) == 0
filename = strcat(basename, '_', int2str(i / batch), '.h5');
end
img = imread(fullfile(opts.dataDirImages, img_list(i).name));
h5create(filename, strcat('/image/', img_list(i).name), [size(img, 3), size(img, 2), size(img, 1)], 'Datatype', 'uint8');
h5write(filename, strcat('/image/', img_list(i).name), permute(img, [3, 2, 1]));
%imwrite(uint8(img), img_list(i).name, 'jpeg');
depth = DepthMapPrediction(img, net, netOpts);
depth = imresize(depth, [size(img, 1), size(img, 2)]);
h5create(filename, strcat('/depth/', img_list(i).name), [size(depth, 1), size(depth, 2)], 'Datatype', 'single');
h5write(filename, strcat('/depth/', img_list(i).name), single(depth));
%seg = im2ucm(img, 'fast');
seg = im2ucm(img, 'accurate');
seg = imresize(seg, [size(img, 1), size(img, 2)]);
level = graythresh(seg);
seg = im2bw(seg, level);
seg = ~seg;
cc = bwconncomp(seg);
seg = labelmatrix(cc);
seg = seg + 1;
% numPixels = cellfun(@numel, cc.PixelIdxList);
% [biggest, idx] = max(numPixels);
% seg(cc.PixelIdxList{idx}) = 255;
% imwrite(seg, img_list(i).name, 'jpeg');
cnt = tabulate(seg(:));
area = (cnt(:, 2))';
label = (cnt(:, 1))';
h5create(filename, strcat('/seg/', img_list(i).name), [size(depth, 1), size(depth, 2)], 'Datatype', 'uint16');
h5write(filename, strcat('/seg/', img_list(i).name), uint16(seg));
h5writeatt(filename, strcat('/seg/', img_list(i).name), 'area', int64(area));
h5writeatt(filename, strcat('/seg/', img_list(i).name), 'label', int64(label));
end
% -------------------------------------------------------------------------
% get depth prediction model
% -------------------------------------------------------------------------
function net = get_dp_model(opts)
opts.dataDir = fullfile(opts.dataDir, 'models');
if ~exist(opts.dataDir, 'dir'), mkdir(opts.dataDir); end
filename = fullfile(opts.dataDir, 'Make3D_ResNet-UpProj.mat');
if ~exist(filename, 'file')
url = 'http://campar.in.tum.de/files/rupprecht/depthpred/Make3D_ResNet-UpProj.zip';
fprintf('downloading trained model: %s\n', url);
unzip(url, opts.dataDir);
end
net = load(filename);