我正在研究一个涉及密度图的研究问题。此代码假设您正在循环遍历文本文件列表,其中每个文本文件都有点注释(或者您正在从对象转换为点注释,就像我做的那样)。它还假设您有一个 annotations 列表,其中 (x,y) 中心 points 可以使用(在读取/处理所述文本文件之后)。
你可以在这里找到 good 的实现:
https://github.com/CommissarMa/MCNN-pytorch/blob/master/data_preparation/k_nearest_gaussian_kernel.py
上面有一些用于自适应内核的额外代码。
上下文中的以下代码(带有更多“绒毛”)在这里:
https://github.com/MattSkiff/cow_flow/blob/master/data_loader.py
这是我使用的代码:
# running gaussian filter over points as in crowdcount mcnn
density_map = np.zeros((img_size[1],img_size[0]), dtype=np.float32)
# add points onto basemap
for point in annotations:
base_map = np.zeros((img_size[1], img_size[0]), dtype=np.float32)
# subtract 1 to account for 0 indexing
base_map[int(round(point[1]*img_size[1])-1),
int(round(point[0]*img_size[0])-1)] += 1
density_map += scipy.ndimage.filters.gaussian_filter(base_map, sigma = sigma, mode='constant')
这应该会创建一个密度图,可以满足您的需求。在 matplotlib 中的 ax 对象上使用“imshow”(例如ax.imshow(density,cmap='hot',interpolation='nearest')应该会产生一个像这样的密度图(我已经添加了航空图像来指示被标记的内容):