Skip to content

Instantly share code, notes, and snippets.

@chexov
Last active August 22, 2021 08:35
Show Gist options
  • Save chexov/1157499c429a7bcaca082787091f034a to your computer and use it in GitHub Desktop.
Save chexov/1157499c429a7bcaca082787091f034a to your computer and use it in GitHub Desktop.
How to Generate Gaussian Heatmap on numpy
import cv2
import numpy
def gaussian_heatmap(sigma: int, spread: int):
extent = int(spread * sigma)
center = spread * sigma / 2
heatmap = numpy.zeros([extent, extent], dtype=numpy.float32)
for i_ in range(extent):
for j_ in range(extent):
heatmap[i_, j_] = 1 / 2 / numpy.pi / (sigma ** 2) * numpy.exp(
-1 / 2 * ((i_ - center - 0.5) ** 2 + (j_ - center - 0.5) ** 2) / (sigma ** 2))
heatmap = (heatmap / numpy.max(heatmap) * 255).astype(numpy.uint8)
return heatmap
hm = gaussian_heatmap(sigma=10, spread=3)
hm = cv2.applyColorMap(hm, cv2.COLORMAP_JET)
cv2.imshow("hm", hm)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment