-
Notifications
You must be signed in to change notification settings - Fork 10
/
gen_density_map.m
124 lines (99 loc) · 3.54 KB
/
gen_density_map.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
clear
% Configuration
dataset = 'dataset_01';
display = 0;
head_only = 0;
gauss_size = 120;
person_height = gauss_size * 4;
gauss_std_low_thresh = 0.8;
addpath('../utils');
% Get the scene list in specified dataset
scene_list = dir(fullfile('..', 'data', dataset, 'frames'));
scene_list = {scene_list([scene_list(:).isdir]).name};
scene_list(1:2) = [];
for index = 1:numel(scene_list)
scene_id = scene_list{index};
% Loading the perspective map of the current scene
pers_map_file = fullfile('..', 'data', dataset, 'perspective', ...
[scene_id '.mat']);
fprintf('Loading %s\n', pers_map_file);
load(pers_map_file);
% The following code fix a historical problem
if exist('pMap_out', 'var')
pMap = pMap_out;
clear pMap_out
end
[height, width, channels] = size(pMap);
frame_dir = fullfile('..', 'data', dataset, 'frames', scene_id);
label_dir = fullfile('..', 'data', dataset, 'labels', scene_id);
file_list = dir([label_dir '/*.mat']);
output_dir = fullfile('..', 'output', dataset, 'density_map', scene_id);
mkdir_if_not_exist(output_dir);
for i = 1:numel(file_list)
[~, basename, ~] = fileparts(file_list(i).name);
fprintf('%s\n', basename);
load(fullfile(label_dir, [basename '.mat']));
% Skip images with no person
if point_num <= 0
continue
end
density_out = zeros(height + person_height, width + gauss_size);
for k = 1:point_num
x = point_position(k, 1);
y = point_position(k, 2);
% Skip error points
if y > height || x > width || y < 1 || x < 1
continue
end
pers_value = ceil(pMap(y, x));
lamda = pers_value * 0.15;
% Discard too small people
if lamda < gauss_std_low_thresh
continue
end
if lamda > 0.20 * gauss_size
lamda = 0.20 * gauss_size;
warning([basename ': lambda is greater than 0.20 * gauss_size']);
end
% Set head
head = fspecial('gaussian', gauss_size, lamda);
% set body
temp_a = pdf('norm', 1:person_height, gauss_size / 2 + pers_value, lamda * 4);
temp_b = pdf('norm', 1:gauss_size, gauss_size / 2, lamda);
body = temp_a' * temp_b * 4;
if head_only
body(:) = 0.0; %#ok<UNRCH>
end
% combine body with head
body(1:gauss_size, 1:gauss_size) = body(1:gauss_size, 1:gauss_size) + head;
% Normalization
body(body < 0.000001) = 0.0;
person = body ./ sum(body(:));
density_out(y:(y+person_height-1), x:(x+gauss_size-1)) = ...
density_out(y:(y+person_height-1), x:(x+gauss_size-1)) + person;
end
density_map = density_out(gauss_size / 2:(gauss_size / 2 + height - 1), gauss_size / 2:(gauss_size / 2 + width - 1));
save(fullfile(output_dir, [basename '.mat']), 'density_map');
if display
im = imread(fullfile(frame_dir, [basename '.jpg'])); %#ok<UNRCH>
figure(1)
subplot(2, 1, 1);
imshow(im);
hold on
% Plot ground truth
plot(point_position(:, 1), point_position(:, 2), 'r.');
tl = title(sprintf('%s: %s', scene_id, basename));
set(tl, 'Interpreter', 'none')
subplot(2, 1, 2);
title(sprintf('sum(density)=%.1f', sum(density_map(:))))
imagesc(density_map);
hold on
% Plot ground truth again for double-check
plot(point_position(:, 1), point_position(:, 2), 'r.');
waitforbuttonpress
end
end
end
if display
close %#ok<UNRCH>
end