Here is my test for density map creation, I draw 10 random points and than create density map, however sum over map is not 10, but 6.95563, for 100 points it's 91.6066 and it depens on point positions, how can we approximate it more precise?
import numpy as np
import scipy
import scipy.spatial
import scipy.ndimage
import cv2
from random import randint
def gaussian_filter_density(mask):
density = np.zeros(mask.shape, dtype=np.float32)
gt_count = np.count_nonzero(mask)
if gt_count == 0:
return density
pts = np.array(zip(np.nonzero(mask)[1], np.nonzero(mask)[0]))
leafsize = 2048
# build kdtree
tree = scipy.spatial.KDTree(pts.copy(), leafsize=leafsize)
# query kdtree
distances, locations = tree.query(pts, k=2, eps=10.)
for i, pt in enumerate(pts):
pt2d = np.zeros(mask.shape, dtype=np.float32)
pt2d[pt[1],pt[0]] = 1.
if gt_count > 1:
sigma = distances[i][1]
else:
sigma = np.average(np.array(mask.shape))/2./2. #case: 1 point
density += scipy.ndimage.filters.gaussian_filter(pt2d, sigma, mode='constant')
print "np.sum(density): ", np.sum(density)
return density
def test_density():
h= 100
w= 120
mask= np.zeros((h,w))
for i in range(0,10):
y= randint(0,h-1)
x= randint(0,w-1)
mask[y,x]= 1
density= gaussian_filter_density(mask)
cv2.imwrite('mask.png', 255*mask)
cv2.imwrite('density.png', 255*density)
test_density()
Here is my test for density map creation, I draw 10 random points and than create density map, however sum over map is not 10, but 6.95563, for 100 points it's 91.6066 and it depens on point positions, how can we approximate it more precise?