Skip to content

Commit 2877b45

Browse files
authored
the functions to generate density maps
1 parent 23eb144 commit 2877b45

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

genDensityMap.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function [ density ] = genDensityMap( loc_map, hsize, sigma)
2+
% loc_map : an image marks the locations of object, where 0 means none and
3+
% 1 (or > 1) means the location.
4+
% hsize : the influential range of the guassian kernel, usually 4.0*6
5+
% sigma : the standard error of the guassian kernel, usually 4.0
6+
% density : the density map of the given location map. Usually, the
7+
% integral of the whole density map is equal to or smaller than that of the
8+
% location map.
9+
if size(loc_map,3) == 3
10+
loc_map = rgb2gray(loc_map);
11+
end
12+
m = double(max(loc_map(:)));
13+
loc_map = double(loc_map)./m;
14+
density = imfilter(loc_map, fspecial('gaussian', hsize, sigma));
15+
16+
end
17+

geometry_adaptive_kernel.m

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function [ density ] = geometry_adaptive_kernel( coordinates, img_sz, beta )
2+
% author: sheperd, wygamle@pku.edu.cn
3+
4+
if ~exist('beta','var')
5+
beta = 0.3;
6+
end
7+
hei = img_sz(1); wid = img_sz(2);
8+
coordinates = floor(abs(coordinates));
9+
10+
dist2 = @(i,j)((coordinates(i,1)-coordinates(j,1)).^2+(coordinates(i,2)-coordinates(j,2)).^2);
11+
dist = @(i,j)(dist2(i,j).^0.5);
12+
n = size(coordinates,1);
13+
matDist = zeros(n,n);
14+
for i = 1 : n
15+
for j = i+1 : n
16+
matDist(i,j) = dist(i,j);
17+
end
18+
end
19+
matDist = matDist + matDist';
20+
21+
coordinates = int32(coordinates);
22+
23+
density = zeros(hei,wid);
24+
k = 5;
25+
for i = 1 : n
26+
delta = zeros(hei,wid);
27+
r = max(min(coordinates(i,2),hei),1);
28+
c = max(min(coordinates(i,1),wid),1);
29+
delta(r,c) = 1;
30+
31+
dv = matDist(i,:);
32+
dv(i) = inf;
33+
34+
d = 0;
35+
for j = 1 : k
36+
[tmp, idx] = min(dv);
37+
d = d + tmp;
38+
dv(idx) = inf;
39+
end
40+
d = d ./ k;
41+
sigma = beta*d;
42+
hsize = max(floor(4 * sigma),3);
43+
f = fspecial('gaussian',hsize,sigma);
44+
delta = imfilter(delta, f);
45+
density = density + delta;
46+
end
47+
48+
end
49+

0 commit comments

Comments
 (0)